yas.js 5.96 KB
import udid from './udid';
import Taro from '@tarojs/taro';
import config from '../config';
import rules from '../router/rules';
import { MD5 } from './crypto';
import { parse, stringify } from 'querystringify';
import { getGlobalData } from '../libs/login/login.js'

export default class yas {
    constructor(app) {
        let self = this;

        this.app = app;
        this.pvid = MD5(`${new Date().getTime()}${udid.get()}`).toString();
        this.deviceInfo = {
            os: '', // 系统类型
            dm: '', // 设备型号
            res: '', // 屏幕大小
            osv: '', // 系统版本
            ak: 'yohoufo_mp',
            ch: '',
            udid: udid.get()
        };

        // 获取设备信息
        Taro.getSystemInfo({
            success(res) {
                self.language = res.language;

                if (res.platform === 'devtools') {
                    // self.devEnv = true; //判断开发环境
                }

                Object.assign(self.deviceInfo, {
                    os: res.platform,
                    dm: res.model,
                    res: `${res.screenWidth}*${res.screenHeight}`,
                    osv: res.system,
                });
            }
        });
    }
    uploadData(params) {
        let sid = '';
        let globalData = getGlobalData();

        if (this.app && this.app.globalData) {
          sid = globalData.sid || '';
        }

        return Taro.request({
            url: config.domains.yasHost,
            data: { _mlogs: JSON.stringify(params) },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'x-yoho-sid': MD5(sid).toString(),
            },
        });
    }
    report(event, info = {}) {
        let self = this;

        for (let key in info) {
            if (info[key]) {
                info[key] = decodeURIComponent(info[key]);
            }
        }

        const pageNameMap = {};

        for (let i in rules) {
            if (rules.hasOwnProperty(i) && rules[i].path) {
                pageNameMap[rules[i].path] = i;
            }
        }

        if (event === 'YB_SHARE_RESULT_L') {
            info.PAGE_NAME = pageNameMap['/' + info.PATH];
        }

        let userInfo = info || {};
        let statusInfo = { ln: this.language };

        let user = Taro.getStorageSync('userInfo') || {};

        userInfo.UNION_ID = user.unionid || user.union_id || Taro.getStorageSync('unionid') || '';
        userInfo.APP_ID = config.appid || '';

        if (!userInfo.PV_ID) {
            userInfo.PV_ID = info.PV_ID;
        }

        return new Promise(resolve => {
            Taro.getNetworkType({
                success(res) {
                    switch (res.networkType) {
                        case 'wifi':
                            statusInfo.net = '1';
                            break;
                        case '2g':
                            statusInfo.net = '2';
                            break;
                        case '3g':
                            statusInfo.net = '3';
                            break;
                        case '4g':
                            statusInfo.net = '1';
                            break;
                        default:
                            statusInfo.net = '0';
                            break;
                    }
                },
                complete() {
                  let globalData = getGlobalData();
                  let userData = globalData.userInfo || {};

                  if (!Object.keys(userData).length) {
                    userData = Taro.getStorageSync('userInfo') || {};
                  }

                  let ch = globalData.ch || '';
                  let uid = userData.uid || '';

                  self.deviceInfo.ch = ch;
                  return resolve(self.uploadData({
                      status: statusInfo,
                      device: self.deviceInfo,
                      events: [{
                          param: userInfo,
                          ts: new Date().getTime(),
                          op: event,
                          uid: uid,
                          sid: globalData.sid || ''
                      }]
                  }));
                }
            });
        });
    }
    pageOpenReport(pvid, extra) {
        let pages = getCurrentPages();
        let currentPage = pages[pages.length - 1];
        let path = `/${currentPage.route}`,
            options = currentPage.options || {},
            copyOptions = Object.assign({}, options), // 拷贝options对象,用于获取当前页面参数
            fromPage = options.fromPage || '',
            fromParam = parse(decodeURIComponent(options.fromParam || ''));

        let info = { PV_ID: pvid || this.pvid };

        for (let i in rules) {
            if (rules.hasOwnProperty(i) && rules[i].path === path) {
                Object.assign(info, {
                    PAGE_PATH: path,
                    PAGE_NAME: rules[i].report && rules[i].report.pageName || i,
                    FROM_PAGE_NAME: fromPage && rules[fromPage].report && rules[fromPage].report.pageName || fromPage
                });
                delete copyOptions.fromPage; // 删除拷贝对象的两个from属性
                delete copyOptions.fromParam;

                info.PAGE_PARAM = stringify(copyOptions); // 获取当前页param
                info.FROM_PAGE_PARAM = ''; // decodeURIComponent(options.fromParam || ''); // 获取来源页param
                if (rules[i].report && rules[i].report.paramKey) {
                    info.PAGE_PARAM = decodeURIComponent(options[rules[i].report.paramKey] || '');
                }

                if (fromPage && rules[fromPage].report && rules[fromPage].report.paramKey) {
                    info.FROM_PAGE_PARAM = decodeURIComponent(fromParam[rules[fromPage].report.paramKey] || '');
                }
            }
        }

        this.report('YB_PAGE_OPEN_L', Object.assign(info, extra || {}));
    }
}