login.vue 8.7 KB

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