管灌系统农户端微信小程序(嘉峪关应用)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const app = getApp();
const storage = require('../../utils/storage.js');
const {
  post
} = require('../../api/request.js');
 
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    verificationCode: '',
    codeText: '获取验证码',
    counting: false,
    countDown: 60,
    projectName: '嘉峪关项目', // 默认项目名称
    selectedProject: 'JYG', // 默认项目代码
    showErrorDialog: false
  },
 
  /**
   * 绑定手机号输入
   */
  bindPhoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },
 
  /**
   * 绑定验证码输入
   */
  bindCodeInput: function (e) {
    this.setData({
      verificationCode: e.detail.value
    });
  },
 
  /**
   * 发送验证码
   */
  sendVerificationCode: function () {
    const {
      phone,
      counting
    } = this.data;
 
    // 如果正在倒计时,则不允许再次发送
    if (counting) {
      return;
    }
 
    // 验证手机号格式
    if (!/^1\d{10}$/.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return;
    }
 
    // 开始倒计时
    this.startCountDown();
 
    // 发送验证码请求
    this.postCode();
  },
 
  /**
   * 开始倒计时
   */
  startCountDown: function () {
    this.setData({
      counting: true,
      countDown: 60
    });
 
    const timer = setInterval(() => {
      if (this.data.countDown <= 1) {
        clearInterval(timer);
        this.setData({
          counting: false,
          codeText: '获取验证码'
        });
      } else {
        this.setData({
          countDown: this.data.countDown - 1,
          codeText: `${this.data.countDown - 1}秒后重发`
        });
      }
    }, 1000);
 
    // 保存timer引用,以便在页面卸载时清除
    this.countDownTimer = timer;
  },
 
  /**
   * 停止倒计时
   */
  stopCountDown: function () {
    if (this.countDownTimer) {
      clearInterval(this.countDownTimer);
    }
    this.setData({
      counting: false,
      codeText: '获取验证码'
    });
  },
 
  /**
   * 登录
   */
  login: function () {
    const {
      phone,
      verificationCode
    } = this.data;
 
    // 验证手机号和验证码
    if (!/^1\d{10}$/.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return;
    }
 
    if (!/^\d{6}$/.test(verificationCode)) {
      wx.showToast({
        title: '请输入6位验证码',
        icon: 'none'
      });
      return;
    }
 
    // 显示加载中
    wx.showLoading({
      title: '登录中...',
      mask: true
    });
    this.wsLogin();
 
 
    // 发送登录请求
    post('/api/loginByCode', {
      phone: phone,
      code: verificationCode,
      projectCode: this.data.selectedProject
    }).then(res => {
      wx.hideLoading();
 
      if (res.code === 0 && res.data) {
        // 保存用户信息和token
        storage.setUserInfo(res.data);
        storage.setToken(res.data.token);
 
        // 跳转到首页
        wx.switchTab({
          url: '/pages/index/index'
        });
      } else {
        wx.showToast({
          title: res.msg || '登录失败,请重试',
          icon: 'none'
        });
      }
    }).catch(err => {
      wx.hideLoading();
      console.error('登录失败', err);
      wx.showToast({
        title: '网络异常,请重试',
        icon: 'none'
      });
    });
  },
 
  /**
   * 跳转到注册页面
   */
  goToRegister: function () {
    wx.showToast({
      title: '注册功能暂未开放',
      icon: 'none',
      duration: 2000
    });
  },
 
  /**
   * 跳转到忘记密码页面
   */
  goToForgetPassword: function () {
    wx.showToast({
      title: '找回密码功能暂未开放',
      icon: 'none',
      duration: 2000
    });
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取选择的项目
    if (options.project) {
      this.setData({
        selectedProject: options.project,
        projectName: options.project === 'JYG' ? '嘉峪关项目' : '民勤项目'
      });
    } else {
      // 尝试从本地存储获取已选择的项目
      storage.getItem('selectedProject').then(project => {
        if (project) {
          this.setData({
            selectedProject: project,
            projectName: project === 'JYG' ? '嘉峪关项目' : '民勤项目'
          });
        }
      }).catch(err => {
        console.error('获取已选择项目失败:', err);
      });
    }
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
 
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 清除验证码倒计时定时器
    if (this.countDownTimer) {
      clearInterval(this.countDownTimer);
      this.countDownTimer = null;
    }
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
 
  },
  //获取验证码
  postCode: function () {
    const params = {
      url: 'wx/client/send_sms?phoneNumber=' + this.data.phone
    };
    post(params)
      .then((data) => {
        wx.showToast({
          title: '验证码已发送',
          icon: 'success',
          duration: 2000,
        });
        this.setData({
          codeSent: true,
        });
        // 启动倒计时
        this.startCountdown();
      })
      .catch((error) => {
        wx.showToast({
          title: error.msg,
          icon: 'none'
        });
        console.error('Failed to add item:', error);
      });
  },
  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.phone,
        securityCode: this.data.verificationCode,
        code: wxCode
      }
    };
    post(params)
      .then((data) => {
        wx.hideLoading();
        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.hideLoading();
        wx.showToast({
          title: error.msg,
          icon: 'error',
          duration: 3000,
        });
        console.error('Failed to add item:', error);
      });
  },
  bindSuccess: function () {
    if (!this.data.isButtonEnabled) return;
    wx.showToast({
      title: '绑定成功',
      icon: 'success'
    });
    // 跳转到 TabBar 页面
    wx.redirectTo({
      url: '/pages/home/home' // 这里填写你想要跳转的 TabBar 页面路径
    });
  },
})