const { api_mobile_prefix, api_user_verification, api_mobile_login } = require('../../utils/api.js'); const { net } = require('../../utils/net.js'); const util = require('../../utils/util.js'); Page({ data: { loadingFlag: false, indexMobile: 0, mobilePrefixList: [], btnDisabled: true, btnCodeDisable: false, codeText: '获取验证码', mobile: '', code: '', timer: null }, onLoad: function (options) { this.getMobileArrHandle() }, onUnload: function () { let { timer } = this.data; if (timer) { clearTimeout(timer); timer = null; } }, bindTelHandle(e) { let mobile = e.detail.value; this.setData({ mobile: mobile }); let { code } = this.data; if (util.validator.checkMobile(mobile) && util.validator.checkCode(code)) { this.setData({ btnDisabled: false }); } else { this.setData({ btnDisabled: true }); } }, codeChange(e) { let code = e.detail.value; this.setData({ code: code }); let { mobile } = this.data; if (util.validator.checkMobile(mobile) && util.validator.checkCode(code)) { this.setData({ btnDisabled: false }); } else { this.setData({ btnDisabled: true }); } }, getMobileArrHandle() { this.setData({ loadingFlag: true }) let params = {}; params.url = api_mobile_prefix; net.req(params, true).then((res) => { this.setData({ loadingFlag: false }) let { data } = res let prefixArr = []; if (data && data.list && Array.isArray(data.list) && data.list.length > 0) { prefixArr = data.list prefixArr.forEach(ele => { ele.customize = `${ele.name}(+${ele.mobilePrefix})` }) this.setData({ mobilePrefixList: prefixArr }, () => { for (let i = 0; i < prefixArr.length; i++) { let tmp = prefixArr[i]; if (["86", "086", "+86", "+086"].indexOf(tmp.mobilePrefix) !== -1) { this.setData({ indexMobile: i }) break; } } }) } }, (e) => { this.setData({ loadingFlag: false }) wx.showToast({ title: e.msg, icon: 'none' }) }); }, bindPickerChange(e) { this.setData({ indexMobile: e.detail.value }) }, checkMobile() { let { mobile } = this.data; if (!util.validator.checkMobile(mobile)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 2000 }); return false; } this.getCodeHandle(); }, getCodeHandle() { let { mobile, mobilePrefixList, indexMobile } = this.data; let prefix = mobilePrefixList[indexMobile].id; let params = {}; params.data = { hooid: 'hoolipartner', // 应用id, hooli客户端: hoolihome, partner: hoolipartner, 后台: platform senceId: 1, // 发送短信的场景id 1登录/三方绑定/绑定手机号 2设置密码 3找回密码 5填写申请表发送短信验证码 10绑定支付卡 mobile: mobile, mobilePrefixId: prefix } params.url = api_user_verification; net.req(params, true).then((res) => { wx.showToast({ title: '发送短信验证码成功', icon: 'none' }) this.countDown(); }, (e) => { this.setData({ btnCodeDisable: false }); wx.showToast({ title: (e && e.msg) || '发送短信验证码失败', icon: 'none' }) }); this.setData({ btnCodeDisable: true }); }, countDown: function () { /*短信验证码倒计时*/ let timeCount = 60; let loop = () => { if (timeCount > 0) { this.setData({ codeText: `重新获取(${timeCount}s)` }); timeCount--; this.data.timer = setTimeout(loop, 1000); } else { this.setData({ codeText: `重新获取`, btnCodeDisable: false }); if (this.data.timer) { clearTimeout(this.data.timer); this.data.timer = null; } } }; loop(); }, gotoLoginAction: function () { let { mobilePrefixList, indexMobile, mobile, code } = this.data; let prefix = mobilePrefixList[indexMobile].id; let mobilePrefix = mobilePrefixList[indexMobile].mobilePrefix; if (!util.validator.checkMobile(mobile)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none' }) return false; } if (!util.validator.checkCode(code)) { wx.showToast({ title: '请输入正确的短信验证码', icon: 'none' }) return false; } let params = {}; params.data = { hooid: 'hoolipartner', // 应用id, hooli客户端: hoolihome, partner: hoolipartner, 后台: platform mobile: mobile, mobilePrefixId: prefix, verifyCode: code, mobilePrefix: mobilePrefix, } params.url = api_mobile_login; net.req(params, true).then((res) => { this.setData({ btnDisabled: false }); if (res) { let { token, userInfo } = res.data; wx.setStorageSync('userInfo', { ...userInfo, token }); wx.reLaunch({ url: '/pages/home/home' }); } }, (e) => { this.setData({ btnDisabled: false }); wx.showModal({ title: '提示', content: (e && e.msg) || '抱歉,您暂无登录权限,合作请咨询400-898-6659', showCancel: false }); }); this.setData({ btnDisabled: true }); } })