// app.ts
App({
  globalDate: {
    global: 1,
    userLocationAuth: false,
    cameraAuth: false,
    extraData: null
  },
  // 根据不同产品展示不同的页面背景
  page_bg: '',
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.ctx = wx.createCameraContext();

    // 登录
    wx.login({
      success(res) {
        var openid = res.code
        console.log('openid', openid)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.setStorage({
          key: 'openid',
          data: openid
        })
        wx.navigateTo({
          url: '../miniprogram/pages/upload/upload'
        })
      }
    })
  },

  onShow(options) {
    // wx.clearStorageSync(); // 清空本地缓存数据
    console.log('options', options)

    // 设置不同产品时AI认证不同背景图片（默认的，灰色的）
    this.page_bg = ''

    // options.referrerInfo.extraData =  {
    //     "uid": "\"gAAAAABl3YfhldYhFyvH_EHb-_CPvbWIffRsxPwS1d_4lC_CxCq-CiLQtqGPEbLmXR--ZPjcIYp5mttnRTpppD6DghuymUheuw==\"",
    //     "uuid": "F1C0DF8F",
    //     "os": "davis",
    //     "cpid": "EH013527418551201",
    //     "company": "fenggu"
    //   }
    if (options.referrerInfo.extraData) {
      console.log('进来了')
      let extraData = options?.referrerInfo?.extraData || {};
      let strData = extraData

      try {
        JSON.parse(extraData)
      } catch (error) {
        strData = JSON.stringify(extraData)
      }
      // console.log('到这里extraData',extraData)
      wx.setStorageSync('extraData', strData)
      wx.setStorageSync('labeljson', JSON.parse(strData)?.cpid_jm || JSON.parse(strData)?.cpid || '')

      // wx.setStorageSync('fenggu', JSON.parse(strData)?.company || '')

      // 启动摄像头预览
      // this.ctx.startPreview();
      // console.log('labelInfoUrl.success', labelInfoUrl.success)
      // {
      //   "uid": "\"gAAAAABl3YfhldYhFyvH_EHb-_CPvbWIffRsxPwS1d_4lC_CxCq-CiLQtqGPEbLmXR--ZPjcIYp5mttnRTpppD6DghuymUheuw==\"",
      //   "uuid": "F1C0DF8F",
      //   "os": "davis",
      //   "cpid": "EH013527418551201",
      //   "company": "fenggu"
      // }
    } else {

      wx.setStorageSync('extraData', JSON.stringify({}))
      // wx.setStorageSync('fenggu', '')
      // wx.setStorage({
      //   key: 'extraData',
      //   data: extraData,
      // })
    }
  },
})