// pages/mine/mine.js import UserApi from '../../assets/js/http/userApi' import { encrypt } from '../../assets/js/jsencrypt/rsa' var countdown = 60; // 倒计时秒数 Page({ data: { phone: '', captcha: '', count: 60, rsaPublicKey: '', showGetCaptcha: true }, bindInputPhone(e) { this.setData({ phone: e.detail.value }) }, // 获取验证码 getVerCode() { countdown = 60 if (/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(this.data.phone)) { UserApi.sendCode({ phone: this.data.phone }).then(res => { // 获取成功,倒计时 start if (res.success) { this.countDown() } else { wx.showToast({ title: res.message, icon: 'none', duration: 2000 }) } }) } else { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 2000 }) } }, // 倒计时 countDown(){ if (countdown === 0) { this.setData({ showGetCaptcha: true }) countdown = 60; return; } else { this.setData({ showGetCaptcha: false }) countdown--; this.setData({ count: countdown }) } setTimeout(() => { this.countDown() }, 1000) // 倒计时 end }, // 登录 formSubmit(e) { if (e.detail.value.phone && e.detail.value.captcha) { let params = { username: encrypt(this.data.rsaPublicKey,e.detail.value.phone), // 手机号加密 captcha: e.detail.value.captcha, rsaPublicKey: this.data.rsaPublicKey } // 请求短信登录start UserApi.codeLogin(params).then(res => { if (res.success) { // 存临时token和userInfo wx.setStorage({ key: 'token-temp', data: res.result.token }) wx.setStorage({ key: 'userInfo', data: res.result.userInfo }) if(res.message === "updatePassword") { // 跳转设置密码页 wx.navigateTo({ url: '../setPassword/setPassword', }) }else{ // 存token和userInfo wx.setStorage({ key: 'X-Access-Token', data: res.result.token }) // 跳转首页 wx.switchTab({ url: '../home/home', }) } } else { wx.showToast({ title: res.message, icon: 'error', duration: 2000 }) } }) // 登录失败 } else { wx.showToast({ title: '请输入正确的验证码', icon: 'none', duration: 2000 }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { UserApi.getRSAPublicKey().then(res => { if (res.success) { this.setData({ rsaPublicKey: res.result }) wx.setStorage({ key: 'rsaPublicKey', data: res.result }) } }) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { } })