Blame view

app/app.js 7.32 KB
htoooth authored
1 2 3
import wx from './utils/wx';
import udid from './common/udid';
import event from './common/event';
htoooth authored
4 5 6
import {
  verify
} from './common/api';
htoooth authored
7
import Promise from './vendors/es6-promise';
htoooth authored
8 9 10 11 12 13 14
import {
  wechatAuthLogin,
  verifySessionKey
} from './common/login';
import {
  MD5
} from './vendors/crypto';
邱骏 authored
15
import config from './common/config';
htoooth authored
16
import Yas from './common/yas';
邱骏 authored
17
import './router/index'; // 新增的router
htoooth authored
18 19 20
import {
  stringify
} from './vendors/query-stringify';
htoooth authored
21 22 23 24 25

let yas;

// app.js
App({
邱骏 authored
26 27 28 29 30 31
  globalData: {
    userInfo: {},
    unionID: '',
    sessionKey: '',
    thirdSession: '',
    udid: '',
htoooth authored
32 33
    unionType: '',
    systemInfo: {}
邱骏 authored
34 35 36 37 38 39
  },
  reportData: {
    awakeReported: false
  },
  onLaunch: function(options) {
    this.globalData.udid = udid.get(); // 生成 UDID
htoooth authored
40
邱骏 authored
41
    verify.gen(); // 此处返回是是 Promise,需要调用接口的业务,最好在 then 里边执行
htoooth authored
42 43 44 45 46 47 48 49 50 51 52

    let sysInfo = wx.getSystemInfoSync();

    if (!sysInfo.screenHeight) {
      sysInfo.screenHeight = sysInfo.windowHeight;
    }
    if (!sysInfo.screenWidth) {
      sysInfo.screenWidth = sysInfo.windowWidth;
    }
    this.globalData.systemInfo = sysInfo;
邱骏 authored
53 54 55 56 57 58 59 60 61 62 63
    this.globalData.unionType = options.query.union_type || options.scene;
    let timestamp = new Date().getTime() + '';

    this.globalData.sid = MD5(timestamp);
    yas = new Yas(this);
    yas.report('YB_LAUNCH_APP');
  },
  onShow: function(options) {
    udid.get(); // 生成 UDID
    verify.gen(); // 此处返回是是 Promise,需要调用接口的业务,最好在 then 里边执行
邱骏 authored
64
    // wx.getLocation({}); // 获取位置信息
邱骏 authored
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
    wx.checkSession().then(() => { // 微信登录未过期
      this.getWechatThirdSession();
      this.getUserInfo();
      this.getUnionID();
      this.doLogin();
    }).catch(() => { // 微信登录过期
      wx.setStorage({
        key: 'thirdSession',
        data: ''
      });
      wx.setStorage({
        key: 'userInfo',
        data: {}
      });

      wx.setStorage({
        key: 'unionID',
        data: ''
      });
      this.setUserInfo({});
      this.setSessionKey('');

      this.doLogin();
    });

    // yas 登录成功回调上报
    event.on('yas-login-type-report', params => {
      yas.report('YB_MY_LOGIN', params);
    });

    // yas  注册成功后上报
    event.on('yas-user-register-success', params => {
      yas.report('YB_REGISTER_SUCCESS', params);
    });


    yas.report('YB_ENTER_FOREGROUND'); // 系统-程序切换置前台上报
    if (this.reportData.awakeReported === false) {
      let path = options.path || ' ';

      if (Object.keys(options.query).length) {
        path = `${path}?${stringify(options.query)}`;
      }
htoooth authored
109 110 111
      yas.report('YB_AWAKE_MP', {
        PAGE_PATH: path
      }); // 唤起事件,即每次启动后获取到原始的推广路径时上报该事件,以对推广渠道数据进行监测;
邱骏 authored
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

      this.reportData.awakeReported = true;
    }
  },
  onHide: function() {
    yas.report('YB_ENTER_BACKGROUND'); // 系统 - 程序切换置后台上报
  },
  doLogin: function() {
    // 默认自动登录,退出不走自动登录
    if (!this._getSync('disableAutoLogin')) {
      this.getSessionKey();
      return Promise.resolve(this.wechatAuthLogin());
    }
  },
  _getSync: function(key) {
    try {
      return wx.getStorageSync(key);
    } catch (e) {
      console.log(`wx.getStorageSync get ${key} failed.`);
      return {};
    }
  },
  _setSync: function(key, value) {
    try {
      wx.setStorageSync(key, value);
    } catch (e) {
      console.log(`wx.setStorageSync set ${key} failed.`);
    }
  },
  _removeSync: function(key) {
    try {
      wx.removeStorageSync(key);
    } catch (e) {
      console.log(`wx.removeStorageSync del ${key} failed.`);
    }
  },
  getWechatThirdSession: function() {
    this.globalData.thirdSession = this._getSync('thirdSession');
    return this.globalData.thirdSession;
  },
  setWechatThirdSession: function(session) {
    this.globalData.thirdSession = session;
    wx.setStorage({
      key: 'thirdSession',
      data: this.globalData.thirdSession
    });
  },
  getUid: function() {
    return this.globalData.userInfo.uid || 0;
  },
  getUserInfo: function() {
    this.globalData.userInfo = this._getSync('userInfo');
    return this.globalData.userInfo;
  },
  getUnionID: function() {
    this.globalData.unionID = this._getSync('unionID');
    return this.globalData.unionID;
  },
  getSessionKey: function() {
    this.globalData.sessionKey = this._getSync('sessionKey');
    return this.globalData.sessionKey;
  },
  getOpenID: function() {
    let openid;

    if (this.globalData.openID) {
      return this.globalData.openID;
    }
htoooth authored
180
邱骏 authored
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
    openid = wx.getStorageSync('openID');
    this.globalData.openID = openid;
    return openid;
  },
  setOpenID: function(openID) {
    if (openID) {
      this.globalData.openID = openID;
      wx.setStorage({
        key: 'openID',
        data: openID
      });
    }
  },
  setUnionID: function(unionID) {
    this.globalData.unionID = unionID;
    wx.setStorage({
      key: 'unionID',
      data: unionID
    });
    event.emit('wx-union-id-update');
  },
  setUserInfo: function(user) {
    this.globalData.userInfo = user;
    wx.setStorage({
      key: 'userInfo',
      data: this.globalData.userInfo
    });
  },
  setSessionKey: function(sessionKey) {
    this.globalData.sessionKey = sessionKey;
    this._setSync('sessionKey', sessionKey);
  },
  resumeUserInfo: function() {
    this.globalData.userInfo = this._getSync('userInfo');
  },
  resumeSessionKey: function() {
    this.globalData.sessionKey = this._getSync('sessionKey');
  },
  wechatAuthLogin: function() {
    if (!this.globalData.userInfo.uid || !this.globalData.sessionKey) {
      return wechatAuthLogin().then(res => {
        if (res.code === 10003) { // 微信号已绑定手机号
          this.setUserInfo(res.data);
          this.setSessionKey(res.data.sessionKey);
          event.emit('user-login-success');
htoooth authored
226 227
        }
邱骏 authored
228 229 230
        if (res.code === 10004) { // 微信号未绑定手机号
          // 自动登录未绑定静默处理、不强制绑定
        }
htoooth authored
231
      }).catch(() => {});
htoooth authored
232
    }
邱骏 authored
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

    return verifySessionKey().then(ves => {
      if (ves.code !== 200) { // sessionKey已过期

        // 清理原存储的用户信息
        this.clearUserSession();
        return wechatAuthLogin();
      } else {
        this.resumeUserInfo();
        this.resumeSessionKey();
        event.emit('user-login-success');
        return Promise.resolve({});
      }
    }).then(res => {
      if (res.code === 10003) { // 微信号已绑定手机号
        this.setUserInfo(res.data);
        this.setSessionKey(res.data.sessionKey);
        event.emit('user-login-success');
      }

      if (res.code === 10004) { // 微信号未绑定手机号
        // 自动登录未绑定静默处理、不强制绑定
      }
      event.emit('enable-tap-login');
    }).catch(() => {
      event.emit('enable-tap-login');
    });
  },
  clearUserSession: function() {
    this.setUserInfo({});
    this.setSessionKey('');
邱骏 authored
264
    this.setUnionID('');
邱骏 authored
265 266 267 268 269 270 271 272 273
  },
  getReportUid() {
    return this.globalData.userInfo.uid || this._getSync('userInfo').uid || '';
  },
  getAppId() {
    return config.appid;
  },
  getPvid() {
    return MD5(`${new Date().getTime()}${udid.get()}`).toString();
htoooth authored
274 275 276
  },
  getSystemInfo() {
    return this.globalData.systemInfo;
邱骏 authored
277 278
  },
  getMiniappType() {
邱骏 authored
279
    return config.mini_app_type;
邱骏 authored
280
  }
htoooth authored
281
});