app.js 5.51 KB
//app.js
import * as Sentry from './utils/lib/sentry-miniapp'
const accountInfo = wx.getAccountInfoSync()
//测试和生产才开发sentry
const env = ['trial', 'release']
env.includes(accountInfo.miniProgram.envVersion) &&
  Sentry.init({
    // dsn: 'http://ebcf31c417cc4ce185782f046e4a87a2@sentry.glsx.com.cn/5'
    dsn: 'https://e440585f391e46278c3c72ddabb1d17f@sentry.glsx.com.cn/31'
  })
import router from './router/index.js'
import util from './utils/util.js'
import './utils/Track'
import { ssoApi, wechatApi } from './api/api'
import { trackData } from './utils/data'
import eventbus from './utils/evenbus'
// 实例化对象存储
import { Storage } from './utils/cache/storage'
const $Storage = new Storage()

App({
  onLaunch: function (options) {
    console.log(options)
    /**挂载到wx对象 */
    this.autoUpdate()
    wx.$storage = $Storage /** 挂载全局缓存方法 */
    wx.$eventbus = eventbus
    wx.$util = util
    wx.$router = router
    wx.$imgUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/'
    wx.$appId = wx.getAccountInfoSync().miniProgram.appId
    const systemInfo = wx.$storage.get('aicard_system_info')
    if (!systemInfo)
      wx.getSystemInfo({
        success(res) {
          wx.$storage.set('aicard_system_info', res)
        }
      })
    if (options.path != 'pages/tab/index/index') {
      this.login()
    }
    // wx.showMeasure = false
    wx.showMeasure = !!(options.query && options.query.scene)
  },
  login(returnResponse) {
    let _this = this
    let openId = null
    let wechatInfo = null
    return new Promise((resolve, reject) => {
      wx.login({
        success({ code }) {
          ssoApi
            .jsCodeLogin({
              code,
              appId: wx.$appId
            })
            .then(res => {
              wechatInfo = res.data
              wx.$storage.set('aicard_info', wechatInfo)
              wx.$authStatus = res.code != 0
              openId = wechatInfo.openId
              console.log(wx.$trackData)
              wx.showMeasure = !!wx.$trackData.scene
              if (wx.$trackData.scene) {
                // 初次进入小程序默认埋首页
                _this.getScene(wx.$trackData.scene).then(res => {
                  wx.$trackData.merchant = res.merchantId
                  wx.$trackData.cardId = res.cardId
                  console.log('wx.$trackData.cardId', wx.$trackData.cardId)
                  wx.$storage.set('scene', {
                    merchant: res.merchantId,
                    cardId: res.cardId
                  })
                  wx.$Track.sendTrack(trackData.COMPANY, wechatInfo)
                })
              // }  else if (wx.$trackData.isShare) {
              //   wx.$storage.set('scene', wx.$trackData)
              // } else if (!wx.$trackData.detailShare && wx.$trackData.cardId) {
              //   // 名片分享在这里设置埋点
              //   wx.$storage.set('scene', wx.$trackData)
              //   wx.$Track.sendTrack(trackData.COMPANY, wechatInfo)
              } else if (wx.$trackData.shareType) {
                // 其他的分享进来的页面走这里,在进页面的onshow中需对wx.$trackData先赋值
                const shareType = wx.$trackData.shareType
                switch(shareType) {
                  case 'SHARE_HOME':
                    // 名片分享在这里设置埋点
                    wx.$storage.set('scene', wx.$trackData)
                    wx.$Track.sendTrack(trackData.COMPANY, wechatInfo)
                    break
                  case 'SHARE_D': 
                    wx.$storage.set('scene', wx.$trackData)
                    break
                  default:
                    wx.$storage.set('scene', wx.$trackData)
                }
              }
              ssoApi
                .getUserInfo({
                  openId
                })
                .then(result => {
                  if (returnResponse) {
                    resolve(result)
                  } else {
                    if (result.code == 0) {
                      util.setUserInfo(result.data)
                    } else {
                      util.showToast(result.message || '网络异常')
                    }
                    resolve(result)
                  }
                })
            })
            .catch(() => {
              wx.$authStatus = true
              reject()
            })
        }
      })
    })
  },
  getScene(scene) {
    return new Promise((resolve, reject) => {
      wechatApi
        .getSceneById({
          id: decodeURIComponent(scene).split('=')[1]
        })
        .then(({ data }) => {
          let op = {}
          data.scene.split('&').map(item => {
            const [key, val] = item.split('=')
            op[key] = val
          })
          resolve(op)
        })
    })
  },
  /**
   * 下载小程序新版本并重启应用
   */
  autoUpdate: function () {
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
  },
  globalData: {}
})