app.js 1.33 KB
//app.js
import router from './router/index.js'
import util from './utils/util.js'
import './utils/Track'
import {
  ssoApi
} from './api/api'
App({
  onLaunch: function () {
    /**挂载到wx对象 */
    wx.$util = util
    wx.$router = router
    const systemInfo = wx.getStorageSync('srcm_shopmall_system_info')
    if (!systemInfo) wx.getSystemInfo({
      success(res) {
        util.setStorageSync('srcm_shopmall_system_info', res)
      }
    })
    this.login()
  },
  login(returnResponse) {
    let _this = this
    return new Promise((resolve, reject) => {
      wx.login({
        success({
          code
        }) {
          ssoApi.jsCodeLogin({
            code,
            appId: _this.globalData.appId
          }).then(res => {
            wx.$authStatus = res.code != 0
            if (returnResponse) {
              resolve(res)
            } else {
              if (res.code != -1) {
                if (res.code == 0) {
                  util.setUserInfo(res.data)
                  util.setSessionId(res.data.sessionId)
                } else {
                  util.showToast(res.message || '网络异常')
                }
              }
              resolve(res)
            }
          }).catch(() => {
            wx.$authStatus = true
            reject()
          })
        }
      })
    })
  },
  globalData: {

  }
})