// pages/register/register.js

const app = getApp()
let time;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yesSend:false,//是否发送验证码
    minter: 60,//发送验证码60秒后才可再次发送
    num:60,
    phone: "",//手机号
    phoneCode: "",//验证码
    yesRegister:false,//防止重复点击注册
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.canIUse('hideHomeButton')) {
      wx.hideHomeButton()
    }
  },

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

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

  },

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

  },

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

  },

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

  },

  yesCodeFn:function(){//验证码接口
    app.wxRequest(app.globalData.httpUrl + 'smscode', { user_phone: this.data.phone }, e => {
      console.log(e)
      if (e.code == 200) {
        wx.showToast({
          title: e.message,
          icon: 'success',
          duration: 500
        })
      }
    }, this)
  },
  sendTouch:function(){//发送验证码
    if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
        wx.showToast({
          title: '手机号错误',
          icon: 'none',
          duration: 500
        })
      return;
    } 
    this.setData({
      yesSend: true,
    })
    if (this.data.yesSend) {
      time = setInterval(this.setIntervalFn,1000)
    }

    if (app.globalData.openId) {
      this.yesCodeFn();
    } else {
      app.globalData.openidSuccessFuc = this.yesCodeFn;
    }
    
  },
  setIntervalFn:function(){//定时器
    if (this.data.num > 0) {
      this.data.num--;
      if (this.data.num == 0) {
        this.setData({
          yesSend: false,
        })
        if (!this.data.yesSend){
          this.setData({
            num: 60,
            minter: 60,
          })
        }
        clearInterval(time);
      }
    }
    this.setData({
      minter: this.data.num
    })
    console.log(this.data.minter)
  },
  registerBtn:function(){//点击按钮注册
    if (app.globalData.openId) {
      this.registerFn();
    } else {
      app.globalData.openidSuccessFuc = this.registerFn;
    }
  },
  registerFn:function(){//注册接口
    if (this.data.yesRegister){
      return;
    }
    this.data.yesRegister = true;
    app.wxRequest(app.globalData.httpUrl + 'register', { user_phone: this.data.phone, code_num: this.data.phoneCode }, e => {
      console.log(e)
      if (e.code == 200) {
        clearInterval(time);
        app.globalData.userInfo = e.data.user_phone
        this.data.yesRegister = false;
        this.setData({
          minter: 60,
          num: 60
        })
        wx.showToast({
          title: '注册成功',
          icon: 'success',
          duration: 500
        })
        setTimeout(function () {
          if (app.globalData.present_id){
            wx.reLaunch({
              url: '/pages/receiveTicket/receiveTicket?shareId=' + app.globalData.present_id + "&number=" + app.globalData.number
            })
          } else {
            wx.switchTab({
              url: '../index/index'
            })
          }
        }, 1000)
      }else{
        wx.showToast({
          title: e.message,
          duration:500
        })
      }
    }, this,"POST")
  },
  getPhone: function (e) {//获取手机号
    console.log(e.detail.value);
    this.setData({
      phone: e.detail.value
    })
  },
  getCode: function (e) {//获取验证码
    console.log(e.detail.value);
    this.setData({
      phoneCode: e.detail.value
    })
  },
})