吉安感知网项目-前端
shuishen
2026-02-03 89380e6260a75d1d3b94de687ebcc2f50d50659d
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!-- 修改密码 -->
<template>
    <view class="container">
    <u-navbar
      title="修改密码"
      :is-back="true"
      back-text=""
      :back-icon-size="40"
      @left-click="onBackClick"
    >
 
    </u-navbar>
            <view class="pageBg"></view>
        <view class="detailBox">
            <div class="detailCon">
                <div class="orderRow">
                    <div class="rowTitle">账号</div>
                    <input type="text" v-model="passwordForm.username" placeholder="请输入" class="input-item" />
                </div>
                <div class="orderRow">
                    <div class="rowTitle">密码</div>
                    <input type="password" v-model="passwordForm.password" placeholder="请输入" class="input-item" />
 
                </div>
                <div class="orderRow">
                    <div class="rowTitle">确认密码</div>
                    <input type="password" v-model="passwordForm.password2" placeholder="请输入" class="input-item" />
                </div>
                <div class="orderRow">
                    <div class="rowTitle">验证码</div>
                    <div class="code-container">
                        <input type="text" v-model="passwordForm.code" placeholder="请输入" class="input-item code-input" />
                        <u-button :disabled="countDown > 0" @click="sendVerificationCode" class="send-code-btn getCode" color="#007AFF">
                            {{ countDown > 0 ? `${countDown}s后重发` : '发送验证码' }}
                        </u-button>
                    </div>
                </div>
            </div>
        </view>
        <view class="btngroup">
            <u-button color="#AEAEAE" class="custom-style"  @click="reset">重置</u-button>
            <u-button color="#1D6FE9" class="custom-style"  @click="submit">提交</u-button>
        </view>
    </view>
</template>
 
<script setup>
    import {
        useUserStore
    } from "@/store/index.js";
    import md5 from "js-md5";
    import { getUserInfo,
    updateInfo,
    updatePassword,
    sendResetCode,
    resetPassword
} from '@/api/user/index.js';
    const passwordIcon = {
        show: 'fas fa-eye', // 显示密码时的图标
        hide: 'fas fa-eye-slash' // 隐藏密码时的图标
    }
    const userStore = useUserStore();
const passwordForm = ref({
    username: '',
    password: '',
    password2: '',
    code: ''
});
const countDown = ref(0);
let timer = null;
    // 校验密码不能包含中文
    const validatePasswordNoChinese = (password) => {
        const chineseRegex = /[\u4e00-\u9fa5]/;
        if (chineseRegex.test(password)) {
 
            uni.showToast({
                title: '密码不能包含中文字符',
                icon: 'none',
                duration: 2000
            });
            return false;
        }
        return true;
    };
    // 校验密码复杂度(长度≥6位,包含字母和数字)
    const validatePasswordStrength = (password) => {
        if (password.length < 6) {
 
            uni.showToast({
                title: '密码长度不能少于6位',
                icon: 'none',
                duration: 2000
            });
            return false;
        }
        // const hasLetter = /[a-zA-Z]/.test(password);
        // const hasNumber = /\d/.test(password);
        // if (!hasLetter || !hasNumber) {
        //   ElMessage.error('密码需同时包含字母和数字');
        //   return false;
        // }
        return true;
    };
    const clearForm = () => {
        passwordForm.value = {
            username: '',
            password: '',
            password2: '',
            code: ''
        };
        countDown.value = 0;
        if (timer) {
            clearInterval(timer);
            timer = null;
        }
    };
 
    // 发送验证码
    const sendVerificationCode = () => {
        const { username } = passwordForm.value;
        if (!username) {
            uni.showToast({
                title: '请先输入账号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
 
        // 这里假设账号就是手机号,实际项目中可能需要验证格式
        sendResetCode(username).then(res => {
            if (res.data.code === 200) {
                uni.showToast({
                    title: '验证码发送成功',
                    icon: 'success',
                    duration: 2000
                });
 
                // 开始倒计时
                countDown.value = 60;
                timer = setInterval(() => {
                    countDown.value--;
                    if (countDown.value <= 0) {
                        clearInterval(timer);
                        timer = null;
                    }
                }, 1000);
            } else {
                uni.showToast({
                    title: res.data.msg || '验证码发送失败',
                    icon: 'none',
                    duration: 2000
                });
            }
        }).catch(err => {
            uni.showToast({
                title: '网络错误,请稍后重试',
                icon: 'none',
                duration: 2000
            });
        });
    };
    const reset = () => {
        clearForm();
    };
 
    const submit = () => {
        const { username, password, password2, code } = passwordForm.value;
 
        // 账号校验
        if (!username) {
            uni.showToast({
                title: '请输入账号',
                icon: 'none',
                duration: 2000
            });
            return;
        }
 
        // 新密码校验
        if (!password) {
            uni.showToast({
                title: '请输入新密码',
                icon: 'none',
                duration: 2000
            });
            return;
        }
 
        // 中文校验
        if (!validatePasswordNoChinese(password)) return;
 
        // 确认密码校验
        if (password !== password2) {
            uni.showToast({
                title: '两次输入的新密码不一致',
                icon: 'none',
                duration: 2000
            });
            return;
        }
 
        // 验证码校验
        if (!code) {
            uni.showToast({
                title: '请输入验证码',
                icon: 'none',
                duration: 2000
            });
            return;
        }
 
        // 复杂度校验
        if (!validatePasswordStrength(password)) return;
 
        // 调用重置密码接口
        const resetData = {
            username,
            // password: md5(password),
            // confirmPassword: md5(password2),
      password: password,
      password2: password2,
            code
        };
 
        resetPassword(resetData).then(res => {
            if (res.data.code === 200) {
                uni.showToast({
                    title: '密码修改成功',
                    icon: 'none',
                    duration: 2000
                });
                clearForm();
                userStore.setUserInfo(null);
                uni.removeStorageSync('rememberedUser');
                uni.reLaunch({
                    url: '/pages/login/index'
                });
            } else {
                uni.showToast({
                    title: res.data.msg || '密码修改失败',
                    icon: 'none',
                    duration: 2000
                });
            }
        }).catch(err => {
            uni.showToast({
                title: err.data.msg,
                icon: 'none',
                duration: 2000
            });
        });
    };
  // 返回按钮点击事件
  const onBackClick = () => {
    uni.navigateBack()
  }
    onShow(async () => {
        clearForm();
    });
</script>
 
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  box-sizing: border-box;
  // 添加导航栏高度的padding-top,避免内容被遮挡
  padding-top: 88rpx;
 
}
:deep(){
  .u-navbar__content{
    background: url("@/static/images/user/userbg.svg")  no-repeat !important;
    background-size: 100%!important;
    height: 44px !important;
  }
}
 
        .pageBg {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: url("@/static/images/user/userbg.svg")  no-repeat ;
            background-size: 100%;
        }
 
    .detailBox {
            position: absolute;
            // top: 180rpx;
            // margin-top: 40rpx;
        width: 702rpx;
 
        min-height: 326rpx;
        background: #FFFFFF;
        border-radius: 12rpx;
        box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
    margin-top: 88rpx;
        .detailCon {
            padding: 0 24rpx;
        }
 
        .orderRow {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 96rpx;
            border-bottom: 2rpx solid #f5f5f5;
            color: #7b7b7b;
 
 
            .rowTitle {
                font-family: Source Han Sans CN, Source Han Sans CN;
                font-weight: 400;
                font-size: 30rpx;
                color: #222324;
                white-space: nowrap;
            }
 
            input.input-item {
                text-align: right;
                border: none !important;
                background: transparent !important;
                box-shadow: none !important;
                padding: 0 !important;
                margin: 0 !important;
                height: auto !important;
 
                outline: none;
                font-size: 30rpx;
                color: #222324;
                font-family: Source Han Sans CN, Source Han Sans CN;
            }
      :deep(.uni-input-placeholder) {
        color: #D5D5D5;
      }
            input.input-item::placeholder {
                font-size: 30rpx;
            }
 
            .code-container {
                display: flex;
                align-items: center;
                justify-content: flex-end;
                width: 100%;
            }
 
            .code-input {
                flex: 1;
                max-width: 400rpx;
                margin-right: 20rpx;
            }
 
            .send-code-btn {
                width: 200rpx;
                height: 70rpx;
                line-height: 70rpx;
                font-size: 26rpx;
                background: #f5f5f5;
                color: #666;
                border-radius: 8rpx;
                text-align: center;
                border: none;
            }
 
            .send-code-btn:disabled {
                background: #e0e0e0;
                color: #999;
            }
        }
    }
 
    .btngroup {
        display: flex;
        position: absolute;
        bottom: 150rpx;
    }
 
    :deep(.u-button:first-child) {
        margin-right: 30rpx !important;
    }
 
    .custom-style {
        width: 276rpx;
        height: 76rpx;
          font-family: "Source Han Sans CN";
        font-weight: 400;
        font-size: 28rpx;
    }
    :deep(.u-button.data-v-461e713c){
        width: 276rpx !important;
        height: 76rpx !important;
    }
    .getCode {
            margin-left: 20rpx;
                width: 180rpx;
            }
</style>