login.vue 8.5 KB

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