app.js 6.12 KB
// app.js
import router from './router/index.js'
import util from './utils/util.js'
import {
  ssoApi,
  commonApi,
  shopmallApi
} from './api/api'
import './utils/Track'
import eventbus from './utils/evenbus'
import userApi from './api/userApi'
App({
  onLaunch(options) {
    /**挂载到wx对象 */
    wx.$authStatus = false
    wx.$util = util
    wx.$router = router
    // wx.$imgUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/cgacar/'
    wx.$imgUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram-test/cgacar/'
    wx.$eventbus = eventbus
    this.globalData.appId = wx.getAccountInfoSync().miniProgram.appId
    const systemInfo = wx.getStorageSync('srcm_shopmall_system_info')
    if (!systemInfo) wx.getSystemInfo({
      success(res) {
        util.setStorageSync('srcm_shopmall_system_info', res)
      }
    })
  },
  /**页面不存在时触发 */
  onPageNotFound(e) {
    wx.$router.goHome()
  },
  getUpdateManager() {
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
  },
  onShow(options) {
    this.getUpdateManager()
    let query = Object.keys(options.query)
    if (query.length) query.map(e => {
      let params = decodeURIComponent(e).split('&')
      params.map(i => {
        let [key, val] = i.split('=')
        if (!val) val = options.query[key]
        this.setPageParmas({
          [key]: val
        })
      })
    })
    else {
      if (this.autoLogin) return
      this.login()
    }
  },
  setPageParmas(query) {
    if (query.merchantId) util.setStorageSync('srcm_shopmall_merchant_id', query.merchantId)
    if (query.customerId) util.setStorageSync('srcm_shopmall_customer_id', query.customerId)
    let storeCode = query.storeCode || util.filterUrlParam(decodeURIComponent(query.scene))['storeCode']
    if (query.scene) {
      let scene = decodeURIComponent(query.scene)
      let [key, val] = scene.split('=')
      if (key == 'storeCode') {
        this.getGHMerchant(val)
        return
      }
      shopmallApi.getLiveActivityId({
        id: val
      }).then(({
        code,
        data,
      }) => {
        if (code == 0) {
          let op = {}
          data.scene.split('&').map(item => {
            const [key, val] = item.split("=")
            op[key] = val
          })
          if (op.merchantId) util.setStorageSync('srcm_shopmall_merchant_id', op.merchantId)
          if (op.openId) this.openId = op.openId
          if (op.storeCode) this.getGHMerchant(op.storeCode)
          if (op.packageType==1&&op.id) util.setStorageSync('packageId', +op.id)
        }
      })
      return
    }
    if (storeCode) {
      this.getGHMerchant(storeCode)
      return
    }
    if (query.merchantId) {
      util.setStorageSync('srcm_shopmall_merchant_id', query.merchantId)
      this.login()
      return
    }
  },
  getGHMerchant(storeCode, callBack) {
    util.setStorageSync('srcm_shopmall_store_code', storeCode)
    commonApi.getBySapCode({
      sapCode: storeCode
    }).then(({
      code,
      data
    }) => {
      if (code == '0') util.setStorageSync('srcm_shopmall_merchant_id', data.merchantNo)
      if (this.openId) {
        this.loginByOpenId(this.openId, data)
        return
      }
      this.login().then(res => {
        if (callBack) callBack(res)
      })
    })
  },
  login(returnResponse) {
    return new Promise(async (resolve, reject) => {
      try {
        const res = await userApi.login()
        wx.$isGroup = res.data.group
        wx.$authStatus = res.code != 0
        if (res && res.data) {
          this.globalData.openId = res.data.openId
          if (res.data.sapCode) util.setStorageSync('srcm_shopmall_store_code', res.data.sapCode)
        }
        if (returnResponse) {
          resolve(res)
        } else {
          if (res.data) wx.setStorageSync('merchant_info', {
            merchantId: res.data.merchantId,
            merchantName: res.data.merchantName,
            sapCode: res.data.sapCode || '',
          })
          if (res.code != -1) {
            if (res.code == 0) {
              util.setUserInfo(res.data)
              util.setSessionId(res.data.sessionId)
              let customerId = util.getStorageSync('srcm_shopmall_customer_id')
              let merchantId = util.getStorageSync('srcm_shopmall_merchant_id')
              if (customerId) {
                ssoApi.updataCustomerPhone({
                  customerId,
                  merchantId
                }).then(() => {
                  wx.removeStorageSync('srcm_shopmall_customer_id')
                })
              }
              // 登录完加载权益会员信息接口(目前只针对权益商品页面)
              util.customerbehavior() // sop埋点
              eventbus.emit('EQUITYGOODS', true)
              wx.removeStorageSync('isShowPreModal')
            } else {
              util.showToast(res.message || '网络异常')
            }
          }
          resolve(res)
        }
      } catch (error) {
        wx.$authStatus = true
        reject()
      }
    })
  },
  // 通过openId静默登录
  loginByOpenId(openId, merchants) {
    ssoApi.loginByOpenId({
      openId
    }).then(res => {
      res.merchantId = merchants.merchantId
      res.merchantName = merchants.merchantName
      this.autoLogin = true
      util.setUserInfo(res)
      util.setSessionId(res.sessionId)
      util.setStorageSync('showRevicesModal', true)
    })
  },
  globalData: {
    latesParam: {
        appearance: 1,
        pageSize: 0,
        pageCount: 3,
    },
    loaded: false,
    commodityList: []
    // appId: '',
    // merchantId: '44189923' // 44184606  44189923
  },
  onHide() {
    util.customerbehavior(2)
  }
})