// packageUser/pages/user/bind/bind.js var validator = require('../../../../utils/validator.js') var _request = require('../../../../utils/request.js') var _handle = require('../../../../utils/handle.js') Page({ /** * 页面的初始数据 */ data: { tel: 0, yzm: '', countdown: 0 }, getTel (val) { console.log(val) if (val.detail.encryptedData) { this.bindWxTel(val.detail) } }, bindWxTel (res) { var str = JSON.stringify({ encryptedData: res.encryptedData, iv: res.iv }) var that = this var url = 'v1/wxuser/one_click/binding/phonumer' var params = { userinfo: str } var success = function (res) { wx.navigateTo({ url: '/packageUser/pages/user/bindsuccess/bindsuccess' }) } _request.$post(url, params, success) }, bindPhone (e) { this.setData({ tel: e.detail.value }) }, bindYzm (e) { this.setData({ yzm: e.detail.value }) }, getYzm (e) { _handle.setFormId(e) if (validator.telphone(this.data.tel)) { this.requireYzm() } else { wx.showToast({ title: '手机号码格式不正确', icon: 'none', duration: 2000 }) } }, startCountdown: function () { var smsRequestTime = wx.getStorageSync('sms_request_time') if (smsRequestTime) { var count = parseInt((60000 - (new Date() - smsRequestTime)) / 1000) this.setData({ countdown: count }) if (this.data.countdown > 0) { var timer = setInterval(() => { var countdown = this.data.countdown - 1 this.setData({ countdown: countdown }) if (this.data.countdown <= 0) { clearInterval(timer) } }, 1000) // clearInterval(this.interval) } } else { } }, requireYzm () { var that = this var url = 'v1/code/send' var params = { tel: this.data.tel, valid_type: 'binding' } var success = function (res) { wx.setStorageSync('sms_request_time', Date.now()) that.startCountdown() } _request.$post(url, params, success) }, requireBindTel () { var that = this var url = 'v1/wxuser/binding/tel' var params = { tel: this.data.tel, code: this.data.yzm } var success = function (res) { wx.navigateTo({ url: '/packageUser/pages/user/bindsuccess/bindsuccess' }) } _request.$post(url, params, success) }, setBind (e) { _handle.setFormId(e) var { isOk, msg } = this.validate() if (isOk) { this.requireBindTel() } else { wx.showToast({ title: msg, icon: 'none', duration: 2000 }) } }, validate () { var msg if (!validator.required(this.data.tel)) { msg = '手机不能为空' } else if (!validator.telphone(this.data.tel)) { msg = '手机号码格式不正确' } else if (!validator.required(this.data.yzm)) { msg = '验证码不能为空' } return { isOk: !msg, msg } }, otherbind () { var that = this var url = 'v1/wxuser/one_click/binding/tel' var params = { } var success = function (res) { wx.navigateTo({ url: '/packageUser/pages/user/bindsuccess/bindsuccess' }) } _request.$post(url, params, success) }, onShareAppMessage: function (val) { return _request.share({ sc: 'xcx_user_bind' }) } })