bindPhoneNumber.js 7.07 KB
// pages/choosecountry/choosecountry.js
'use strict';
import { API_HOST, SERVICE_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import { getVerifyKey, bindAction, decodePhoneNumber, getUnionID} from '../../utils/login';


let CHOOSED_COUNTRY_ID = 'CHOOSED_COUNTRY_ID'
let CHOOSED_COUNTRY_NAME = 'CHOOSED_COUNTRY_NAME'
let app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    country_name:'中国',
    country_area:'86',
    phoneNum:'',
    verifyBtnText:'获取验证码',
    counting:false,
    verifyCode:'',
    hasUnionID:'',
  },

  onLoad: function (options) {
    this.setData({
      hasUnionID: app.globalData.WXUnion_ID !== null && app.globalData.WXUnion_ID !== '' && app.globalData.WXUnion_ID !== undefined ? true:false,
    })
    new app.WeToast();
  },

  onShow: function () {
    let that = this;
    wx.getStorage({
      key: CHOOSED_COUNTRY_ID,
      success: function (res) {
        if (res.data.length>0) {
          // console.log(res.data)
          that.setData({ country_area: res.data});
        }
      }
    })

    wx.getStorage({
      key: CHOOSED_COUNTRY_NAME,
      success: function (res) {
        if (res.data.length > 0) {
          // console.log(res.data)
          that.setData({ country_name : res.data});
        }
      }
    })  
  },

  getPhoneNumber: function (e) {
    var that = this;


    // console.log(e)
    // console.log(e.detail.errMsg)
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      decodePhoneNumber(e.detail.iv, e.detail.encryptedData, function (result) {
        // console.log('!!!!result', result)
        if (result.code != 200) {
          that.wetoast.toast({
            title: result.message,
            titleClassName: 'wetoast-title',
            duration: 1000
          });
        } else {
          if(result.succeed){
            let prePage = getCurrentPages();
            if (prePage.length > 1 && prePage[prePage.length - 2].bindPhoneNumComplete != undefined) {
              prePage[prePage.length - 2].bindPhoneNumComplete();
            }
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
    } else {
    //   let needBind = true;
    //   that.setData({ needBind });
    //   that.loginAndRegisterTapped(true);
    }
  },

  chooseCountry:function(){
    wx.navigateTo({
      url: '../choosecountry/choosecountry',
    })
  },

  getVerifyCode:function() {
    let that = this
    if (that.data.counting) {
      return;
    }
    that.data.counting = true;

    if (that.data.phoneNum == "" || that.data.phoneNum == undefined || that.data.phoneNum == null) {
      that.wetoast.toast({
        title: "手机号不能为空",
        titleClassName: 'wetoast-title',
        duration: 1500
      });

      that.data.counting = false;
      return;
    }

    getVerifyKey(that.data.phoneNum, that.data.country_area, function (result) {
      let self = that
      // console.log('result',result)
      if (result.message && result.message != '')
        self.wetoast.toast({
          title: result.message,
          titleClassName: 'wetoast-title',
          duration: 1500
        });
      if (result.succeed) {
        let countDownNumb = 60;
        let timer = setInterval(function () {
          countDownNumb--;
          if (countDownNumb < 0) {
            self.setData({
              verifyBtnText: "重新发送",
              counting: false,
            })
            clearInterval(timer);
          } else {
            self.setData({
              verifyBtnText: countDownNumb + "s",
              counting: true,
            })
          }
        }, 1000);
        // if (callBackFun)
        //   callBackFun(result);
      }
      else {
        self.setData({
          counting: false,
        })
        // if (callBackFun)
        //   callBackFun(result);
      }
    });
},

phoneInputChanged:function (e) {
  let that = this
  that.setData({
    phoneNum: e.detail.value
  })

  that.setData({
    disabledClass: that.data.phoneNum && that.data.verifyCode ? "" : "disabled"
  })
},

verifyCodeInputChanged:function (e) {
  let that = this
  that.setData({
    verifyCode: e.detail.value,
  });

  that.setData({
    disabledClass: that.data.phoneNum && that.data.verifyCode ? "" : "disabled"
  })
},

verifyBtnAction:function(){
  let that = this;
  let app = getApp()

  this.setData({
    hasUnionID: app.globalData.WXUnion_ID !== null && app.globalData.WXUnion_ID !== '' && app.globalData.WXUnion_ID !== undefined ? true : false,
  })
  if (wx.getSetting){
    wx.getSetting({
      success: (res) => {
        // console.log("userInfo:", res.authSetting["scope.userInfo"])
        if (res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === false) {
          wx.showModal({
            title: '',
            content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
            confirmText: "去开启",
            confirmColor: "#000000",
            success: function (res) {
              if (res.confirm) {
                wx.openSetting({
                  success: (res) => {
                    // console.log(res.authSetting["scope.userInfo"])
                    if (res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === true) {
                      getUnionID(app.getWechatThirdSession(), function (response) {
                        if (response.succeed === true) {
                          wx.navigateBack({
                            delta: 1,
                          })
                        }
                      })
                    }
                  }
                })
              } else {

              }
            }
          })
        } else {
        }
      }
    })
  } else {
    wx.showModal({
      title: '提示',
      content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
    })
  }
},

bindBtnAction:function (needBind, callBackFun) {
  let that = this
  // console.log('phoneNumber,verifyCode', that.data.phoneNum, that.data.verifyCode)
  if (!that.data.phoneNum) {
    that.wetoast.toast({
      title: '请输入有效手机号',
      titleClassName: 'wetoast-title',
      duration: 1500
    });

    return;
  }

  if (!that.data.verifyCode) {
    that.wetoast.toast({
      title: '请输入有效验证码',
      titleClassName: 'wetoast-title',
      duration: 1500
    });
    return;
  }

  bindAction(that.data.phoneNum, that.data.verifyCode, that.data.country_area, function (result) {
    console.log("result:",result)
    if (result.message)
      that.wetoast.toast({
        title: result.message,
        titleClassName: 'wetoast-title',
        duration: 1500
      });
      
    if (result.succeed) {
      let prePage = getCurrentPages();
      if (prePage.length > 1 && prePage[prePage.length - 2].bindPhoneNumComplete != undefined) {
        prePage[prePage.length - 2].bindPhoneNumComplete();
      }
      wx.navigateBack({
        delta: 1,
      })
    }
  })
}
})