我面临着一个问题,如何获得真正的“可信”用户时间,防止他们在更改计算机时间时作弊。
无论我是使用普通的date对象,还是moment time date,甚至google timezone api,如果我尝试操作当前时间,我都无法获得用户的“真实”时间。
如果我们是在20:00 (与位置无关),而用户将时间设置为11:00,那么我总是以该时间结束,而不是真正的时间,或者
const time = new Date();
const timestamp = (time.getTime() / 1000 + time.getTimezoneOffset() * 60);
const url = 'https://maps.googleapis.com/maps/api/timezone/json?location=-31.369926900000003,-64.2218601×tamp=1568396292&key=MY_API_KEY';
this.httpDuplicate.get(url ).subscribe((res: any) => {
if (res) {
const dst = res.dstOffset;
const raw = res.rawOffset;
const fixed = (timestamp + raw + dst) * 1000;
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const currentTime = momenttz().tz(timezone).format();
console.warn('initial time ', new Date(time),
' - google time ', new Date(fixed),
' - moment timezone ', currentTime);
// all these values are wrong and point to 11:00 rather than 20:00
}
});有没有办法做到这一点?我最终想要的是为某个位置找到合适的时间……显然不信任用户的系统时间,但信任它的位置(尽管也有方法改变这一点)
发布于 2019-09-14 02:59:41
失败的原因是因为您引用了基于用户的客户端时间(' time‘变量)创建的Date对象。
如果您不信任系统时间,则必须从其他地方获取时间,例如您信任的外部服务器,或者您的应用程序的后端(如果您有一个服务器时间)。例如:http://worldtimeapi.org/
当然,在这种情况下,您还需要用户的位置。我可以想象你也不相信这一点,因为你也不相信时间,所以在这种情况下没有办法做到这一点。
https://stackoverflow.com/questions/57928906
复制相似问题