//app.js
import fetch from '/utils/http'
const $api = require('/config/api.js').$api

App({
  onLaunch: function() {
    let _self = this
    wx.login({
      success(res) {
        if (res.code) {
          // 发起网络请求
          fetch({
            url: $api.user.getCode.url + '?code=' + res.code,
            method: $api.user.getCode.method
          }).then((res) => {
            _self.globalData.UserInfo = res.data
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.UserInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    wx.getSystemInfo({
      success(res) {
        if (res.model.indexOf('iPhone') !== -1 && res.system.indexOf('iOS') !== -1 && res.screenHeight >= 812) { // iphoneX及以上机型适配
          _self.globalData.ScreenBottomHeight = 30
        }
      }
    })

    fetch({
      url: $api.dict.list.url,
      method: $api.dict.list.method,
    }).then((data) => {
      _self.globalData.Dict = data.data
    })
  },
  globalData: {
    UserInfo: {
      customerId: 1,
      customerName: '',
      name: '',
      phone: '',
      idNum: '',
      remark: '',
      openid: '1',
      avatar: '',
    },
    ScreenBottomHeight: 0,
    Dict: []
  }
})