var validator = require('../../../../utils/validator.js') var _request = require('../../../../utils/request.js') Page({ data: { tel: 0, yzm: '', countdown: 0, pwd: '', repwd: '' }, onLoad: function (options) { this.getTel() this.startCountdown() }, bindYzm (e) { this.setData({ yzm: e.detail.value }) }, bindPwd (e) { this.setData({ pwd: e.detail.value }) }, bindRepwd (e) { this.setData({ repwd: e.detail.value }) }, getYzm () { if (validator.telphone(this.data.tel)) { this.requireYzm() } else { wx.showToast({ title: '手机号码格式不正确', icon: 'none', duration: 2000 }) } }, requireYzm () { var that = this var url = 'v1/code/send' var params = { tel: this.data.tel, valid_type: 'reset_trade_pwd' } var success = function (res) { wx.setStorageSync('sms_request_time', Date.now()) that.startCountdown() } _request.$post(url, params, success) }, 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) } } else { } }, getTel () { var that = this var url = 'v1/user/info' var params = { } var success = function (res) { var data = res.data.user.tel that.setData({ tel: data }) } _request.$get(url, params, success) }, requireUpdate () { var that = this var url = 'v1/user/trade_pwd' var params = { tel: this.data.tel, code: this.data.yzm, pwd: this.data.pwd, confirmed_pwd: this.data.repwd } var success = function (res) { wx.navigateBack({ delta: 1 }) } _request.$put(url, params, success) }, setUpdate () { var { isOk, msg } = this.validate() if (isOk) { this.requireUpdate() } 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 = '验证码不能为空' } else if (!validator.required(this.data.pwd)) { msg = '密码不能为空' } else if (!validator.required(this.data.repwd)) { msg = '确认密码不能为空' } else if (!validator.minLen(this.data.pwd, 6)) { msg = '密码的长度是6-20位' } else if (!validator.maxLen(this.data.pwd, 20)) { msg = '密码的长度是6-20位' } else if (!validator.minLen(this.data.repwd, 6)) { msg = '确认密码的长度是6-20位' } else if (!validator.maxLen(this.data.repwd, 20)) { msg = '确认密码的长度是6-20位' } else if (!validator.confirmed(this.data.pwd, this.data.repwd)) { msg = '两次密码不一致' } return { isOk: !msg, msg } }, onShareAppMessage: function (val) { return _request.share({ sc: 'xcx_user_safe' }) } })