login.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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="width:100%;">
  31. <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
  32. <span v-if="!loading">登 录</span>
  33. <span v-else>登 录 中...</span>
  34. </el-button>
  35. <div style="float: right;" v-if="register">
  36. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  37. </div>
  38. </el-form-item>
  39. <div style="display: flex;justify-content: flex-end;flex-direction: row;">
  40. <el-button circle>
  41. <svg-icon icon-class="qq" @click="doSocialLogin('qq')" />
  42. </el-button>
  43. <el-button circle>
  44. <svg-icon icon-class="wechat" @click="doSocialLogin('wechat')" />
  45. </el-button>
  46. <el-button circle>
  47. <svg-icon icon-class="gitee" @click="doSocialLogin('gitee')" />
  48. </el-button>
  49. <el-button circle>
  50. <svg-icon icon-class="github" @click="doSocialLogin('github')" />
  51. </el-button>
  52. </div>
  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 { ElForm, FormRules } from 'element-plus';
  68. import { to } from 'await-to-js';
  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: FormRules = {
  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(ElForm);
  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(encrypt(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. // prittier-ignore
  116. const [err] = await to(userStore.login(loginForm.value));
  117. if (!err) {
  118. await router.push({ path: redirect.value || '/' });
  119. } else {
  120. loading.value = false;
  121. // 重新获取验证码
  122. if (captchaEnabled.value) {
  123. await getCode();
  124. }
  125. }
  126. } else {
  127. console.log('error submit!', fields);
  128. }
  129. });
  130. };
  131. /**
  132. * 获取验证码
  133. */
  134. const getCode = async () => {
  135. const res = await getCodeImg();
  136. const { data } = res;
  137. captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
  138. if (captchaEnabled.value) {
  139. codeUrl.value = 'data:image/gif;base64,' + data.img;
  140. loginForm.value.uuid = data.uuid;
  141. }
  142. };
  143. const getCookie = () => {
  144. const tenantId = Cookies.get("tenantId");
  145. const username = Cookies.get('username');
  146. const password = Cookies.get('password');
  147. const rememberMe = Cookies.get('rememberMe');
  148. loginForm.value = {
  149. tenantId: tenantId === undefined ? loginForm.value.tenantId : tenantId,
  150. username: username === undefined ? loginForm.value.username : username,
  151. password: password === undefined ? loginForm.value.password : (decrypt(password) as string),
  152. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  153. };
  154. }
  155. /**
  156. * 获取租户列表
  157. */
  158. const initTenantList = async () => {
  159. const { data } = await getTenantList();
  160. tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  161. if (tenantEnabled.value) {
  162. tenantList.value = data.voList;
  163. if (tenantList.value != null && tenantList.value.length !== 0) {
  164. loginForm.value.tenantId = tenantList.value[0].tenantId;
  165. }
  166. }
  167. }
  168. /**
  169. * 第三方登录
  170. * @param type
  171. */
  172. const doSocialLogin = (type: string) => {
  173. authBinding(type).then((res: any) => {
  174. if (res.code === 200) {
  175. window.location.href = res.msg;
  176. } else {
  177. ElMessage.error(res.msg);
  178. }
  179. });
  180. };
  181. onMounted(() => {
  182. getCode();
  183. initTenantList();
  184. getCookie();
  185. });
  186. </script>
  187. <style lang="scss" scoped>
  188. .login {
  189. display: flex;
  190. justify-content: center;
  191. align-items: center;
  192. height: 100%;
  193. background-image: url("../assets/images/login-background.jpg");
  194. background-size: cover;
  195. }
  196. .title {
  197. margin: 0px auto 30px auto;
  198. text-align: center;
  199. color: #707070;
  200. }
  201. .login-form {
  202. border-radius: 6px;
  203. background: #ffffff;
  204. width: 400px;
  205. padding: 25px 25px 5px 25px;
  206. .el-input {
  207. height: 40px;
  208. input {
  209. height: 40px;
  210. }
  211. }
  212. .input-icon {
  213. height: 39px;
  214. width: 14px;
  215. margin-left: 0px;
  216. }
  217. }
  218. .login-tip {
  219. font-size: 13px;
  220. text-align: center;
  221. color: #bfbfbf;
  222. }
  223. .login-code {
  224. width: 33%;
  225. height: 40px;
  226. float: right;
  227. img {
  228. cursor: pointer;
  229. vertical-align: middle;
  230. }
  231. }
  232. .el-login-footer {
  233. height: 40px;
  234. line-height: 40px;
  235. position: fixed;
  236. bottom: 0;
  237. width: 100%;
  238. text-align: center;
  239. color: #fff;
  240. font-family: Arial, serif;
  241. font-size: 12px;
  242. letter-spacing: 1px;
  243. }
  244. .login-code-img {
  245. height: 40px;
  246. padding-left: 12px;
  247. }
  248. </style>