import {
  imgServerUrl
} from '../../config/config.js'
import {
  getWxCode,
  hasAuth,
  getUserInfo
} from '../../utils/wx.js'
import {
  updataStorageData
} from '../../utils/storage.js'
import {
  wxLogin,
  saveLogin
} from '../../services/wx.js'
import {
  showToast
} from '../../utils/tips.js'
var app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgServerUrl: imgServerUrl,
    disabled: false,
  },
  onLoad: function() {

  },

  //用户授权
  bindGetUserInfo: function(e) {
    var self = this;
    if (!self.data.disabled) {
      self.setData({
        disabled: true
      })
      //检查登录态
      wx.checkSession({
        success() {
          //session_key 未过期，并且在本生命周期一直有效
          getWxCode().then(code => {
              return wxLogin({
                code: code
              })
            })
            .then(res => {
              console.log("dd", res)
              app.globalData.openId = res.data.openId ? res.data.openId : '';
              app.globalData.sessionKey = res.data.sessionKey ? res.data.sessionKey : '';
            })
            .then(data => {
              if (e.detail.userInfo) {
                return getUserInfo()
              } else {

              }
            })
            .then(res => {
              console.log("ddf", res)
              if (res) {
                wx.setStorageSync("userInfo", res.userInfo);
                saveLogin({
                  encryptedData: encodeURIComponent(res.encryptedData),
                  iv: encodeURIComponent(res.iv),
                  openid: app.globalData.openId,
                  session_key: app.globalData.sessionKey
                }).then(data => {
                  console.log("save", data)
                  wx.setStorageSync("hpuser", data.data.hpUser);
                  wx.setStorageSync("hpMember", data.data.hpMember);
                  wx.setStorageSync("user_id", data.data.hpUser.id);
                  wx.setStorageSync("member_id", data.data.hpMember.id);
                  wx.setStorageSync("user_token", data.data.hpUser.userToken);
                  wx.setStorageSync("phone", data.data.hpUser.phone);
                  // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                  // 所以此处加入 callback 以防止这种情况
                  if (app.userInfoReadyCallback) {
                    app.userInfoReadyCallback(res)
                  }
                  showToast("登录成功");
                  setTimeout(function() {
                    wx.navigateBack({
                      fail:()=>{
                        wx.switchTab({
                          url: '/pages/index/index',
                        })
                      }
                    });
                  }, 500)
                }).catch(data => {
                  console.log("fgh", data)
                })
              }
            })
        },
        fail() {
          // session_key 已经失效，需要重新执行登录流程
          getWxCode().then(code => {
              return wxLogin({
                code: code
              })
            })
            .then(res => {
              console.log("dd", res)
              app.globalData.openId = res.data.openId ? res.data.openId : '';
              app.globalData.sessionKey = res.data.sessionKey ? res.data.sessionKey : '';
            })
            .then(data => {
              if (e.detail.userInfo) {
                return getUserInfo()
              } else {

              }
            })
            .then(res => {
              if (res) {
                wx.setStorageSync("userInfo", res.userInfo);
                saveLogin({
                  encryptedData: encodeURIComponent(res.encryptedData),
                  iv: encodeURIComponent(res.iv),
                  openid: app.globalData.openId,
                  session_key: app.globalData.sessionKey
                }).then(data => {
                  console.log("save", data)
                  wx.setStorageSync("hpuser", data.data.hpUser);
                  wx.setStorageSync("hpMember", data.data.hpMember);
                  wx.setStorageSync("user_id", data.data.hpUser.id);
                  wx.setStorageSync("member_id", data.data.hpMember.id);
                  wx.setStorageSync("user_token", data.data.hpUser.userToken);
                  wx.setStorageSync("phone", data.data.hpUser.phone);
                  // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                  // 所以此处加入 callback 以防止这种情况
                  if (app.userInfoReadyCallback) {
                    app.userInfoReadyCallback(res)
                  }
                  showToast("登录成功");
                  setTimeout(function() {
                    wx.navigateBack({
                      fail: () => {
                        wx.switchTab({
                          url: '/pages/index/index',
                        })
                      }
                    });
                  }, 500)
                }).catch(data => {
                  console.log("fgh", data)
                })
              }
            })
        }
      })
    }
    setTimeout(function () {
      self.setData({
        disabled: false
      })
    }, 1000)
  }

})