login.vue 10 KB

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