import SdkData from "./entity/SdkData"; import SDKUtils from "../utils/SDKUtils"; import StorageUtils from "../utils/StorageUtils"; import { StorageKeys, GAMEDATA, LoginHost, GameHost, DotHost, OrderHost, GMHost } from "../base/SDKConst"; import { Gender, NetworkType } from "../base/SDKEnum"; import WxLaunch from "../wx/WxLaunch"; import LogService from "./LogService"; /** * SDK数据管理类 */ export default class DataService { private _data: SdkData; private constructor() { this._data = new SdkData; // 0点数据变动 let expiration = +((this.nextDay() - Date.now()) / 1000).toFixed(0); setTimeout(this.resetData, expiration * 1000); wx.onShow(this.onShow.bind(this)); wx.onHide(this.onHide.bind(this)); } onShow() { // console.log("dataOnshow"); this.updateTodayOnlineTime(); } onHide() { // console.log("dataOnhide"); clearInterval(this.timeInterval); } private resetData() { this.setPtm(0);//更新当天是否300s this.setLogindays(this.logindays + 1);// 更新登录天数 this.setIsnew(0);//更新新老用户 } setValue(key: string, val: any) { // console.error(key,val) if (SDKUtils.isUndefined(val)) return; this._data = { ...this._data, [key]: val }; } get Data(): SdkData { return this._data; } setLoginData(data: _LoginInnerData) { if (!data || Object.keys(data).length === 0) return; if (data.hasOwnProperty('openId')) this.setOpenId(data.openId); if (data.hasOwnProperty('channel')) this.setChannelId(data.channel); if (data.hasOwnProperty('userId')) this.setUserId(data.userId); if (data.hasOwnProperty('regTime')) this.setRegTime(data.regTime); if (data.hasOwnProperty('token')) this.setToken(data.token); if (data.hasOwnProperty('refToken')) this.setRefToken(data.refToken); if (data.hasOwnProperty('expire')) this.setExpice(data.expire); if (data.hasOwnProperty('isnew')) this.setIsnew(data.isnew); if (data.hasOwnProperty('logindays')) this.setLogindays(data.logindays); if (data.hasOwnProperty('amount')) this.setAmount(data.amount); if (data.hasOwnProperty('pon')) this.setPon(data.pon); if (data.hasOwnProperty('ptm')) this.setPtm(data.ptm); } get expice() { return this._data.expice } setExpice(val: any) { this.setValue('expire', val); return this; } get isnew() { return this._data.isnew } setIsnew(val: any) { this.setValue('isnew', val); return this; } get logindays() { return this._data.logindays; } setLogindays(val: any) { this.setValue('logindays', val); return this; } setAmount(val: any) { this.setValue('amount', val); return this; } get amount() { return this._data.amount; } setPon(val: any) { this.setValue('pon', val); return this; } // 用户类型:1微信投放2抖音3自然量0未知 get pon() { return this._data.pon; } setPtm(val: any) { this.setValue('ptm', val); return this; } // 300s用户判断 get ptm() { return this._data.ptm; } get Scene() { return this._data.scene || 0; } setScene(val: any) { this.setValue('scene', val); return this; } get ReferrerInfo() { return this._data.referrerInfo || {}; } setReferrerInfo(val: any) { this.setValue('referrerInfo', val); return this; } get Token() { return this._data.token || '';//|| StorageUtils.I.get(StorageKeys.SDKTokenKey) || } setToken(val: any) { this.setValue('token', val); // StorageUtils.I.set(StorageKeys.SDKTokenKey, val); return this; } get refToken() { return this._data.refToken } setRefToken(val: any) { this.setValue('refToken', val); return this; } get OpenId() { return this._data.openId; } private setOpenId(val: any) { this.setValue('openId', val); // this.fixLaunchKey(val); return this; } get UserId() { if (this._data.userId) return this._data.userId; else return this.OpenId; } setUserId(val: any) { this.setValue('userId', val); StorageUtils.I.set("userId", val); return this; } get RegTime() { return this._data.regTime; } setRegTime(val: any) { this.setValue('regTime', val); return this; } get LaunchTime() { return this._data.launchTime; } setLaunchTime(val: any) { this.setValue('launchTime', val); return this; } get ShareId() { return this._data.shareId; } setShareId(val: number) { this.setValue('shareId', val); return this; } get ShareKey() { return this._data.shareKey; } setShareKey(val: string) { this.setValue('shareKey', val); return this; } get Platform(): string { return this._data.platform; } setPlatform(val: string) { this.setValue('platform', val); return this; } setNetworkType(networkType: NetworkType) { this.setValue('networkType', networkType); return this; } get Gender(): Gender { return this._data.gender || Gender.Unknown; } setGender(val: Gender) { this.setValue('gender', val); return this; } get ChannelId(): any { return this._data.channelId; } setChannelId(val: any) { this.setValue('channelId', val); return this; } get QueryChannelId(): any { return this._data.queryChannelId || 0; } setQueryChannelId(val: any) { this.setValue('queryChannelId', val); return this; } get InviteType(): any { return this._data.inviteType || 0; } setInviteType(val: any) { this.setValue('inviteType', val); return this; } get QueryUserInviteUid(): any { return this._data.queryUserInviteUid || 0; } setQueryUserInviteUid(val: any) { //console.warn('====> PCSDK.data.setQueryUserInviteUid', val); this.setValue('queryUserInviteUid', val); return this; } get QueryExtData(): any { return this._data.queryExtData || {}; } setQueryExtData(val: any) { this.setValue('queryExtData', val); return this; } get UserInviteUid(): any { return this._data.inviteUid || 0; } setUserInviteUid(val: any) { this.setValue('inviteUid', val); return this; } // get UserState(): UserState { // return this._data.userState || UserState.Default; // } setUserState(val: any) { this.setValue('userState', val); return this; } get Shield(): any { return this._data.shield || StorageUtils.I.get(StorageKeys.SDKUserShieldKey) || 0; } setShield(val: any) { this.setValue('shield', val); StorageUtils.I.set(StorageKeys.SDKUserShieldKey, val); return this; } get ShareTicket(): any { return this._data.shareTicket; } setShareTicket(val: any) { this.setValue('shareTicket', val); return this; } get SystemId(): number { return this._data.systemId; } setSystemId() { let systemId = this.Platform === 'android' ? 1 : 0; this.setValue('systemId', systemId); return this; } get Authorize(): boolean { return this._data.authorize; } setAuthorize(val: boolean) { // DebugUtils.I.dynamic('====> PCSDK data setAuthorize: ' + val); this.setValue('authorize', val); return this; } get Version(): string { return this._data.version; } setVersion(val: string) { this.setValue('version', val); return this; } get CdnUrl(): string { return this._data.cdnUrl; } setCdnUrl(val: string) { this.setValue('cdnUrl', val); return this; } get EnvEnum() { return this._data.envEnum; } setEnvEnum(val: number) { this.setValue('envEnum', val); return this; } get LoginApi() { let path = LoginHost.Prod; if (this.EnvEnum === 1) { path = LoginHost.Pre } return path; } get GameApi() { let path = GameHost.Prod; if (this.EnvEnum === 1) { path = GameHost.Pre } return path; } get DotApi() { let path = DotHost.Prod; if (this.EnvEnum === 1) { path = DotHost.Pre } return path; } get OrderApi() { let path = OrderHost.Prod; if (this.EnvEnum === 1) { path = OrderHost.Pre } return path; } get GMApi() { let path = GMHost.Prod; if (this.EnvEnum === 1) { path = GMHost.Pre; } return path; } get GameId() { return GAMEDATA.game_id; } get LaunchKey() { return this._data.launchKey || StorageUtils.I.get(StorageKeys.SDKLaunchKey) || ''; } fixLaunchKey(openId: string) { if (openId) { StorageUtils.I.set(StorageKeys.SDKLaunchKey, openId); this.setValue('launchKey', openId); } return this; } // getEventLaunchKey(): string { // let key = StorageKeys.SDKEventLaunchKey; // let launchKey = StorageUtils.I.get(key); // if (!launchKey) { // launchKey = StringUtils.createCode(); // StorageUtils.I.set(key, launchKey); // } // return launchKey; // } // get LaunchSource() { // return this._data.launchSource; // } setLaunchSource() { let source = 0; let { query } = WxLaunch.I.LaunchData; if (query) { if (query.scene) { let arr = (decodeURIComponent(query.scene) as string).split('&'); arr[1] && (source = 1); } else if (query.user_invite_id || query.user_invite_uid || query.invite_unionid) source = 1; } else source = 0; this.setValue('launchSource', source); return this; } setClueToken(clue_token) { this.setValue('clue_token', clue_token) return this; } setCreativeId(creative_id) { this.setValue('creative_id', creative_id) return this; } setAdvertisterId(advertister_id) { this.setValue('advertister_id', advertister_id) return this; } setRequestId(request_id) { this.setValue('request_id', request_id) return this; } get IsCross(): boolean { return this._data.isCross; } setIsCross(val: boolean) { this.setValue('isCross', val); return this; } get IsDrawer(): boolean { return this._data.isDrawer; } setIsDrawer(val: boolean) { this.setValue('isDrawer', val); return this; } get IsGuessLike(): boolean { return this._data.isGuessLike; } setIsGuessLike(val: boolean) { this.setValue('isGuessLike', val); return this; } // public addOnlineTime(t) { // let expiration = +((this.nextDay() - Date.now()) / 1000).toFixed(0); // let time = StorageUtils.I.get("pcsdk_today_online_time") || 0; // time += t; // StorageUtils.I.set("pcsdk_today_online_time", time, expiration) // } private timeInterval public updateTodayOnlineTime() { let dot = StorageUtils.I.get("pcsdk_today_online_time_300") || 0; if (dot === 1) { this.setPtm(1) // 定时重启 let expiration = +((this.nextDay() - Date.now()) / 1000).toFixed(0); setTimeout(() => { this.updateTodayOnlineTime(); }, expiration * 1000); return; } else { this.setPtm(0) this.timeInterval = setInterval(this.stayFun.bind(this), 1000); } } private stayFun() { let expiration = +((this.nextDay() - Date.now()) / 1000).toFixed(0); let time = StorageUtils.I.get("pcsdk_today_online_time") || 0; time += 1; StorageUtils.I.set("pcsdk_today_online_time", time, expiration) let dot = StorageUtils.I.get("pcsdk_today_online_time_300") || 0; if (dot === 1) { this.setPtm(1) clearInterval(this.timeInterval); // 定时重启 setTimeout(() => { this.updateTodayOnlineTime(); }, expiration * 1000); return; } console.log("newUserTime", time, dot) if (time >= 300 && dot == 0) { StorageUtils.I.set("pcsdk_today_online_time_300", 1, expiration); this.setPtm(1) LogService.I.daystay(); clearInterval(this.timeInterval); // 定时重启 setTimeout(() => { this.updateTodayOnlineTime(); }, expiration * 1000); } } public nextDay() { var str = new Date(); var str2 = str.getFullYear() + "/" + (str.getMonth() + 1) + "/" + str.getDate(); return new Date(str2).getTime() + 24 * 60 * 60 * 1000 } private static instance: DataService; static get I(): DataService { return this.instance || (this.instance = new DataService()); } }