const app = getApp();
Page({
  data: {
    canClick:true,
    codeBtnTxt:"发送验证码"
  },
  onLoad(){
    var userInfo = wx.getStorageSync('userInfo')
    this.setData({
      loginName:userInfo.mobile,
      userId:userInfo.userId
    })
  },
  goSubmit(){
    var that = this
    var phoneNumber = that.data.loginName
    var userId = that.data.userId
    var phonereg = /^[1][0-9]{10}$/;
    let token = wx.getStorageSync('token');
    if (!phonereg.test(phoneNumber)) {
      app.showToptip(that, "worning", "手机号输入有误")
      return;
    }
    var smsCode = that.data.code
    if (!smsCode) {
      app.showToptip(that, "worning", "验证码输入有误")
      return;
    }
    var password = that.data.password
    if (!password) {
      app.showToptip(that, "worning", "密码不能为空")
      return;
    }
    var repassword = that.data.password2
    if (password != repassword) {
      app.showToptip(that, "worning", "两次密码输入不一致")
      return;
    }
    wx.showLoading({
      mask: true
    })
     
    app.requestP({
      url :token? "/user/editPassword" : '/login/editPassword', 
      // needToken:false,
      contentType:"application/json",
      data: {
        phonenumber:phoneNumber,
        repassword:password,
        password,
        smsCode,
        userId:token ? userId : '',
      }
    }).then((res) => {
      app.showToptip(that,"success", res.data.msg)
      wx.redirectTo({
        url: '/pages/login/login?phoneNumber='+phoneNumber+"&password="+password,
      })
    }).catch((res) => {
      app.showToptip(that,"error", res.data.msg)
    });
  },
  setLoginName: function (e) {//手机号输入校验
    var loginName = e.detail.value.replace(/[^\d]/g, "").substr(0,11)
    //验证手机号
    var myreg = /^[1][0-9]{10}$/;
    this.setData({
      loginName,
      canClick : myreg.test(loginName)
    })
  },
  setPwd: function (e) {//密码输入绑定
    var password = e.detail.value
    this.setData({
      password: password
    })
  },
  setPwd2: function (e) {//密码输入绑定
    var password2 = e.detail.value
    this.setData({
      password2: password2
    })
  },
  getValidCode:function(e){
    //验证手机号
    var myreg = /^[1][0-9]{10}$/;
    if (!myreg.test(this.data.loginName)) {
      app.showToptip(this, "worning", "手机号输入有误")
      return;
    }
    var that=this
    wx.showLoading({
      title: '发送中...',
      mask: true
    })
    app.requestP({
      url: "/sms/getSmsCodeByMobile",
      method:'post',
      contentType:"application/json",
      needToken:false,
      data: { 
        mobile: that.data.loginName
      }
    }).then((res) => {
        app.showToptip(that, "success","发送成功")
        that.data.lastSendTime = Date.now()
        that.setData({
          canClick: false
        })
        //定时器触发倒计时，
        var times = 60
        var i = setInterval(function () {
          times--
          if (times == 0) {
            that.setData({
              canClick: true,
              codeBtnTxt: "获取验证码",
            })
            clearInterval(i)
          } else {
            that.setData({
              codeBtnTxt: "重新获取" + times + "s",
            })
          }
        }, 1000)
    }).catch(function(r){
      app.showToptip(that, "error",r.data.msg)
    })
  },
  setCode:function(e){//验证码输入绑定
    this.setData({
      code: e.detail.value.replace(/[^\d]/g, "").substr(0,6)
    })
  },
})