//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    var _this = this
    wx.getSystemInfo({
      success: function (response) {
        if (response.model.toUpperCase().indexOf('IPHONE') >= 0 && response.model.toUpperCase().indexOf('X') >= 0) {
          _this.globalData.isIpx = true;
        }
      }
    })
    wx.request({
      url: 'https://m.zuihuibi.cn/api/authorize/obtainToken',
      method: 'POST',
      data: {
        ticket: 'miniPro'
      },
      success: function (response) {
        wx.setStorageSync('X-Authorization', response.data.data.token);
        // 登录
        wx.login({
          success: function (res) {
            if (res.code) {
              //发起网络请求
              wx.request({
                url: 'https://m.zuihuibi.cn/api/jscode2session',
                method: 'POST',
                header: { 'X-Authorization': wx.getStorageSync('X-Authorization') },
                data: {
                  appid: 'wx53a601e27aaf7897',
                  secret: 'f61707a62e46fee78ef46155330cdf45',
                  js_code: res.code
                },
                success: function (res) {
                  wx.setStorageSync('sessionKey', res.data.data.session_key);
                  wx.setStorageSync('openid', res.data.data.openid);
                }
              })
            } else {
              console.log('登录失败！' + res.errMsg)
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    isIpx: false
  }
})