//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // this.getUserInfo()
    var that = this;
  },
  // 获取openid
  getUserInfo: function (cb, u_openid) {
    // console.log(u_openid,1)
    let that = this,
        userInfo = wx.getStorageSync('userInfo');
    that.globalData.u_openid = u_openid;
    // 更新用户信息
    if (userInfo) {
      typeof cb == "function" && cb(userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: (res) => {
          console.log(res,"调用登录接口")
          wx.request({
            url: that.globalData.hostname + 'User/get_openid',
            data: {
              business_id: that.globalData.business_id,
              code: res.code
            },
            header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
            method: 'POST',
            dataType: 'JSON',
            success: function (res) {
              res.data = JSON.parse(res.data)
                console.log(res.data)
              if (res.data.status === 1) {
                let openId = res.data.openid
                that.globalData.openId = openId
              
                if(that.callbackOpenid){
                  that.callbackOpenid(res.data)
                }
                wx.setStorage({
                  key: 'openId',
                  data: openId,
                  success: () => {
                    if (!that.globalData.userInfo) {
                      wx.getUserInfo({      
                        withCredentials: false,
                        success: (res) => {
                          console.log(res,"userinfo")
                          let { userInfo } = res
                          that.globalData.userInfo = userInfo
                          wx.setStorageSync('userInfo', userInfo)
                          let { nickName, gender, province, country, city, avatarUrl } = userInfo
                          wx.request({
                            url: that.globalData.hostname + 'User/save_user_info',
                            data: {
                              business_id: that.globalData.business_id,
                              openid: that.globalData.openId,
                              nickname: nickName,
                              avatarurl: avatarUrl,
                              gender,
                              province,
                              country,
                              city,
                              u_openid: that.globalData.u_openid,
                            },
                            header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
                            method: 'POST',
                            dataType: 'JSON',
                            success: function (res) {
                              let { users_id } = JSON.parse(res.data)
                              that.globalData.users_id = users_id
                              wx.setStorageSync('users_id', users_id)
                              wx.getStorageSync('userInfo', userInfo)
                            },
                            fail: function (res) { },
                            complete: function (res) { },
                          })
                        },
                        fail: (res) => {
                          wx.redirectTo({
                            url: '../authorize/authorize',
                          })
                          wx.setStorageSync('userInfo', false)
                          // that.accessInfo()
                        },
                        complete: (res) => {

                        }
                      })
                    }
                  },
                  fail: () => { },
                  complete: () => { }
                })
               
              }
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        },
        fail: (error) => { },
        complete: (res) => { }
      })
    }
  },
  accessInfo() {
    wx.showModal({
      title: '提示',
      content: '请授权获取用户信息以获得完整体验 !',
      confirmColor: "#d31944",
      showCancel: false,
      success: (res) => {
        wx.openSetting({
          success: (res) => {
            if (!res.authSetting['scope.userInfo']) {
              this.accessInfo()
            } else {
              this.getUserInfo()
            }
          }
        })
      }
    })
  }
  ,
  
  globalData: {
    hostname: 'https://jisu.shenmikj.com/canyin2/Api/', 
    business_id: 112,
    page:1,
    openid: '',
    u_openid: '',//分享者
    s_openid: '',//被分享者
    userid: '',
    share_title:'PageModulesFNB',
    take_out:true
  }
})
