// pages/index/index.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';
import * as STORAGE from '../../../utils/storage'
const APP = getApp();
Page({
    data: {
        isIPhoneX:false,
        privacyCheck:true,  //用户协议
        showDialog:false
    },
    onLoad: function (options) {
        this.setData({
            isIPhoneX:UTIL.isIPhoneX()
        })
       
        this.automaticLogin()
        //this.getUserInfo()
       //this.getPhoneNumber()
    },
    
    //自动登录
    automaticLogin(){
        let automatic = STORAGE.getToken();
        let getOpenId = STORAGE.getOpenId();
        if(automatic == '' && getOpenId ==''){
            console.log(1);
            UTIL.getCOdeFromWX({
                complate: (code) => {
                    let sendData = {
                        code: code
                    }
                    UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
                        success: (res) => {
                            console.log(res);
                            if (res.code == API.SUCCESS_CODE) {
                                wx.navigateTo({
                                    url: '/pages/index/index',
                                })
                            } else{
                                this.getPhoneNumber()
                            }
                        }
                    })
                }
            });
        }else{
            // wx.navigateTo({
            //     url: '/pages/index/index',
            // })
        }
    },
    //用户隐私协议选项
    checkboxChange: function(res) {
        let checkStatus = false;
        if(res.detail.value.length!=0){
            checkStatus = true;
        }else{
            checkStatus = false;
        }
        this.setData({
            privacyCheck:checkStatus
        })
    },
      /* 获取用户信息*/
      getUserInfo(){
        UTIL.httpRequest(API.URL_GET_GETINFO, {method:'GET'}, {
          success: (res) => {
            if (res.code == API.SUCCESS_CODE) {
              this.setData({userInfoObj:res.user})
            }
          }
        })
      },
    //微信一键登录授权
    getPhoneNumber: function(res) {
        let that = this;
        let {
            detail
        } = res;
        if (!detail.encryptedData) {
            //允许授权
            APP.showToast("未获取到手机号码，注册失败！");
            return;
        }else if(this.data.privacyCheck == false){
            APP.showToast("请阅读并同意用户协议和隐私政策！");
            return;
        }
        let sendData = {
            sessionKey:STORAGE.getSessionKey(),
            iv:detail.iv,
            encryptedData:detail.encryptedData
        }
        UTIL.httpRequestNoneDetal(API.URL_POST_DECRYPTEDWXDATA, sendData, "POST", {
            success: (res) => {
              console.log(res)
              if (res.code == API.SUCCESS_CODE) {
                  let phoneNumber = res.data.phoneNumber;
                  //微信手机号码绑定
                  that.wxUserBand(phoneNumber)
        //         UTIL.showToastNoneIcon("数据共：" + res._data.length + "条");
              } else {
                  //待删
                UTIL.showToastNoneIcon(res.msg)
              }
            },
            fail: (res) => {
                console.log("fail");
              UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
            }
        })
        // UTIL.httpRequest(API.URL_POST_DECRYPTEDWXDATA, sendData,{
        //     success: (res) => {
        //         console.log(res);
        // //       if (res.code == API.SUCCESS_CODE) {
        // //           let phoneNumber = res.data.phoneNumber;
        // //           //微信手机号码绑定
        // //           that.wxUserBand(phoneNumber)
        // // //         UTIL.showToastNoneIcon("数据共：" + res._data.length + "条");
        // //       } else {
        // //           //待删
               
        // //         UTIL.showToastNoneIcon(res.msg)
        // //       }
        //     },
        //     fail: (res) => {
        //         console.log("fail");
        //       UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
        //     }
        //  });
    },
    wxUserBand(phone){
        let sendData = {
            openId:STORAGE.getOpenId(),
            phonenumber:phone
        }
        console.log(STORAGE.getOpenId());
        UTIL.httpRequestNoneDetal(API.URL_POST_USERBAND, sendData,"POST",{
            success: (res) => {
                if (res.code == API.SUCCESS_CODE) {
                    wx.navigateTo({
                        url: '/pages/index/index',
                    })
                }else{
                    UTIL.showToastNoneIcon(res.msg)
                }
            },
            fail: (res) => {
                UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
              }
           });
        // 
    },
    //打开协议窗口
    openDialog(){
      this.setData({showDialog:true})
    },
    agree(){
      this.setData({
        privacyCheck:true
      })
    }
      
})