王飞
2025-01-23 99365608dec6eade7d645a91fb0f2205a332d1f1
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
var util = require("../../utils/util")
var token1 = getApp().globalData.token;
var req_url = getApp().globalData.base_url;
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'
const app = getApp()
Page({
    data: {
        locak:false,
        logoUrl: ['/pages/statis/img/login.png'],
        phoneUrl: ['/pages/statis/img/user.png'],
        codeUrl: ['/pages/statis/img/password.png'],
        buttonColor: false,
        yzms: '',
        yzmcoon: '发送验证码',
        checked: true
    },
    username: '',
    password: '',
    phone: '',
    usernameBindInput(event) {
        this.username = event.detail
        this.inputIsEmpty()
    },
    passwordBindInput(event) {
        this.password = event.detail
        this.inputIsEmpty()
    },
    tap(event) {
        
        this.setData({
            locak:false
        })
    },
    hqyzm(event) {
        if (this.data.checked) {
            var phone = this.phone
            if (phone == "" || phone == null) {
                return Notify({
                    message: '请先输入手机号后在获取验证码',
                    top: app.globalData.CustomBar,
                    type: 'warning'
                })
                // layer.msg('请先输入手机号后在获取验证码')
            } else {
                var that = this
                var data = "phone=" + phone
                wx.request({
                    url: req_url + "/findPhoneUser",
                    data: {
                        phone: that.phone,
                    },
                    verify: false,
                    loading: true,
                    title: '请稍后...',
                    success(data) {
                        if (data.data == "" || data.data.length == 0) {
                            return Notify({
                                message: '当前手机号不存在,请重新输入后在获取',
                                top: app.globalData.CustomBar,
                                type: 'warning'
                            })
                        } else {
                            wx.setStorageSync("username", data.data.name);
                            that.username = data.data.name;
                            //发送验证码
                            var yzm = that.generateRandomCode();
                            var yzmpd = "";
                            var data = "phone=" + phone + "&randomNumber=" + yzm;
                            wx.request({
                                url: req_url + "/sms",
                                data: {
                                    phone: that.phone,
                                    randomNumber: yzm,
                                },
                                verify: false,
                                loading: true,
                                title: '请稍后...',
                                success(data) {
                                    console.log(data);
                                    console.log(data.data);
                                    that.setData({
                                        yzms: data.data
                                    })
                                },
                                fail: (error) => {
                                    console.error("QwQ请求失败:", error);
                                    wx.showToast({
                                        title: '请求失败',
                                        icon: 'none',
                                        duration: 2000
                                    });
                                }
                            });
                            that.startCountdown(60)
                        }
                    }
                })
            }
        }
    },
    startCountdown: function (seconds) {
        var count = seconds;
        this.setData({
            checked: false,
            yzmcoon: count + ' 秒后重新获取'
        })
        var that = this
        var timer = setInterval(function () {
            count--;
            if (count <= 0) {
                clearInterval(timer);
                that.setData({
                    checked: true,
                    yzmcoon: ' 获取验证码'
                })
            } else {
                that.setData({
                    checked: false,
                    yzmcoon: count + ' 秒后重新获取'
                })
            }
        }, 1000);
    },
    generateRandomCode: function () {
        var code = "";
        var possible = "0123456789"; // 可选的字符集合
        for (var i = 0; i < 4; i++) {
            // 从字符集合中随机选择一个字符,并添加到验证码中
            code += possible.charAt(Math.floor(Math.random() * possible.length));
        }
        return code;
    },
    inputIsEmpty() {
        let buttonColor = false
        if (this.username && this.password)
            buttonColor = true
        if (buttonColor !== this.data.buttonColor)
            this.setData({
                'buttonColor': buttonColor
            })
    },
    login(event) {
        Notify.clear()
        if (!this.username)
            return Notify({
                message: '用户名不能为空',
                top: app.globalData.CustomBar,
                type: 'warning'
            })
        if (!this.password)
            return Notify({
                message: '验证码不能为空',
                top: app.globalData.CustomBar,
                type: 'warning'
            })
        // if (this.data.yzms != this.password)
        //     return Notify({
        //         message: '验证码有误',
        //         top: app.globalData.CustomBar,
        //         type: 'warning'
        //     })
        let _this = this
        wx.login({
            success: (res) => {
                wx.request({
                    url: req_url + "/wxlogin",
 
                    header: {
                        'content-type': 'application/json;charset=UTF-8'
                    },
                    method: 'POST',
                    data: {
                        username: this.username,
                        password: this.password,
                        code: res.code
                    },
                    success: (res) => {
                        console.log(res);
                        if (res.data.code == 0) {
                            // _this.token1 = res.data.data
                            wx.setStorageSync("username", res.data.data.name);
                            wx.setStorageSync("phone", res.data.data.phone);
                            wx.setStorageSync("token", res.data.data.token);
                            wx.setStorageSync("password", res.data.data.password);
                            wx.setStorage({
                                key: 'token',
                                data: res.data.data.token
                            })
                            wx.redirectTo({
                                url: `/pages/index/index?message=${'登陆成功'}`
                            })
 
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                                duration: 2000
                            });
                        }
 
                    },
                    fail: (error) => {
                        console.error("QwQ请求失败:", error);
                        wx.showToast({
                            title: '请求失败',
                            icon: 'none',
                            duration: 2000
                        });
                    }
                })
            }
        })
    },
    onChange(event) { //复选框onchange事件
        this.setData({
            checked: event.detail,
        });
    },
    onLoad(options) {
        console.log(options);
        this.setData({
            locak:options
        })
        // debugger
        if (wx.getStorageSync('user')) {
            setTimeout(item => {
                if (this.checked) {
                    this.username = wx.getStorageSync('username');
                    this.password = wx.getStorageSync('password');
                    this.login();
                }
            }, 500)
        }
    }
})