index.vue
4.9 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
<template>
<view class="login-page">
<!-- 登录表单区域 -->
<view class="login-form">
<!-- 账号输入框 -->
<view class="form-item">
<up-input
v-model="form.account"
placeholder="请输入登录账号"
border="surround"
clearable
input-align="left"
:disabled="isLoading"
@blur="checkAccount"
>
<template #prefix>
<up-icon name="account" color="#909399" size="20"></up-icon>
</template>
</up-input>
<!-- 账号错误提示 -->
<view class="error-tip" v-if="error.account">{{ error.account }}</view>
</view>
<!-- 密码输入框(移除查看密码功能) -->
<view class="form-item">
<up-input
v-model="form.password"
placeholder="请输入登录密码"
border="surround"
clearable
input-align="left"
type="password"
:disabled="isLoading"
@blur="checkPassword"
>
<template #prefix>
<up-icon name="lock" color="#909399" size="20"></up-icon>
</template>
</up-input>
<!-- 密码错误提示 -->
<view class="error-tip" v-if="error.password">{{ error.password }}</view>
</view>
<!-- 登录按钮 -->
<up-button
class="login-btn"
type="primary"
size="large"
:loading="isLoading"
@click="handleLogin"
>
登录
</up-button>
</view>
</view>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useUserStore } from '@/pinia/user';
import globalConfig from '@/common/config/global';
// 表单数据
const form = reactive({
account: '', // 账号
password: '' // 密码
});
// 状态管理
const isLoading = ref(false);
const error = reactive({
account: '',
password: ''
});
// 实例化 Pinia 用户仓库
const userStore = useUserStore();
// 页面加载时校验登录态
onMounted(() => {
try {
// 已登录则直接跳首页
if (userStore.isLogin) {
uni.switchTab({
url: globalConfig.router.tabBarList[1].path,
fail: () => {
// 非tabBar页面用redirectTo
uni.redirectTo({ url: '/pages/workbench/index' });
}
});
return;
}
} catch (err) {
console.warn('检查登录状态失败:', err);
}
});
// 校验账号
const checkAccount = () => {
if (!form.account) {
error.account = '请输入登录账号';
} else if (!/^[a-zA-Z0-9_-]{4,16}$/.test(form.account)) {
error.account = '账号格式错误(4-16位字母/数字/下划线)';
} else {
error.account = '';
}
};
// 校验密码
const checkPassword = () => {
if (!form.password) {
error.password = '请输入登录密码';
} else if (form.password.length < 6) {
error.password = '密码长度不能少于6位';
} else {
error.password = '';
}
};
// 表单整体校验
const validateForm = () => {
checkAccount();
checkPassword();
return !error.account && !error.password;
};
// 登录处理(核心:补充跳转逻辑)
const handleLogin = async () => {
if (!validateForm()) return;
isLoading.value = true;
try {
await userStore.login({
username: form.account,
password: form.password
});
uni.showToast({ title: '登录成功', icon: 'success', duration: 1500 });
// 登录成功后跳转首页(优先tabBar,兼容普通页面)
setTimeout(() => {
// 方式1:跳tabBar首页(推荐,需在pages.json配置tabBar)
uni.switchTab({
url: globalConfig.router.tabBarList[1].path,
fail: (err) => {
console.warn('tabBar跳转失败,切换为普通跳转:', err);
// 方式2:跳普通首页(非tabBar页面)
uni.redirectTo({ url: '/pages/workbench/index' });
}
});
}, 1500);
} catch (err) {
console.error('登录失败详情:', err);
const errorMsg = err.message === '网络异常,请稍后重试'
? '网络异常,请稍后重试'
: err.message || '登录失败,请检查账号密码';
uni.showToast({
title: errorMsg,
icon: 'none',
duration: 2000
});
} finally {
isLoading.value = false;
}
};
</script>
<style scoped lang="scss">
.login-page {
min-height: 100vh;
background-color: #f5f5f5;
padding: 40rpx 30rpx;
box-sizing: border-box;
}
.login-form {
background-color: #fff;
padding: 40rpx 30rpx;
border-radius: 12rpx;
box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}
.form-item {
margin-bottom: 30rpx;
position: relative;
}
.error-tip {
font-size: 24rpx;
color: #f56c6c;
margin-top: 10rpx;
line-height: 1.2;
}
.login-btn {
width: 100%;
height: 88rpx;
line-height: 88rpx;
font-size: 32rpx;
border-radius: 44rpx;
background-color: #409eff;
margin-top: 10rpx;
}
</style>