// pages/mine/wxml/Connect/Connect.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataObj: {},
    phonename: "",
    verificationname: "",
    setInformation: true,
    countdown: 30,
    sendType: true
  },

  phone: function (e) {
    var _this = this;
    _this.setData({
      phonename: e.detail.value
    })
  },
  verification: function (e) {
    var _this = this;
    _this.setData({
      verificationname: e.detail.value
    })
  },

  //发送验证码

  obtain: function (e) {
    var _this = this;
    var phonenames = this.data.phonename;
    var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    if (reg.test(phonenames)) {

      wx.getStorage({
        key: 'key',
        success: function (cookies) {
          console.log(cookies.data.ydw_token)
          wx.request({
            url: app.globalData.prodUrl + 'web-pm/wx/pm/send_mobile_wx',
            data: {
              mobile: phonenames
            },
            header: {
              'ydw-token': cookies.data.ydw_token
            },
            success: function (data) {
              if (data.data.success){
                wx.showToast({
                  title: '发送成功',
                  icon: 'none',
                  duration: 2000
                });
                _this.setData({
                  sesstionid: data.data.sessionid
                })
                _this.setData({
                  setInformation: false
                })
                var num = 30;
                var time = setInterval(function () {
                  num -= 1;
                  _this.setData({
                    countdown: num
                  })
                  if (num === 0) {
                    clearInterval(time)
                    _this.setData({
                      setInformation: true
                    })
                  }
                }, 1000)
             }else{
                wx.showToast({
                  title: '手机已被注册',
                  icon: 'none',
                  duration: 2000
                })

             }
             
            },
            Error: function (data) {
              wx.showToast({
                title: '发送失败，请重新发送验证码',
                icon: 'none',
                duration: 2000
              })
            }
          });
        }
      });
    } else {
      wx.showToast({
        title: '请正确输入手机号',
        icon: 'none',
        duration: 2000
      })
      // clearInterval(time)
      _this.setData({
        setInformation: true
      });
    }
  },
//绑定手机号
  signin: function () {
    var _this = this;
    var verificationname = this.data.verificationname;
    var regs = /^\d{4}$/;
    var phonenames = this.data.phonename;
    var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    if (reg.test(phonenames) && regs.test(verificationname)) {
      wx.getStorage({
        key: 'key',
        success: function (cookies) {
          wx.request({
            url: app.globalData.prodUrl + 'web-pm/wx/pm/check_mobile_and_code_wx',
            data: {
              mobile: phonenames,
              code: verificationname
            },
            header: {
              'ydw-token': cookies.data.ydw_token,
              'Cookie': 'JSESSIONID=' + _this.data.sesstionid
            },
            success: function (data) {
              if (data.data.success) {
                wx.setStorageSync('persion', { 'persion': data.data.persion });
                wx.navigateBack({
                  delta:1,
                  success: function () {
                    var pages = getCurrentPages();
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    pages[0].onLoad();
                  }
                }); 
              }else{
                wx.showToast({
                  title: '验证码错误',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          });
        }
      });
    } else {
      wx.showToast({
        title: '请正确输入验证码或手机号',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})