login.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
  4. <div class="title-box">
  5. <h3 class="title">RuoYi-Vue-Plus多租户管理系统</h3>
  6. <lang-select />
  7. </div>
  8. <el-form-item v-if="tenantEnabled" prop="tenantId">
  9. <el-select v-model="loginForm.tenantId" filterable :placeholder="$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="$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="$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="$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">{{ $t('login.rememberPassword') }}</el-checkbox>
  47. <el-form-item style="float: right">
  48. <el-button circle :title="$t('login.social.wechat')" @click="doSocialLogin('wechat')">
  49. <svg-icon icon-class="wechat" />
  50. </el-button>
  51. <el-button circle :title="$t('login.social.maxkey')" @click="doSocialLogin('maxkey')">
  52. <svg-icon icon-class="maxkey" />
  53. </el-button>
  54. <el-button circle :title="$t('login.social.topiam')" @click="doSocialLogin('topiam')">
  55. <svg-icon icon-class="topiam" />
  56. </el-button>
  57. <el-button circle :title="$t('login.social.gitee')" @click="doSocialLogin('gitee')">
  58. <svg-icon icon-class="gitee" />
  59. </el-button>
  60. <el-button circle :title="$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">{{ $t('login.login') }}</span>
  67. <span v-else>{{ $t('login.logging') }}</span>
  68. </el-button>
  69. <div v-if="register" style="float: right">
  70. <router-link class="link-type" :to="'/register'">{{ $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-2024 疯狂的狮子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 { authBinding } from '@/api/system/social/auth';
  83. import { useUserStore } from '@/store/modules/user';
  84. import { LoginData, TenantVO } from '@/api/types';
  85. import { to } from 'await-to-js';
  86. import { HttpStatus } from '@/enums/RespEnum';
  87. import { useI18n } from 'vue-i18n';
  88. const userStore = useUserStore();
  89. const router = useRouter();
  90. const { t } = useI18n();
  91. const loginForm = ref<LoginData>({
  92. tenantId: '000000',
  93. username: 'admin',
  94. password: 'admin123',
  95. rememberMe: false,
  96. code: '',
  97. uuid: ''
  98. } as LoginData);
  99. const loginRules: ElFormRules = {
  100. tenantId: [{ required: true, trigger: 'blur', message: t('login.rule.tenantId.required') }],
  101. username: [{ required: true, trigger: 'blur', message: t('login.rule.username.required') }],
  102. password: [{ required: true, trigger: 'blur', message: t('login.rule.password.required') }],
  103. code: [{ required: true, trigger: 'change', message: t('login.rule.code.required') }]
  104. };
  105. const codeUrl = ref('');
  106. const loading = ref(false);
  107. // 验证码开关
  108. const captchaEnabled = ref(true);
  109. // 租户开关
  110. const tenantEnabled = ref(true);
  111. // 注册开关
  112. const register = ref(false);
  113. const redirect = ref('/');
  114. const loginRef = ref<ElFormInstance>();
  115. // 租户列表
  116. const tenantList = ref<TenantVO[]>([]);
  117. watch(
  118. () => router.currentRoute.value,
  119. (newRoute: any) => {
  120. redirect.value = newRoute.query && decodeURIComponent(newRoute.query.redirect);
  121. },
  122. { immediate: true }
  123. );
  124. const handleLogin = () => {
  125. loginRef.value?.validate(async (valid: boolean, fields: any) => {
  126. if (valid) {
  127. loading.value = true;
  128. // 勾选了需要记住密码设置在 localStorage 中设置记住用户名和密码
  129. if (loginForm.value.rememberMe) {
  130. localStorage.setItem('tenantId', String(loginForm.value.tenantId));
  131. localStorage.setItem('username', String(loginForm.value.username));
  132. localStorage.setItem('password', String(loginForm.value.password));
  133. localStorage.setItem('rememberMe', String(loginForm.value.rememberMe));
  134. } else {
  135. // 否则移除
  136. localStorage.removeItem('tenantId');
  137. localStorage.removeItem('username');
  138. localStorage.removeItem('password');
  139. localStorage.removeItem('rememberMe');
  140. }
  141. // 调用action的登录方法
  142. const [err] = await to(userStore.login(loginForm.value));
  143. if (!err) {
  144. const redirectUrl = redirect.value || '/';
  145. await router.push(redirectUrl);
  146. loading.value = false;
  147. } else {
  148. loading.value = false;
  149. // 重新获取验证码
  150. if (captchaEnabled.value) {
  151. await getCode();
  152. }
  153. }
  154. } else {
  155. console.log('error submit!', fields);
  156. }
  157. });
  158. };
  159. /**
  160. * 获取验证码
  161. */
  162. const getCode = async () => {
  163. const res = await getCodeImg();
  164. const { data } = res;
  165. captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
  166. if (captchaEnabled.value) {
  167. codeUrl.value = 'data:image/gif;base64,' + data.img;
  168. loginForm.value.uuid = data.uuid;
  169. }
  170. };
  171. const getLoginData = () => {
  172. const tenantId = localStorage.getItem('tenantId');
  173. const username = localStorage.getItem('username');
  174. const password = localStorage.getItem('password');
  175. const rememberMe = localStorage.getItem('rememberMe');
  176. loginForm.value = {
  177. tenantId: tenantId === null ? String(loginForm.value.tenantId) : tenantId,
  178. username: username === null ? String(loginForm.value.username) : username,
  179. password: password === null ? String(loginForm.value.password) : String(password),
  180. rememberMe: rememberMe === null ? false : Boolean(rememberMe)
  181. } as LoginData;
  182. };
  183. /**
  184. * 获取租户列表
  185. */
  186. const initTenantList = async () => {
  187. const { data } = await getTenantList(false);
  188. tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  189. if (tenantEnabled.value) {
  190. tenantList.value = data.voList;
  191. if (tenantList.value != null && tenantList.value.length !== 0) {
  192. loginForm.value.tenantId = tenantList.value[0].tenantId;
  193. }
  194. }
  195. };
  196. /**
  197. * 第三方登录
  198. * @param type
  199. */
  200. const doSocialLogin = (type: string) => {
  201. authBinding(type, loginForm.value.tenantId).then((res: any) => {
  202. if (res.code === HttpStatus.SUCCESS) {
  203. // 获取授权地址跳转
  204. window.location.href = res.data;
  205. } else {
  206. ElMessage.error(res.msg);
  207. }
  208. });
  209. };
  210. onMounted(() => {
  211. getCode();
  212. initTenantList();
  213. getLoginData();
  214. });
  215. </script>
  216. <style lang="scss" scoped>
  217. .login {
  218. display: flex;
  219. justify-content: center;
  220. align-items: center;
  221. height: 100%;
  222. background-image: url('../assets/images/login-background.jpg');
  223. background-size: cover;
  224. }
  225. .title-box {
  226. display: flex;
  227. .title {
  228. margin: 0px auto 30px auto;
  229. text-align: center;
  230. color: #707070;
  231. }
  232. :deep(.lang-select--style) {
  233. line-height: 0;
  234. color: #7483a3;
  235. }
  236. }
  237. .login-form {
  238. border-radius: 6px;
  239. background: #ffffff;
  240. width: 400px;
  241. padding: 25px 25px 5px 25px;
  242. .el-input {
  243. height: 40px;
  244. input {
  245. height: 40px;
  246. }
  247. }
  248. .input-icon {
  249. height: 39px;
  250. width: 14px;
  251. margin-left: 0px;
  252. }
  253. }
  254. .login-tip {
  255. font-size: 13px;
  256. text-align: center;
  257. color: #bfbfbf;
  258. }
  259. .login-code {
  260. width: 33%;
  261. height: 40px;
  262. float: right;
  263. img {
  264. cursor: pointer;
  265. vertical-align: middle;
  266. }
  267. }
  268. .el-login-footer {
  269. height: 40px;
  270. line-height: 40px;
  271. position: fixed;
  272. bottom: 0;
  273. width: 100%;
  274. text-align: center;
  275. color: #fff;
  276. font-family: Arial, serif;
  277. font-size: 12px;
  278. letter-spacing: 1px;
  279. }
  280. .login-code-img {
  281. height: 40px;
  282. padding-left: 12px;
  283. }
  284. </style>