/*
 * @Author: your name
 * @Date: 2021-06-23 22:15:11
 * @LastEditTime: 2021-06-24 21:01:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \Mina_B_T\miniprogram\app.ts
 */
// app.ts

App<IAppOption>({
  globalData: {
    captchaTicketExpire: null,
    captchaResult: null,
    phone_zz: /1[0-9]{10}/,
    webview: null,
    version: '1.2.5',
    platformId: 1
  },
  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch() {
    if (!wx.getStorageSync('hasClear')) {
      wx.clearStorage({
        success: () => {
          wx.setStorageSync('hasClear', true)
        },
      })
    }
  },

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow(options: any) {    
    if (!this.globalData.captchaTicketExpire) this.globalData.captchaTicketExpire = {};

    if (options.referrerInfo.appId === 'wx5a3a7366fd07e119') {
      const result = options.referrerInfo.extraData;
      if (result.ret === 0) {
        const ticket = result.ticket;
        if (!this.globalData.captchaTicketExpire[ticket]) {
          this.globalData.captchaResult = result;
          this.globalData.captchaTicketExpire[ticket] = true;
        }
      }
    }
    if (wx.getUpdateManager) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(res => {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(() => {
            wx.showModal({
              title: '更新提示',
              content: '新版本上线了，请点击确定按钮更新！',
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(() => {
            wx.showModal({
              title: '更新提示',
              content: '新版本已上线，请删除当前小程序，重新搜索进入',
              showCancel: false,
            })
          })
        }
      })
    }
  },

  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide() {
  },

  /**
   * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError(msg) {
    console.error(msg)
  },
})