// pages/login/login.js

const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login:1,
    url:'',
    phonenum:'',
    currentTime: 60,
    mimaphone:'',
    mima:'',
    type:'',
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      url: app.globalData.url,
      type:options.type,
      id:options.id
    })
  },
  yanzheng(){
    this.setData({
      login:1
    })
  },
  mima(){
    this.setData({
      login:2
    })
  },
  //输入手机号
  enterphone(e){
    this.setData({
      phonenum:e.detail.value
    })
  },
  //清空手机号
  clearphone(){
    console.log(43823489)
    this.setData({
      phonenum:'',
      phone:''
    })
  },
  
  //验证码登录
  login() {
    let that = this;

    //判断手机验证码
    let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    let phone = that.data.phonenum;
   
    if (phone == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })


    } else if (!reg.test(phone)) {
      wx.showToast({
        title: '请填写正确的手机号',
        icon: 'none'
      })
    } else {

      wx.navigateTo({
        url: '/pages/enteryanzheng/enteryanzheng?phonenum='+that.data.phonenum+'&type='+that.data.type+'&id='+that.data.id,
      })
     
      
    }
  },

  //密码登录输入手机号
  mimaphone(e){
    this.setData({
      mimaphone:e.detail.value
    })
  },
  //输入密码
  shumima(e){
    this.setData({
      mima: e.detail.value
    })
    
  },

  //密码登录
  mimalogin(){
    let that = this;

    //判断手机验证码
    let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    let phone = that.data.mimaphone;
    let mima=that.data.mima

    if (phone == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })


    } else if (!reg.test(phone)) {
      wx.showToast({
        title: '请填写正确的手机号',
        icon: 'none'
      })
    } else if(mima=='') {

      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })

    }else{
      var url ="/user/Profile/login_password"
      var params = {
        mobile: that.data.mimaphone,
        password:that.data.mima
      }

      app.post(url, params).then((res) => {
        console.log(res);
        wx.setStorageSync('token', res.token)
        if (res.is_update==0){
          wx.navigateTo({
            url: '/pages/peopleinfo/peopleinfo',
          })
        }else{
          //type=1  发现首页的分享
          if(that.data.type==1){
            let url=wx.getStorageSync("url")
            wx.navigateTo({
              url: url+'?id='+that.data.id
            })
          } else if (that.data.type == 2){
            let url = wx.getStorageSync("url")
            wx.navigateTo({
              url: url + '?id=' + that.data.id
            })
          }else{
            wx.switchTab({
              url: '/pages/homapage/homepage',
            })
          }

         
        }
       
      }).catch((err) => {
        console.log(err)
        wx.showToast({
          title: err.data.msg,
          icon: "none"
        })
      })
    }
  },

  //忘记密码
  forgetmima(){
    wx.navigateTo({
      url: '/pages/forgetmima/forgetmima',
    })
  },
  clearphone(){
    this.setData({
      mimaphone:''
    })
  },
  clearmima(){
    this.setData({
      mima:''
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})