1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
| // pages/wxbind/wxbind.js
| const app = getApp();
| const {
| get,
| post
| } = require('../../api/request.js');
| const storage = require('../../utils/storage.js');
| Page({
|
| /**
| * 页面的初始数据
| */
| data: {
| mobile: '',
| phoneCode: '',
| codeSent: false,
| countdown: 60,
| isButtonEnabled: false,
| showErrorDialog: false,
| errorData: "",
| errorDialogTitle: "错误提示",
| confirmBtn: {
| content: '确认'
| }
| },
| onLoad(options) {
|
| },
| bindMobileInput(e) {
| this.setData({
| mobile: e.detail.value
| }, this.checkButtonState);
| },
|
| bindCodeInput(e) {
| this.setData({
| phoneCode: e.detail.value
| });
| this.checkButtonState();
| },
| checkButtonState() {
| const {
| mobile,
| phoneCode
| } = this.data;
| const isButtonEnabled = mobile.length === 11 && phoneCode.length == 6;
| this.setData({
| isButtonEnabled: isButtonEnabled
| });
| console.log(isButtonEnabled);
| },
| sendCode: function () {
| if (!this.data.mobile) {
| wx.showToast({
| title: '请输入手机号',
| icon: 'none',
| duration: 2000,
| });
| return;
| }
| if (this.data.mobile.length != 11) {
| wx.showToast({
| title: '请输入完整手机号',
| icon: 'none',
| duration: 2000,
| });
| return;
| }
| // 在这里处理发送验证码的逻辑,可以调用后台接口实现
| this.postCode();
|
| },
| //倒计时
| startCountdown: function () {
| let that = this;
| let timer = setInterval(function () {
| let countdown = that.data.countdown - 1;
| that.setData({
| countdown: countdown,
| });
|
| if (countdown <= 0) {
| clearInterval(timer);
| that.setData({
| codeSent: false,
| countdown: 60,
| });
| }
| }, 1000);
| },
| bindSuccess: function () {
| if (!this.data.isButtonEnabled) return;
| wx.showToast({
| title: '绑定成功',
| icon: 'success'
| });
| // 跳转到 TabBar 页面
| wx.redirectTo({
| url: '/pages/home/home' // 这里填写你想要跳转的 TabBar 页面路径
| });
| },
| //获取验证码
| postCode: function () {
| const params = {
| url: 'wx/client/send_sms?phoneNumber=' + this.data.mobile
| };
| post(params)
| .then((data) => {
| wx.showToast({
| title: '验证码已发送',
| icon: 'success',
| duration: 2000,
| });
| this.setData({
| codeSent: true,
| });
| // 启动倒计时
| this.startCountdown();
| })
| .catch((error) => {
| this.setData({
| showErrorDialog: true,
| errorData: error.msg
| })
| 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 = String(data.content.sessionId)
| storage.setItem("sessionId", String(data.content.sessionId))
| getApp().globalData.clientId = String(data.content.clientId)
| storage.setItem("clientId", String(data.content.clientId))
| this.bindSuccess();
| })
| .catch((error) => {
| wx.showToast({
| title: error.msg,
| icon: 'error',
| duration: 3000,
| });
| console.error('Failed to add item:', error);
| });
| },
| closeDialog() {
| this.setData({
| showErrorDialog: false
| })
| }
| })
|
|