login.vue 8.3 KB

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