login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <div class="login" v-loading="pageLoading" element-loading-background="rgba(0,0,0,0.6)">
  3. <template v-if="!pageLoading">
  4. <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
  5. <div class="title-box">
  6. <h3 class="title">{{ title }}</h3>
  7. <lang-select />
  8. </div>
  9. <el-form-item v-if="tenantEnabled" prop="tenantId">
  10. <el-select v-model="loginForm.tenantId" filterable :placeholder="proxy.$t('login.selectPlaceholder')"
  11. style="width: 100%">
  12. <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.companyName"
  13. :value="item.tenantId"></el-option>
  14. <template #prefix><svg-icon icon-class="company" class="el-input__icon input-icon" /></template>
  15. </el-select>
  16. </el-form-item>
  17. <el-form-item prop="username">
  18. <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off"
  19. :placeholder="proxy.$t('login.username')">
  20. <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item prop="password">
  24. <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off"
  25. :placeholder="proxy.$t('login.password')" @keyup.enter="handleLogin">
  26. <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
  27. </el-input>
  28. </el-form-item>
  29. <el-form-item v-if="captchaEnabled" prop="code">
  30. <el-input v-model="loginForm.code" size="large" auto-complete="off" :placeholder="proxy.$t('login.code')"
  31. style="width: 63%" @keyup.enter="handleLogin">
  32. <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
  33. </el-input>
  34. <div class="login-code">
  35. <img :src="codeUrl" class="login-code-img" @click="getCode" />
  36. </div>
  37. </el-form-item>
  38. <el-checkbox v-model="loginForm.rememberMe" style="margin: 0 0 25px 0">{{ proxy.$t('login.rememberPassword')
  39. }}</el-checkbox>
  40. <el-form-item style="width: 100%">
  41. <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin">
  42. <span v-if="!loading">{{ proxy.$t('login.login') }}</span>
  43. <span v-else>{{ proxy.$t('login.logging') }}</span>
  44. </el-button>
  45. </el-form-item>
  46. </el-form>
  47. <div class="el-login-footer">
  48. <span>Copyright © 2018-2026 疯狂的狮子Li All Rights Reserved.</span>
  49. </div>
  50. </template>
  51. </div>
  52. </template>
  53. <script setup lang="ts">
  54. import { getCodeImg, getTenantList } from '@/api/login';
  55. import { authRouterUrl } from '@/api/system/social/auth';
  56. import { useUserStore } from '@/store/modules/user';
  57. import { LoginData, TenantVO } from '@/api/types';
  58. import { to } from 'await-to-js';
  59. import { HttpStatus } from '@/enums/RespEnum';
  60. import { useI18n } from 'vue-i18n';
  61. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  62. const title = import.meta.env.VITE_APP_TITLE;
  63. const userStore = useUserStore();
  64. const router = useRouter();
  65. const { t } = useI18n();
  66. const loginForm = ref<LoginData>({
  67. tenantId: '000000',
  68. username: 'admin',
  69. password: 'admin123',
  70. rememberMe: false,
  71. code: '',
  72. uuid: ''
  73. } as LoginData);
  74. const loginRules: ElFormRules = {
  75. tenantId: [{ required: true, trigger: 'blur', message: t('login.rule.tenantId.required') }],
  76. username: [{ required: true, trigger: 'blur', message: t('login.rule.username.required') }],
  77. password: [{ required: true, trigger: 'blur', message: t('login.rule.password.required') }],
  78. code: [{ required: true, trigger: 'change', message: t('login.rule.code.required') }]
  79. };
  80. const codeUrl = ref('');
  81. const loading = ref(false);
  82. const pageLoading = ref(true);
  83. // 验证码开关
  84. const captchaEnabled = ref(true);
  85. // 租户开关
  86. const tenantEnabled = ref(true);
  87. // 注册开关
  88. const register = ref(false);
  89. const redirect = ref('/');
  90. const loginRef = ref<ElFormInstance>();
  91. // 租户列表
  92. const tenantList = ref<TenantVO[]>([]);
  93. watch(
  94. () => router.currentRoute.value,
  95. (newRoute: any) => {
  96. redirect.value = newRoute.query && newRoute.query.redirect && decodeURIComponent(newRoute.query.redirect);
  97. },
  98. { immediate: true }
  99. );
  100. const handleLogin = () => {
  101. loginRef.value?.validate(async (valid: boolean, fields: any) => {
  102. if (valid) {
  103. loading.value = true;
  104. // 勾选了需要记住密码设置在 localStorage 中设置记住用户名和密码
  105. if (loginForm.value.rememberMe) {
  106. localStorage.setItem('tenantId', String(loginForm.value.tenantId));
  107. localStorage.setItem('username', String(loginForm.value.username));
  108. localStorage.setItem('password', String(loginForm.value.password));
  109. localStorage.setItem('rememberMe', String(loginForm.value.rememberMe));
  110. } else {
  111. // 否则移除
  112. localStorage.removeItem('tenantId');
  113. localStorage.removeItem('username');
  114. localStorage.removeItem('password');
  115. localStorage.removeItem('rememberMe');
  116. }
  117. // 调用action的登录方法
  118. const [err] = await to(userStore.login(loginForm.value));
  119. if (!err) {
  120. const redirectUrl = redirect.value || '/';
  121. await router.push(redirectUrl);
  122. loading.value = false;
  123. } else {
  124. loading.value = false;
  125. // 重新获取验证码
  126. if (captchaEnabled.value) {
  127. await getCode();
  128. }
  129. }
  130. } else {
  131. console.log('error submit!', fields);
  132. }
  133. });
  134. };
  135. /**
  136. * 获取验证码
  137. */
  138. const getCode = async () => {
  139. const res = await getCodeImg();
  140. const { data } = res;
  141. captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
  142. if (captchaEnabled.value) {
  143. // 刷新验证码时清空输入框
  144. loginForm.value.code = '';
  145. codeUrl.value = 'data:image/gif;base64,' + data.img;
  146. loginForm.value.uuid = data.uuid;
  147. }
  148. };
  149. const getLoginData = () => {
  150. const tenantId = localStorage.getItem('tenantId');
  151. const username = localStorage.getItem('username');
  152. const password = localStorage.getItem('password');
  153. const rememberMe = localStorage.getItem('rememberMe');
  154. loginForm.value = {
  155. tenantId: tenantId === null ? String(loginForm.value.tenantId) : tenantId,
  156. username: username === null ? String(loginForm.value.username) : username,
  157. password: password === null ? String(loginForm.value.password) : String(password),
  158. rememberMe: rememberMe === null ? false : Boolean(rememberMe)
  159. } as LoginData;
  160. };
  161. /**
  162. * 获取租户列表
  163. */
  164. const initTenantList = async () => {
  165. const { data } = await getTenantList(false);
  166. tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  167. if (tenantEnabled.value) {
  168. tenantList.value = data.voList;
  169. if (tenantList.value != null && tenantList.value.length !== 0) {
  170. loginForm.value.tenantId = tenantList.value[0].tenantId;
  171. }
  172. }
  173. };
  174. /**
  175. * 第三方登录
  176. * @param type
  177. */
  178. const doSocialLogin = (type: string) => {
  179. authRouterUrl(type, loginForm.value.tenantId).then((res: any) => {
  180. if (res.code === HttpStatus.SUCCESS) {
  181. // 获取授权地址跳转
  182. window.location.href = res.data;
  183. } else {
  184. ElMessage.error(res.msg);
  185. }
  186. });
  187. };
  188. onMounted(async () => {
  189. pageLoading.value = true;
  190. await Promise.all([
  191. getCode(),
  192. initTenantList(),
  193. getLoginData()
  194. ]);
  195. pageLoading.value = false;
  196. });
  197. </script>
  198. <style lang="scss" scoped>
  199. .login {
  200. display: flex;
  201. justify-content: center;
  202. align-items: center;
  203. height: 100%;
  204. background-image: url('../assets/images/login-background.jpg');
  205. background-size: cover;
  206. background-position: center;
  207. }
  208. .title-box {
  209. display: flex;
  210. align-items: center;
  211. gap: 8px;
  212. .title {
  213. margin: 0px auto 26px auto;
  214. text-align: center;
  215. color: var(--el-text-color-primary);
  216. font-weight: 600;
  217. letter-spacing: 0.5px;
  218. }
  219. :deep(.lang-select--style) {
  220. line-height: 0;
  221. color: var(--el-text-color-secondary);
  222. }
  223. }
  224. .login-form {
  225. border-radius: var(--app-radius-lg);
  226. background: rgba(255, 255, 255, 0.94);
  227. border: 1px solid rgba(255, 255, 255, 0.5);
  228. width: min(420px, 90vw);
  229. padding: 32px 30px 12px 30px;
  230. z-index: 1;
  231. box-shadow: var(--app-shadow-lg);
  232. backdrop-filter: blur(6px);
  233. -webkit-backdrop-filter: blur(6px);
  234. .el-input {
  235. height: 40px;
  236. input {
  237. height: 40px;
  238. }
  239. }
  240. .input-icon {
  241. height: 39px;
  242. width: 14px;
  243. margin-left: 0px;
  244. }
  245. }
  246. .login-tip {
  247. font-size: 13px;
  248. text-align: center;
  249. color: #bfbfbf;
  250. }
  251. .login-form :deep(.el-input__wrapper) {
  252. background-color: rgba(255, 255, 255, 0.9);
  253. }
  254. .login-form :deep(.el-input__wrapper.is-focus) {
  255. box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
  256. }
  257. .login-form :deep(.el-button--primary) {
  258. border-radius: var(--app-radius-md);
  259. box-shadow: 0 8px 20px rgba(59, 130, 246, 0.25);
  260. }
  261. .login-form :deep(.el-button.is-circle) {
  262. background: rgba(15, 23, 42, 0.04);
  263. border: 1px solid rgba(15, 23, 42, 0.08);
  264. color: var(--el-text-color-regular);
  265. }
  266. .login-form :deep(.el-button.is-circle:hover) {
  267. background: rgba(59, 130, 246, 0.1);
  268. border-color: rgba(59, 130, 246, 0.2);
  269. }
  270. .login-code {
  271. width: calc(37% - 10px);
  272. height: 40px;
  273. float: right;
  274. margin-left: 10px;
  275. box-sizing: border-box;
  276. border-radius: var(--app-radius-sm);
  277. overflow: hidden;
  278. background: rgba(255, 255, 255, 0.9);
  279. border: 1px solid var(--el-border-color-light);
  280. img {
  281. cursor: pointer;
  282. vertical-align: middle;
  283. display: block;
  284. width: 100%;
  285. height: 40px;
  286. object-fit: cover;
  287. }
  288. }
  289. .el-login-footer {
  290. height: 40px;
  291. line-height: 40px;
  292. position: fixed;
  293. bottom: 0;
  294. width: 100%;
  295. text-align: center;
  296. color: rgba(255, 255, 255, 0.75);
  297. font-family: Arial, serif;
  298. font-size: 12px;
  299. letter-spacing: 1px;
  300. }
  301. .login-code-img {
  302. height: 40px;
  303. padding-left: 0;
  304. }
  305. :global(html.dark) {
  306. .login-form {
  307. background: rgba(17, 24, 39, 0.9);
  308. border-color: rgba(148, 163, 184, 0.2);
  309. }
  310. .login-form :deep(.el-input__wrapper) {
  311. background-color: rgba(17, 24, 39, 0.7);
  312. }
  313. .login-form :deep(.el-button.is-circle) {
  314. background: rgba(148, 163, 184, 0.12);
  315. border-color: rgba(148, 163, 184, 0.25);
  316. color: #e5e7eb;
  317. }
  318. .el-login-footer {
  319. color: rgba(226, 232, 240, 0.65);
  320. }
  321. }
  322. </style>