import DataService from "./DataService"; import { __LOG__, GAMEDATA } from "../base/SDKConst"; import { SDKApi } from "../http/SDKApi"; import WxSystem from "../wx/WxSystem"; import StorageUtils from "../utils/StorageUtils"; import { DOT_SHARE_TYPE, DOT_AD_TYPE, DOT_AD_STATUS, AdType } from "../base/SDKEnum"; import DateUtils from "../utils/DateUtils"; export default class LogService { // 曝光数据 private exposureArr: any; // 资源变更统计 private logResArr: _LogResData[]; // 关卡统计 private logLevelArr: _LogLevelData[]; // 是否已经处理请求了shareList接口 private isHandledShare: boolean; private timeoutShareId: any; // reg接口是否已经返回数据状态 private regFinishState: number; private lastReqBannerTime: any; private lastReqBannerData: any; private queue: Array = []; private constructor() { this.exposureArr = {}; this.logResArr = []; this.logLevelArr = []; this.regFinishState = 0; this.isHandledShare = false; this.timeoutShareId = 0; this.lastReqBannerTime = {}; this.lastReqBannerData = {}; } setRegFinishState(state: number) { this.regFinishState = state; } private get SystemId() { return DataService.I.SystemId; } private get LaunchTime() { return DataService.I.LaunchTime; } private get LaunchKey() { return DataService.I.LaunchKey; } // private get EventLaunchKey() { // return DataService.I.getEventLaunchKey(); // } // private get LaunchSource() { // return DataService.I.LaunchSource; // } private get Scene() { return DataService.I.Scene; } private get RegTime() { return DataService.I.RegTime; } private get UserId() { return DataService.I.UserId; } private get OpenId() { return DataService.I.OpenId; } private get UserInviteUid() { return DataService.I.UserInviteUid; } private get QueryUserInviteUid() { return DataService.I.QueryUserInviteUid; } private get QueryExtData() { return DataService.I.QueryExtData; } private get Shield() { return DataService.I.Shield; } private get shareKey() { return DataService.I.ShareKey; } private get shareId() { return DataService.I.ShareId; } fixLaunchKey(launch_key: string) { DataService.I.fixLaunchKey(launch_key); } //是否登录 isLogin: boolean = false; /** * 内部游戏调用设置:是否新老用户 * @param data */ setLogind(data: _LoginInnerData) { DataService.I.setLoginData(data); this.isLogin = true; if (this.queue.length > 0) { for (let i = 0; i < this.queue.length; i++) { let fun = this.queue[i]; fun(); } } this.queue = []; } async dot(eventkey: string, options = {}) { //2020-12-31 改二级key let itemkey = ''; let itemvalue = ''; if (JSON.stringify(options) != '{}' && typeof options === 'object') { for (let k in options) { if (itemkey === '') { itemkey = k; itemvalue = options[k]; } } } // !this.isLogin && await PromiseSame.get(SDKTools.login, 'login') let fun = () => SDKApi.dot({ ...this.buildParams(), eventkey, value: JSON.stringify(options), itemkey, itemvalue }); this.checkLogin(fun); } /** * 关卡开始 * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 * @param stagename 关卡名称,格式:"xx模式-第x关" * @param pattern 模式名称,格式:"xx模式" */ async levelStart(stageid, stagename, pattern) { let fun = () => SDKApi.level({ ...this.buildParams(), stageid, stagename, pattern, typ: 1 }) this.checkLogin(fun); } /** * 关卡进行中 * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 * @param stagename 关卡名称,格式:"xx模式-第x关" * @param pattern 模式名称,格式:"xx模式" * @param event 事件 tools:使用道具 award:奖励 * @param params_id 道具ID * @param params_name 道具名称 * @param params_count 道具数量 * @param params_desc 道具描述 */ async levelRunning(stageid, stagename, pattern, event, params_id, params_name, params_count, params_desc?) { let fun = () => SDKApi.level({ ...this.buildParams(), stageid, stagename, pattern, event, params_id, params_name, params_count, params_desc, typ: 2 }) this.checkLogin(fun); } /** * 关卡结束 * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 * @param stagename 关卡名称,格式:"xx模式-第x关" * @param pattern 模式名称,格式:"xx模式" * @param event complete:成功 fail:失败 * @param times 时间 * @param perc 失败时的完成进度 (浮点数) */ async levelEnd(stageid, stagename, pattern, event, times,perc?) { let fun = () => SDKApi.level({ ...this.buildParams(), stageid, stagename, pattern, event, times, perc, typ: 3 }) this.checkLogin(fun); } /** * loading资源完成,新版加载游戏资源完成时调用 */ async loadingFinish() { let fun = () => SDKApi.loadingFinish({ ...this.buildParams() }) this.checkLogin(fun); // 30秒及时 this.startStay() } async active() { let fun = () => SDKApi.active({ ...this.buildParams(), isnew: 0, sharekey: this.shareKey, shareid: this.shareId }) this.checkLogin(fun); } /** * 用户log - 退出登录 * @param time_len 用户在线时长,单位毫秒 */ logOut(time_len: number) { let fun = () => SDKApi.logOut({ ...this.buildParams(), times: time_len }) this.checkLogin(fun); } /** * * @param sharekey * @param type 1分享 2点击 */ share(sharekey: string, shareid: number, type: DOT_SHARE_TYPE) { let fun = () => SDKApi.share({ ...this.buildParams(), typ: type, sharekey, shareid, shareuid: type === DOT_SHARE_TYPE.click ? this.QueryUserInviteUid : '', //点击才要分享id }) this.checkLogin(fun); } /** * adtyp:类型 1banner2激励视频3插屏4格子5原生模板 adstatus: 1请求 2返回 3展示 4点击 5完成 6中断 7失败 banner:1,3,6,7 video:1,5,6,7 interstitial:1 grid:1,3,6,7 custom:1,3,6,7 * @param sharekey * @param adid * @param adtyp * @param adstatus */ adStat(videokey: string, adid: string, adtyp: DOT_AD_TYPE, adstatus: DOT_AD_STATUS) { let fun = () => SDKApi.adStat({ ...this.buildParams(), videokey, adplat: "1", adid, adtyp, adstatus }) this.checkLogin(fun); } private stayInterval private startStay() { this.stayInterval = setInterval(this.stayFun.bind(this), 1000); } private stayFun() { let newUserTime = StorageUtils.I.get("newUserTime") || 0; newUserTime++; StorageUtils.I.set("newUserTime", newUserTime) let newUserDot = StorageUtils.I.get("newUserDot") || 0; if (newUserDot === 1) clearInterval(this.stayInterval); // console.log("newUserTime", newUserTime, newUserDot) if (newUserTime >= 30) { StorageUtils.I.set("newUserDot", 1); this.loadingFinishStay(30); clearInterval(this.stayInterval); } } async loadingFinishStay(times) { let fun = () => { if (DataService.I.RegTime === DateUtils.today) { SDKApi.stay({ ...this.buildParams(), times, }) } } this.checkLogin(fun); } /** * 用户停留30s打点 2021-1-6 废弃 */ stay(times) { // let fun = () => SDKApi.stay({ // ...this.buildParams(), // times, // }) // this.checkLogin(fun); } /** * 关键行为打点 */ behaviors() { let fun = () => SDKApi.behaviors({ ...this.buildParams(), }) this.checkLogin(fun); } checkLogin(fun) { if (this.isLogin) { fun(); } else { this.queue.push(fun) } } /** * 跳转打点 */ jumps(id, adtyp) { let fun = () => SDKApi.jumps({ ...this.buildParams(), adid: 0, adplat: 1, adtyp, typ: 1, times: 1, id }) this.checkLogin(fun); } /** * 曝光打点 */ bannerExposure2(id, adtyp, times) { let fun = () => SDKApi.jumps({ ...this.buildParams(), adid: 0, adplat: 1, adtyp, typ: 2, times, id }) this.checkLogin(fun); } /** * 曝光累计 */ addExposure(type: AdType | number, data: Array) { if (!data) return data.forEach(item => { let { id } = item; if (this.exposureArr.hasOwnProperty(type)) { let d = this.exposureArr[type]; if (d.hasOwnProperty(id)) { this.exposureArr[type][id]++; } else { this.exposureArr[type] = { ...this.exposureArr[type], [id]: 1 } } } else { let obj = { [type]: { [id]: 1 } } this.exposureArr = { ...this.exposureArr, ...obj } } }) // console.log("this.exposureArr", this.exposureArr) } /** * 曝光打点 */ bannerExposure() { if (JSON.stringify(this.exposureArr) === '{}') return; let obj = JSON.parse(JSON.stringify(this.exposureArr)); this.exposureArr = {}; for (let type in obj) { let data = obj[type]; for (let key2 in data) { this.bannerExposure2(key2, type, data[key2]) } } } /** * 用户创角 * @param role_name 角色名称 * @param region 区服 */ role(role_name: string,region:string) { let fun = () => SDKApi.role({ ...this.buildParams(), role_name, region }) this.checkLogin(fun); } /** * 构建登录/弱登录公用参数 */ private buildParams() { let APIVersion = '0.6.0';//固定值 let gameid = GAMEDATA.game_id; let channel = DataService.I.ChannelId; let brand = WxSystem.I.brand; let model = WxSystem.I.model; let version = WxSystem.I.version; let system = WxSystem.I.system; let platform = WxSystem.I.platform; let sdkversion = WxSystem.I.SDKVersion; let scene = DataService.I.Scene + ''; let uid = this.UserId; let env = DataService.I.EnvEnum === 1 ? 'pre' : 'prod'; return { gameid, channel, APIVersion, brand, model, version, system, platform, sdkversion, scene, uid, env }; } private static instance: LogService; static get I(): LogService { return this.instance || (this.instance = new LogService()); } }