const app = getApp();
Page({
  data: {
    // loginName:"13153864796",
    // loginPwd:"123456",
    isVivo:false,
    loginNav:"pwd",
    canClick:false,
    codeBtnTxt:"发送验证码",
    code:'',
  },
  onLoad: function (options) {
    var that = this
    //获取手机型号，vivo手机安全键盘bug
    wx.getSystemInfo({
      success:function(res){
        if(res.brand=="vivo"){
         that.data.isVivo = true; 
        }
      }
    });
    wx.removeStorageSync("token")
    wx.removeStorageSync("userInfo")
    if(options.phoneNumber){
      this.setData({
        loginName:options.phoneNumber,
        loginPwd:options.password
      })
    }
    this.getCaptchaImage();
  },
  setNav:function(e){//登录方式切换
    this.setData({
      loginNav:e.currentTarget.dataset.type
    })
    this.getCaptchaImage();
  },
  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)
    })
  },
  setLoginPwd: function (e) {//密码输入绑定
    var loginPwd = e.detail.value
    this.setData({
      loginPwd: loginPwd
    })
    if(this.data.isVivo){//vivo密码键盘bug，超过1秒没输入收起键盘
      if(this.timer)clearTimeout(this.timer)
      this.timer = setTimeout(function(){
        wx.hideKeyboard();
      },1000)
    }
  },
  getValidCode:function(e){
    // if(!this.data.canClick){
    //   return
    // }
    //验证手机号
    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",
      needToken:false,
      contentType:' application/json',
      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)
    })
  },
  setCaptChaImage:function(e){
    this.setData({
      tCaptChaCode:e.detail.value
    })
  },
  loginIn: function (e) {//登录
    const that=this
    let url = ""
    let obj = {}
    //验证手机号
    var myreg = /^[1][0-9]{10}$/;
    if (!myreg.test(this.data.loginName)) {
      app.showToptip(this, "worning", "手机号输入有误")
      return;
    }
    if(that.data.loginNav == "pwd"){
      url = "/login"
      //验证密码必填
      if (!that.data.loginPwd) {
        app.showToptip(this, "worning", "请输入密码")
        return;
      }
      obj= {
        "password": that.data.loginPwd,
        "username": that.data.loginName,
        "uuid":that.data.uuid,
        "code":that.data.tCaptChaCode
      }
    }
    if(that.data.loginNav == "code"){
      url = "/login/mobileLogin"
      //验证码必填
      if (!that.data.code) {
        app.showToptip(this, "worning", "请输入验证码")
        return;
      }
      obj= {
        code: that.data.code,
        mobile: that.data.loginName,
      }
    }
    
    wx.showLoading({
      title: '正在登录...',
      mask: true
    })
    app.requestP({
      url : url, 
      needToken:false,
      data: obj,
      contentType:' application/json'
    }).then((res) => {
        wx.setStorageSync("token",res.data.token)
        app.requestP({
          method:"post",
          url: "/user/getUserInfo",
        }).then(function(r){
          var data = r.data;
          wx.setStorageSync('userInfo',r.data.data)
          wx.switchTab({
            url: '/pages/index/index',
          })
        })
    }).catch((err) => {
      that.getCaptchaImage();
      app.showToptip(that,"error", err.data.msg)
    });
  },
  getCaptchaImage(){
    var that = this;
    app.requestP({
      url: "/captchaImage",
      needToken:false,
      method:'get',
      // contentType:' application/json',
    }).then(res=>{
      that.setData({
        uuid:res.data.uuid,
        img:'data:image/gif;base64,'+res.data.img
      })
    })
  }
})