const _api = require('../../utils/api.js')
const request = require('../../utils/js.js');
const api = _api.api;
const app = getApp()
const globalData = app.globalData
Page({
  data: {
    tel:''
  },

  /**  生命周期函数--监听页面加载 **/
  onLoad: function (options) {
    var that = this;
    that.setKey(that)
  },

 /********************  微信授权  *****************************/
  AuthMap() { app.AuthMap(this) },
  //授权登录
  Authorize() { app.Authorize(this) },
  bindGetUserInfo(e){
    var that = this
    app.login(that,e,function(res){
      
    })
  },
   /******************** 微信授权  *****************************/


  onPullDownRefresh(e){
    
  },

  /** 页面上拉触底事件 **/
  onReachBottom: function () {
    var that = this;
  },

  /** 用户点击右上角分享 **/
  onShareAppMessage: function () {
    return {
      title: globalData.share.title,
      path: globalData.share.path,
      imageUrl: globalData.share.imageUrl // 图片 URL
    }
  },
  getUserProfile(_that, sessionKey,sucFun) {
    var that=this
    wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success:function(res2){
        // console.log('获取成功: ',res2)
          //调用登录接口
          var code = ''
          request.login().then(function (d) {
            code = d.code
            console.log(code) 
          }).then(function (e) {
            that.ajaxget(api.code + '?code=' + code, {}, {}, function (res) {

              if (res.code == 1) {
                // that.saveUserInfo(_that, res.data.session_key, sucFun)
                that.ajaxpost(api.login, {}, {
                  'encryptedData': res2.encryptedData,
                  'iv': res2.iv,
                  'sessionKey': res.data.session_key,
                  'openid': res.data.openid
                }, function (e) {
                  console.log(e)
                  if (e.code == 1) {
                    console.log('成功啦')
                    wx.setStorageSync('user', e.data)
                    wx.setStorageSync('sessionKey', res.data.session_key)
                    wx.setStorageSync('token', e.data.token)
                    that.setData({ Authorize: true, user: e.data, token: e.data.token })
                    // sucFun(e.data)
                  }
          
                }, function (e) {
          
                }, function () { })
              } else {
                that.showToast(res.msg)
                console.log('失败')
              }
            }, function (res) { }, function (res) { })
          });
        },
        fail:function(err){
          console.log("获取失败: ",err)
        }
    })

  },
  setKey(_that){
    var that=this
    _that.setData({
      token: wx.getStorageSync('token')
      , user: wx.getStorageSync('user')
      , Authorize: false
    })
  },


  //post请求-token
  ajaxget: function (url, header, data, successFunc, errFunc) {
    let that = this;
    var _errFunc = function (msg) {
      if (!!errMsg) {
        wx.showToast({
          title: msg || errMsg,
          icon: 'none'
        })
      }
    };
    let _data = data || {};//数据类型
    let token = wx.getStorageSync('token');//授权后的用户唯一码（openid）
    let _header = {//请求头
      'content-type': 'application/x-www-form-urlencoded',
      'token': token,
    };
    wx.request({
      url: url,
      data: _data,
      method: 'get',
      header: _header,
      success(res) {
        if (res) {
          res = res.data;
          successFunc(res);
        }
        if (!res) {
          _errFunc(res)
          return;
        }
      },
      fail() {
        _errFunc(res);
      }
    })
  },
  //post请求-token
  ajaxpost: function (url, header, data, successFunc, errFunc) {
    let that = this;
    var _errFunc = function (msg) {
      if (!!errMsg) {
        wx.showToast({
          title: msg || errMsg,
          icon: 'none'
        })
      }
    };
    let _data = data || {};//数据类型
    let token = wx.getStorageSync('token');//授权后的用户唯一码（openid）
    let _header = {//请求头
      'content-type': 'application/x-www-form-urlencoded',
      'token': token,
      lat: wx.getStorageSync('location').lat,
      lng: wx.getStorageSync('location').lng
    };
    wx.request({
      url: url,
      data: _data,
      method: 'post',
      header: _header,
      success(res) {
        if (res) {
          res = res.data;
          successFunc(res);
        }
        if (!res) {
          _errFunc(res)
          return;
        }
      },
      fail() {
        _errFunc(res);
      }
    })
  },
  
  getPhoneNumber(res){
    console.log(res)
    let that = this
    let _data = {
      encryptedData:res.detail.encryptedData,
      iv:res.detail.iv,
      sessionKey:wx.getStorageSync('sessionKey')
    }
    that.ajaxpost(api.getPhone, {}, _data, function (e) {
      console.log(e)
      if (e.code == 1) {
        console.log('成功啦')
        that.setData({
          tel:e.data.phoneNumber
        })
      }

    })
    
  },


   /**  生命周期函数--监听页面初次渲染完成 **/
  onReady: function () {

  },

  /** 生命周期函数--监听页面隐藏 **/
  onHide: function () {

  },

  /** 生命周期函数--监听页面卸载 **/
  onUnload: function () {

  }
})
