LogService.ts 12.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
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<any> = [];

    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<IGameList>) {
        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());
    }
}