login.vue 8.3 KB

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