//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力 
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({ 
      success: res => {
        console.log(res)
        let tempCode = res.code;
        wx.setStorageSync('tempCode', res.code)
        wx.getUserInfo({
          lang: 'zh_CN',
          success: ret => {
            this.globalData.userInfo = ret.userInfo
            let params = { 
              code: tempCode,
              nickName: ret.userInfo.nickName,
              avatarUrl: ret.userInfo.avatarUrl,
              gender: ret.userInfo.gender
            } 
            let codeurl = '/Portal/Index/getOpenid';
            this.post(codeurl, params).then((res) => {
              //正确返回结果
              wx.setStorageSync('openid', res.openid)
              if(res.res== '0'){  
                wx.setStorageSync('register', false)                        
              }else{ 
                wx.setStorageSync('register', true)  
              }
              wx.hideNavigationBarLoading();
              wx.hideLoading();
            }).catch((errMsg) => {
              console.log(errMsg);//错误提示信息
              wx.hideNavigationBarLoading();
              wx.hideLoading();
            });
          } 
        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
              wx.getUserInfo({
                success: res => {
                  let params = {
                    code: tempCode,
                    userInfo: res.userInfo
                  }
                  let codeurl = '/Portal/Index/getOpenid';
                  this.post(codeurl, params).then((res) => {
                    //正确返回结果
                    wx.setStorageSync('openid', res.openid)
                    if(res.res== '0'){  
                      wx.setStorageSync('register', false)                        
                    }else{ 
                      wx.setStorageSync('register', true)  
                    } 
                    wx.hideNavigationBarLoading();
                    wx.hideLoading();
                  }).catch((errMsg) => { 
                    console.log(errMsg);//错误提示信息
                    wx.hideNavigationBarLoading();
                    wx.hideLoading();
                  });
                  // 可以将 res 发送给后台解码出 unionId
                  this.globalData.userInfo = res.userInfo
                  // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回、
                  // 所以此处加入 callback 以防止这种情况
                  if (this.userInfoReadyCallback) {
                    this.userInfoReadyCallback(res)
                  }
                }
              })  
            } else {
              console.log(JSON.stringify(res));
            }
          }
        })
      }
    })
  },
  post: function (url, data) {
    /** 
  * 自定义post函数,返回Promise
  * +-------------------
  * @param {String}      url 接口网址
  * @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
  * +-------------------
  * @return {Promise}    promise 返回promise供后续操作
  */
    wx.showLoading({
      title: '加载中',
    })
    wx.showNavigationBarLoading()
    var promise = new Promise((resolve, reject) => {
      //init
      let that = this;
      let postData = data; 
      let baseUrl = 'https://fdjc.wx.bronet.cn/index.php';
      // postData.signature = that.makeSign(postData);
      //网络请求  
      wx.request({
        url: baseUrl + url,
        data: postData,
        method: 'POST', 
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {//返回取得的数据
          if (res.data.code == 0) { 
            resolve(res.data.data);
          } else {//返回错误提示信息
            reject(res.data.msg);
          }
          setTimeout(function () {
            wx.hideLoading()
            wx.hideNavigationBarLoading()
          }, 1500)

        },
        error: function (e) {
          console.log(e)
          reject('网络出错');
          wx.hideLoading()
          wx.hideNavigationBarLoading()
        }
      })
    });
    return promise;
  },
  getopenid(obj){
    
  },
  globalData: {
    userInfo: null,
    hasUserInfo: null,
    register: false
  }
})