提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<el-form |
|
3 |
v-show="getShow" |
|
4 |
ref="formLogin" |
|
5 |
:model="loginData.loginForm" |
|
6 |
:rules="LoginRules" |
|
7 |
class="login-form" |
|
8 |
label-position="top" |
|
9 |
label-width="120px" |
|
10 |
size="large" |
|
11 |
> |
|
12 |
<el-row style="margin-right: -10px; margin-left: -10px"> |
|
13 |
<el-col :span="24" style="padding-right: 10px; padding-left: 10px"> |
|
14 |
<el-form-item> |
|
15 |
<LoginFormTitle style="width: 100%" /> |
|
16 |
</el-form-item> |
|
17 |
</el-col> |
|
18 |
<el-col :span="24" style="padding-right: 10px; padding-left: 10px"> |
|
19 |
<el-form-item v-if="loginData.tenantEnable === 'true'" prop="tenantName"> |
|
20 |
<el-input |
|
21 |
v-model="loginData.loginForm.tenantName" |
|
22 |
:placeholder="t('login.tenantNamePlaceholder')" |
|
23 |
:prefix-icon="iconHouse" |
|
24 |
link |
|
25 |
type="primary" |
|
26 |
/> |
|
27 |
</el-form-item> |
|
28 |
</el-col> |
|
29 |
<el-col :span="24" style="padding-right: 10px; padding-left: 10px"> |
|
30 |
<el-form-item prop="username"> |
|
31 |
<el-input |
|
32 |
v-model="loginData.loginForm.username" |
|
33 |
:placeholder="t('login.usernamePlaceholder')" |
|
34 |
:prefix-icon="iconAvatar" |
|
35 |
/> |
|
36 |
</el-form-item> |
|
37 |
</el-col> |
|
38 |
<el-col :span="24" style="padding-right: 10px; padding-left: 10px"> |
|
39 |
<el-form-item prop="password"> |
|
40 |
<el-input |
|
41 |
v-model="loginData.loginForm.password" |
|
42 |
:placeholder="t('login.passwordPlaceholder')" |
|
43 |
:prefix-icon="iconLock" |
|
44 |
show-password |
|
45 |
type="password" |
|
46 |
@keyup.enter="getCode()" |
|
47 |
/> |
|
48 |
</el-form-item> |
|
49 |
</el-col> |
|
50 |
<el-col |
|
51 |
:span="24" |
|
52 |
style="padding-right: 10px; padding-left: 10px; margin-top: -20px; margin-bottom: -20px" |
|
53 |
> |
|
54 |
<el-form-item> |
|
55 |
<el-row justify="space-between" style="width: 100%"> |
|
56 |
<el-col :span="6"> |
|
57 |
<el-checkbox v-model="loginData.loginForm.rememberMe"> |
|
58 |
{{ t('login.remember') }} |
|
59 |
</el-checkbox> |
|
60 |
</el-col> |
|
61 |
<el-col :offset="6" :span="12"> |
|
62 |
<el-link style="float: right" type="primary">{{ t('login.forgetPassword') }}</el-link> |
|
63 |
</el-col> |
|
64 |
</el-row> |
|
65 |
</el-form-item> |
|
66 |
</el-col> |
|
67 |
<el-col :span="24" style="padding-right: 10px; padding-left: 10px"> |
|
68 |
<el-form-item> |
|
69 |
<XButton |
|
70 |
:loading="loginLoading" |
|
71 |
:title="t('login.login')" |
|
72 |
class="w-[100%]" |
|
73 |
type="primary" |
|
74 |
@click="getCode()" |
|
75 |
/> |
|
76 |
</el-form-item> |
|
77 |
</el-col> |
|
78 |
<Verify |
|
79 |
ref="verify" |
|
80 |
:captchaType="captchaType" |
|
81 |
:imgSize="{ width: '400px', height: '200px' }" |
|
82 |
mode="pop" |
|
83 |
@success="handleLogin" |
|
84 |
/> |
|
85 |
</el-row> |
|
86 |
</el-form> |
|
87 |
</template> |
|
88 |
<script lang="ts" setup> |
|
89 |
import { ElLoading } from 'element-plus' |
|
90 |
import LoginFormTitle from './LoginFormTitle.vue' |
|
91 |
import type { RouteLocationNormalizedLoaded } from 'vue-router' |
|
92 |
|
|
93 |
import { useIcon } from '@/hooks/web/useIcon' |
|
94 |
|
|
95 |
import * as authUtil from '@/utils/auth' |
|
96 |
import { usePermissionStore } from '@/store/modules/permission' |
|
97 |
import * as LoginApi from '@/api/login' |
|
98 |
import { LoginStateEnum, useFormValid, useLoginState } from './useLogin' |
|
99 |
|
|
100 |
defineOptions({ name: 'LoginForm' }) |
|
101 |
|
|
102 |
const { t } = useI18n() |
|
103 |
const message = useMessage() |
|
104 |
const iconHouse = useIcon({ icon: 'ep:house' }) |
|
105 |
const iconAvatar = useIcon({ icon: 'ep:avatar' }) |
|
106 |
const iconLock = useIcon({ icon: 'ep:lock' }) |
|
107 |
const formLogin = ref() |
|
108 |
const { validForm } = useFormValid(formLogin) |
|
109 |
const { setLoginState, getLoginState } = useLoginState() |
|
110 |
const { currentRoute, push } = useRouter() |
|
111 |
const permissionStore = usePermissionStore() |
|
112 |
const redirect = ref<string>('') |
|
113 |
const loginLoading = ref(false) |
|
114 |
const verify = ref() |
|
115 |
const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字 |
|
116 |
|
|
117 |
const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN) |
|
118 |
|
|
119 |
const LoginRules = { |
|
120 |
tenantName: [required], |
|
121 |
username: [required], |
|
122 |
password: [required] |
|
123 |
} |
|
124 |
const loginData = reactive({ |
|
125 |
isShowPassword: false, |
|
126 |
captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE, |
|
127 |
tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE, |
|
128 |
loginForm: { |
|
129 |
tenantName: import.meta.env.VITE_APP_DEFAULT_LOGIN_TENANT || '', |
|
130 |
username: import.meta.env.VITE_APP_DEFAULT_LOGIN_USERNAME || '', |
|
131 |
password: import.meta.env.VITE_APP_DEFAULT_LOGIN_PASSWORD || '', |
|
132 |
captchaVerification: '', |
|
133 |
rememberMe: true // 默认记录我。如果不需要,可手动修改 |
|
134 |
} |
|
135 |
}) |
|
136 |
|
|
137 |
// 获取验证码 |
|
138 |
const getCode = async () => { |
|
139 |
// 情况一,未开启:则直接登录 |
|
140 |
if (loginData.captchaEnable === 'false') { |
|
141 |
await handleLogin({}) |
|
142 |
} else { |
|
143 |
// 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录 |
|
144 |
// 弹出验证码 |
|
145 |
verify.value.show() |
|
146 |
} |
|
147 |
} |
|
148 |
// 获取租户 ID |
|
149 |
const getTenantId = async () => { |
|
150 |
if (loginData.tenantEnable === 'true') { |
|
151 |
const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName) |
|
152 |
authUtil.setTenantId(res) |
|
153 |
} |
|
154 |
} |
|
155 |
// 记住我 |
|
156 |
const getLoginFormCache = () => { |
|
157 |
const loginForm = authUtil.getLoginForm() |
|
158 |
if (loginForm) { |
|
159 |
loginData.loginForm = { |
|
160 |
...loginData.loginForm, |
|
161 |
username: loginForm.username ? loginForm.username : loginData.loginForm.username, |
|
162 |
password: loginForm.password ? loginForm.password : loginData.loginForm.password, |
|
163 |
rememberMe: loginForm.rememberMe, |
|
164 |
tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName |
|
165 |
} |
|
166 |
} |
|
167 |
} |
|
168 |
// 根据域名,获得租户信息 |
|
169 |
const getTenantByWebsite = async () => { |
|
170 |
const website = location.host |
|
171 |
const res = await LoginApi.getTenantByWebsite(website) |
|
172 |
if (res) { |
|
173 |
loginData.loginForm.tenantName = res.name |
|
174 |
authUtil.setTenantId(res.id) |
|
175 |
} |
|
176 |
} |
|
177 |
const loading = ref() // ElLoading.service 返回的实例 |
|
178 |
// 登录 |
|
179 |
const handleLogin = async (params) => { |
|
180 |
loginLoading.value = true |
|
181 |
try { |
|
182 |
await getTenantId() |
|
183 |
const data = await validForm() |
|
184 |
if (!data) { |
|
185 |
return |
|
186 |
} |
|
187 |
loginData.loginForm.captchaVerification = params.captchaVerification |
|
188 |
const res = await LoginApi.login(loginData.loginForm) |
|
189 |
if (!res) { |
|
190 |
return |
|
191 |
} |
|
192 |
loading.value = ElLoading.service({ |
|
193 |
lock: true, |
|
194 |
text: '正在加载系统中...', |
|
195 |
background: 'rgba(0, 0, 0, 0.7)' |
|
196 |
}) |
|
197 |
if (loginData.loginForm.rememberMe) { |
|
198 |
authUtil.setLoginForm(loginData.loginForm) |
|
199 |
} else { |
|
200 |
authUtil.removeLoginForm() |
|
201 |
} |
|
202 |
authUtil.setToken(res) |
|
203 |
if (!redirect.value) { |
|
204 |
redirect.value = '/' |
|
205 |
} |
39248b
|
206 |
let tenantId = authUtil.getTenantId() |
H |
207 |
// if(tenantId != 1) { |
|
208 |
// //只要不是系统租户,登录成功跳转到home2页面 |
|
209 |
// window.location.href = '/home2' |
|
210 |
// } else { |
|
211 |
// 判断是否为SSO登录 |
|
212 |
if (redirect.value.indexOf('sso') !== -1) { |
|
213 |
window.location.href = window.location.href.replace('/login?redirect=', '') |
|
214 |
} else { |
|
215 |
push({ path: redirect.value || permissionStore.addRouters[0].path }) |
|
216 |
} |
|
217 |
// } |
820397
|
218 |
} finally { |
H |
219 |
loginLoading.value = false |
|
220 |
loading.value.close() |
|
221 |
} |
|
222 |
} |
|
223 |
|
|
224 |
watch( |
|
225 |
() => currentRoute.value, |
|
226 |
(route: RouteLocationNormalizedLoaded) => { |
|
227 |
redirect.value = route?.query?.redirect as string |
|
228 |
}, |
|
229 |
{ |
|
230 |
immediate: true |
|
231 |
} |
|
232 |
) |
|
233 |
onMounted(() => { |
|
234 |
getLoginFormCache() |
|
235 |
getTenantByWebsite() |
|
236 |
}) |
|
237 |
</script> |
|
238 |
|
|
239 |
<style lang="scss" scoped> |
|
240 |
:deep(.anticon) { |
|
241 |
&:hover { |
|
242 |
color: var(--el-color-primary) !important; |
|
243 |
} |
|
244 |
} |
|
245 |
|
|
246 |
.login-code { |
|
247 |
float: right; |
|
248 |
width: 100%; |
|
249 |
height: 38px; |
|
250 |
|
|
251 |
img { |
|
252 |
width: 100%; |
|
253 |
height: auto; |
|
254 |
max-width: 100px; |
|
255 |
vertical-align: middle; |
|
256 |
cursor: pointer; |
|
257 |
} |
|
258 |
} |
|
259 |
</style> |