| | |
| | | // pages/wxbind/wxbind.js |
| | | const app = getApp(); |
| | | const { |
| | | get, |
| | | post |
| | | } = require('../../api/request.js'); |
| | | const storage = require('../../utils/storage.js'); |
| | | Page({ |
| | | |
| | | /** |
| | |
| | | */ |
| | | data: { |
| | | mobile: '', |
| | | code: '', |
| | | phoneCode: '', |
| | | codeSent: false, |
| | | countdown: 60, |
| | | isButtonEnabled: false |
| | | }, |
| | | onLoad(options) { |
| | | |
| | | }, |
| | | bindMobileInput(e) { |
| | | this.setData({ |
| | |
| | | |
| | | bindCodeInput(e) { |
| | | this.setData({ |
| | | code: e.detail.value |
| | | }, this.checkButtonState); |
| | | phoneCode: e.detail.value |
| | | }); |
| | | this.checkButtonState(); |
| | | }, |
| | | checkButtonState() { |
| | | const { |
| | | mobile, |
| | | code |
| | | phoneCode |
| | | } = this.data; |
| | | const isButtonEnabled = mobile.length === 11 && code.length == 6; |
| | | const isButtonEnabled = mobile.length === 11 && phoneCode.length == 6; |
| | | this.setData({ |
| | | isButtonEnabled |
| | | isButtonEnabled: isButtonEnabled |
| | | }); |
| | | console.log(isButtonEnabled); |
| | | }, |
| | |
| | | }); |
| | | return; |
| | | } |
| | | |
| | | if (this.data.mobile.length != 11) { |
| | | wx.showToast({ |
| | | title: '请输入完整手机号', |
| | | icon: 'none', |
| | | duration: 2000, |
| | | }); |
| | | return; |
| | | } |
| | | // 在这里处理发送验证码的逻辑,可以调用后台接口实现 |
| | | // 以下是一个简单的示例,仅作参考 |
| | | wx.showToast({ |
| | | title: '验证码已发送', |
| | | icon: 'success', |
| | | duration: 2000, |
| | | }); |
| | | |
| | | this.postCode(); |
| | | this.setData({ |
| | | codeSent: true, |
| | | }); |
| | | |
| | | // 启动倒计时 |
| | | this.startCountdown(); |
| | | }, |
| | |
| | | } |
| | | }, 1000); |
| | | }, |
| | | bind: function () { |
| | | bindSuccess: function () { |
| | | if (!this.data.isButtonEnabled) return; |
| | | wx.showToast({ |
| | | title: '绑定成功', |
| | | icon: 'success' |
| | | }); |
| | | // 跳转到 TabBar 页面 |
| | | wx.switchTab({ |
| | | wx.navigateTo({ |
| | | url: '/pages/home/home' // 这里填写你想要跳转的 TabBar 页面路径 |
| | | }); |
| | | }, |
| | | //获取验证码 |
| | | postCode: function () { |
| | | post('/items', { name: 'New Item' }) |
| | | const params = { |
| | | url: 'wx/client/send_sms?phoneNumber=' + this.data.mobile |
| | | }; |
| | | post(params) |
| | | .then((data) => { |
| | | this.setData({ |
| | | items: [...this.data.items, data] |
| | | wx.showToast({ |
| | | title: '验证码已发送', |
| | | icon: 'success', |
| | | duration: 2000, |
| | | }); |
| | | }) |
| | | .catch((error) => { |
| | | console.error('Failed to add item:', error); |
| | | }); |
| | | }, |
| | | |
| | | bind() { |
| | | this.wsLogin(); |
| | | }, |
| | | wsLogin() { |
| | | wx.login({ |
| | | success: res => { |
| | | if (res.code) { |
| | | console.log('登录成功,获取到的code:', res.code); |
| | | // 发送 res.code 到后台服务器换取 openId, sessionKey, unionId |
| | | this.verify(res.code) |
| | | } else { |
| | | console.log('登录失败!' + res.errMsg); |
| | | } |
| | | } |
| | | }); |
| | | }, |
| | | //用户绑定 |
| | | verify(wxCode) { |
| | | const params = { |
| | | url: 'wx/client/verify', |
| | | data: { |
| | | phoneNumber: this.data.mobile, |
| | | securityCode: this.data.phoneCode, |
| | | code: wxCode |
| | | } |
| | | }; |
| | | post(params) |
| | | .then((data) => { |
| | | getApp().globalData.sessionId = data.content.sessionId |
| | | storage.setItem("sessionId", data.content.sessionId) |
| | | getApp().globalData.clientId = data.content.clientId |
| | | storage.setItem("clientId", data.content.clientId) |
| | | this.bindSuccess(); |
| | | }) |
| | | .catch((error) => { |
| | | wx.showToast({ |
| | | title: error.msg, |
| | | icon: 'error', |
| | | duration: 3000, |
| | | }); |
| | | console.error('Failed to add item:', error); |
| | | }); |
| | | } |
| | | }) |