// pages/mine/mine.js import UserApi from '../../assets/js/http/userApi' import { encrypt } from '../../assets/js/jsencrypt/rsa' import { wxBind } from '../../assets/js/common' var countdown = 60; // 倒计时秒数 Page({ data: { phone: '', captcha: '', openId: '', 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, codeType: 1 }).then(res => { // 获取成功,倒计时 start if (res.success) { this.countDown() wx.showToast({ title: '发送成功', icon: 'none', duration: 2000 }) } 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 }, // 手机号密码登录页 phonePassword(){ wx.navigateTo({ url: '../phonePassword/phonePassword', }) }, // 微信登录 wxLogin(){ wxBind((res)=>{ let params = { openId: res.data.openid, rsaPublicKey: this.data.rsaPublicKey } UserApi.wxLogin(params).then(res=>{ if(res.success) {// 已经用手机号登录过,此时用微信快捷登录时,把微信的个人信息存进缓存(微信信息在缓存的userInfoTemp) wx.setStorageSync('userInfo', res.result.userInfo) wx.setStorage({ key: 'X-Access-Token', data: res.result.token }) wx.switchTab({ url: '../home/home', }) }else{ // 首次登录不能直接用微信快捷登录 wx.showToast({ title: '暂未授权手机号', duration: 2000, icon: "none", }) } }) }) }, // 登录 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) { wx.setStorage({ key: 'userInfo', data: res.result.userInfo }) if(res.message === "updatePassword") { // 存临时token wx.setStorage({ key: 'token-temp', data: res.result.token }) // 跳转设置密码页 wx.navigateTo({ url: '../setPassword/setPassword', }) }else{ // 存token和userInfo wx.setStorage({ key: 'X-Access-Token', data: res.result.token }) // 绑定微信 this.wxBind() // 跳转首页 wx.switchTab({ url: '../home/home', }) } } else { wx.showToast({ title: res.message, icon: 'error', duration: 2000 }) } }) // 登录失败 } else { wx.showToast({ title: '请输入正确的验证码', icon: 'none', duration: 2000 }) } }, // 忘记密码 updateInfo(){ wx.navigateTo({ url: '../forgetPassword/forgetPassword', }) }, // 绑定微信 wxBind(){ wxBind((res)=>{ let params = { openId: res.data.openid, rsaPublicKey: that.data.rsaPublicKey } // 需要判断是否绑定过微信,再进行绑定请求 UserApi.wxBind(params).then(res=>{}) }) }, /** * 生命周期函数--监听页面显示 */ onShow: function () { UserApi.getRSAPublicKey().then(res => { if (res.success) { this.setData({ rsaPublicKey: res.result }) wx.setStorage({ key: 'rsaPublicKey', data: res.result }) } }) } })