index.vue
10.4 KB
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
405
406
407
408
409
410
411
412
413
414
415
<template>
<view class="page-container">
<!-- 顶部标题区 -->
<view class="top-title">
<text class="welcome-text">你好,欢迎光临</text>
<text class="platform-name">全域智能运营管理平台</text>
</view>
<!-- 登录表单区域 -->
<view class="login-form">
<!-- uview-plus的Tabs组件 -->
<up-tabs
:list="tabList"
active-color="#0A86F4"
inactive-color="#666"
font-size="16px"
line-width="40"
line-height="3"
@change="handleTabChange"
></up-tabs>
<!-- 表单校验容器 -->
<up-form
label-position="left"
:model="form"
ref="loginFormRef"
labelWidth="0"
>
<!-- 手机号输入框 -->
<up-form-item v-if="loginType === '手机号登录'" prop="mobile">
<up-input
v-model="form.mobile"
border="surround"
clearable
maxlength="11"
input-align="left"
fontSize="16px"
:disabled="isLoading"
shape="circle"
placeholder="请输入手机号"
@blur="() => loginFormRef.validateField('mobile')"
/>
</up-form-item>
<!-- 账号输入框 -->
<up-form-item v-else prop="account">
<up-input
v-model="form.account"
border="surround"
clearable
maxlength="30"
input-align="left"
fontSize="16px"
:disabled="isLoading"
shape="circle"
placeholder="请输入账户"
@blur="() => loginFormRef.validateField('account')"
/>
</up-form-item>
<!-- 密码输入框 -->
<up-form-item
prop="password"
class="password-item"
>
<up-input
v-model="form.password"
placeholder="请输入密码"
maxlength="20"
border="surround"
clearable
input-align="left"
type="password"
:disabled="isLoading"
shape="circle"
@blur="() => loginFormRef.validateField('password')"
/>
</up-form-item>
<!-- 记住密码单独一行,居右显示 -->
<view class="remember-wrap">
<up-checkbox
:customStyle="{marginBottom: '8px'}"
label="记住密码"
name="agree"
usedAlone
v-model:checked="rememberPwd"
>
</up-checkbox>
</view>
</up-form>
<!-- 登录按钮 -->
<up-button
class="login-btn"
type="primary"
size="large"
:loading="isLoading"
@click="handleLogin"
shape="circle"
>
登录
</up-button>
</view>
<!-- 版权信息 -->
<view class="copyright">蓟城山水集团版权所有</view>
</view>
</template>
<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import { useUserStore } from '@/pinia/user';
import globalConfig from '@/common/config/global';
import CryptoJS from 'crypto-js';
// ========== 加密工具函数(建议抽离到 @/utils/encrypt.js) ==========
// 密钥配置:生产环境建议从后端接口获取,不要硬编码
const CRYPTO_CONFIG = {
key: CryptoJS.enc.Utf8.parse('jcsscrypto123abc'), // 16位key(AES-128)
iv: CryptoJS.enc.Utf8.parse('abc123jcsscrypto'), // 16位iv
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
};
/**
* AES加密
* @param {string} text 待加密文本
* @returns {string} 加密后的字符串
*/
const aesEncrypt = (text) => {
if (!text) return '';
try {
return CryptoJS.AES.encrypt(text, CRYPTO_CONFIG.key, {
iv: CRYPTO_CONFIG.iv,
mode: CRYPTO_CONFIG.mode,
padding: CRYPTO_CONFIG.padding
}).toString();
} catch (err) {
console.error('AES加密失败:', err);
return '';
}
};
/**
* AES解密
* @param {string} encryptedText 加密后的字符串
* @returns {string} 解密后的原始文本
*/
const aesDecrypt = (encryptedText) => {
if (!encryptedText) return '';
try {
const decryptObj = CryptoJS.AES.decrypt(encryptedText, CRYPTO_CONFIG.key, {
iv: CRYPTO_CONFIG.iv,
mode: CRYPTO_CONFIG.mode,
padding: CRYPTO_CONFIG.padding
});
return decryptObj.toString(CryptoJS.enc.Utf8);
} catch (err) {
console.error('AES解密失败:', err);
return '';
}
};
// ========== 业务逻辑 ==========
const userStore = useUserStore();
const loginFormRef = ref(null);
const isLoading = ref(false);
// Tabs配置
const tabList = ref([
{ name: '手机号登录' },
{ name: '账号登录' }
]);
const loginType = ref('手机号登录');
// 记住密码
const rememberPwd = ref(true);
// 表单数据
const form = reactive({
account: '',
mobile: '',
password: ''
});
// 表单校验规则
const loginFormRules = reactive({
account: [
{ type: 'string', required: true, message: '请输入登录账号', trigger: ['change', 'blur'] },
{ type: 'string', min: 2, max: 30, message: '账号长度为2-30个字符', trigger: ['change', 'blur'] }
],
mobile: [
{ type: 'string', required: true, message: '请输入手机号', trigger: ['change', 'blur'] },
{ type: 'string', pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: ['change', 'blur'] }
],
password: [
{ type: 'string', required: true, message: '请输入登录密码', trigger: ['change', 'blur'] },
{ type: 'string', min: 3, max: 20, message: '密码长度为3-20个字符', trigger: ['change', 'blur'] }
]
});
// Tabs切换事件
const handleTabChange = ({ name }) => {
if (isLoading.value) return;
loginType.value = name;
if (name === '手机号登录') {
form.account = '';
} else {
form.mobile = '';
}
nextTick(() => {
loginFormRef.value?.clearValidate();
});
};
// 检查登录状态
const checkLoginStatus = () => {
try {
if (userStore.isLogin) {
uni.switchTab({
url: '/pages/workbench/index',
fail: () => uni.reLaunch({ url: '/pages/workbench/index' })
});
}
} catch (err) {
console.warn('检查登录状态失败:', err);
}
};
// 登录方法
const handleLogin = async () => {
try {
await loginFormRef.value.validate();
isLoading.value = true;
// 组装登录参数
const loginParams = { password: form.password };
if (loginType.value === '手机号登录') {
loginParams.username = form.mobile;
} else {
loginParams.username = form.account;
}
// 执行登录
await userStore.login(loginParams);
// 保存数据:账号/密码 均用AES加密
if (rememberPwd.value) {
try {
if (loginType.value === '手机号登录') {
uni.setStorageSync('login_mobile', aesEncrypt(form.mobile));
uni.removeStorageSync('login_account');
} else {
uni.setStorageSync('login_account', aesEncrypt(form.account));
uni.removeStorageSync('login_mobile');
}
// 密码加密存储(可解密还原)
uni.setStorageSync('login_password', aesEncrypt(form.password));
} catch (err) {
console.warn('保存登录信息失败:', err);
}
} else {
// 清除缓存
uni.removeStorageSync('login_account');
uni.removeStorageSync('login_mobile');
uni.removeStorageSync('login_password');
}
// 登录成功跳转
uni.showToast({ title: '登录成功', icon: 'success', duration: 1000 });
setTimeout(() => {
uni.switchTab({
url: globalConfig.router.tabBarList[1].path,
fail: () => uni.reLaunch({ url: '/pages/workbench/index' })
});
}, 1000);
} catch (err) {
if (!Array.isArray(err)) {
console.error('登录失败:', err);
uni.showToast({
title: err.msg || '账号或密码错误,请重试',
icon: 'none',
duration: 1000
});
}
} finally {
isLoading.value = false;
}
};
// 生命周期
onMounted(() => {
checkLoginStatus();
// 初始化表单规则
nextTick(() => {
loginFormRef.value?.setRules(loginFormRules);
});
// 读取缓存并解密填充
if (rememberPwd.value) {
try {
const savedAccount = aesDecrypt(uni.getStorageSync('login_account') || '');
const savedMobile = aesDecrypt(uni.getStorageSync('login_mobile') || '');
const savedPwd = aesDecrypt(uni.getStorageSync('login_password') || '');
if (savedAccount) {
form.account = savedAccount;
loginType.value = '账号登录';
} else if (savedMobile) {
form.mobile = savedMobile;
loginType.value = '手机号登录';
}
// 密码解密后填充到输入框(可展示)
form.password = savedPwd;
} catch (err) {
console.warn('读取缓存失败:', err);
}
}
});
</script>
<style scoped lang="scss">
.top-title {
background: url("https://img.jichengshanshui.com.cn:28207/appimg/bg.jpg") no-repeat;
background-size: 100% 100%;
color: #fff;
padding: 200rpx 0 200rpx 40rpx;
text-align: left;
position: relative;
z-index: 1;
.welcome-text {
font-size: 23px;
display: block;
margin-bottom: 10px;
font-weight: 500;
}
.platform-name {
margin-bottom: 10px;
font-size: 23px;
display: block;
opacity: 0.95;
}
}
.login-form {
background-color: #fff;
padding: 20px 15px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
margin: -90px 20px 0;
position: relative;
z-index: 10;
box-sizing: border-box;
}
:deep(.u-tabs) {
margin-bottom: 15px;
.u-tabs__content {
height: auto !important;
}
.u-tab-item {
padding: 0 10px;
}
}
:deep(.u-form-item) {
margin-bottom: 10px;
position: relative;
}
.password-item {
position: relative;
margin-bottom: 5px !important;
}
.remember-wrap {
padding: 0;
margin: 10px 0 20px;
:deep(.u-checkbox) {
font-size: 12px;
color: #666;
justify-content: flex-end;
margin-left: 0 !important;
.u-checkbox__label {
margin-left: 5px;
}
}
}
.login-btn {
margin-top: 10px;
width: 100%;
height: 44px;
line-height: 44px;
border-radius: 4px;
font-size: 16px;
background: #0A86F4;
box-shadow: 0px 4px 6px 1px rgba(25,94,215,0.5);
border-radius: 23px;
}
.copyright {
width: 100%;
text-align: center;
font-size: 12px;
color: #999;
position: fixed;
bottom: 100px;
}
</style>