app.js 5.98 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, busassistantApi } 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) {
    /**挂载到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
    this.clearFileSystem()
    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 = !!(options.query && options.query.scene)
    wx.onMemoryWarning(function () {
    })
  },
  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
              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
                  wx.$storage.set('scene', {
                    merchant: res.merchantId,
                    cardId: res.cardId
                  })
                  _this.getUserInfo(openId, returnResponse).then(result => {
                    wx.$Track.sendTrack(trackData.HOME, wechatInfo)
                    resolve(result)
                  })
                })
              } else {
                // 其他的分享进来的页面走这里,在进页面的onshow中需对wx.$trackData先赋值
                const shareType = wx.$trackData.shareType
                _this.getUserInfo(openId, returnResponse).then(result => {
                  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:
                      if (wx.$trackData.cardId) {
                        wx.$storage.set('scene', wx.$trackData)
                      }
                  }
                  resolve(result)
                })
              }
            })
            .catch(() => {
              wx.$authStatus = true
              reject()
            })
        }
      })
    })
  },
  getUserInfo(openId, returnResponse) {
    return new Promise((resolve) => {
      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)
          }
        })
    })
  },
  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)
        })
    })
  },
  // 获取公共数据
  getBaseData() {
    console.log("method")
     return new Promise((resolve, reject) => {
      busassistantApi.getDiscoverList().then(res => {
        console.log(res, '555');
        resolve(res.data)
      })
     })
  },

  /**
   * 下载小程序新版本并重启应用
   */
  autoUpdate: function () {
    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 () {
      // 新版本下载失败
    })
  },
  // 清内存
  clearFileSystem: function() {
    const fn = wx.getFileSystemManager()
    fn.readdir({
      dirPath: wx.env.USER_DATA_PATH,
      success({
        files
      }) {
        files.length && files.map(e => {
          fn.unlink({
            filePath: `${wx.env.USER_DATA_PATH}/${e}`
          })
        })
      }
    })
  },
  globalData: {},
  onPageNotFound(res) {
    setTimeout(() => {
      wx.$router.go('/pages/tab/index/index', res.query)
    }, 1000)
  },
})