login.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <dv-full-screen-container>
  3. <div class="login">
  4. <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
  5. <h3 class="title">{{ title }}</h3>
  6. <el-form-item prop="username">
  7. <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
  8. <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
  9. </el-input>
  10. </el-form-item>
  11. <el-form-item prop="password">
  12. <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
  13. @keyup.enter="handleLogin">
  14. <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
  15. </el-input>
  16. </el-form-item>
  17. <el-form-item prop="code" v-if="captchaEnabled">
  18. <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%"
  19. @keyup.enter="handleLogin">
  20. <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
  21. </el-input>
  22. <div class="login-code">
  23. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  24. </div>
  25. </el-form-item>
  26. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  27. <el-form-item style="width:100%;">
  28. <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
  29. <span v-if="!loading">登 录</span>
  30. <span v-else>登 录 中...</span>
  31. </el-button>
  32. <div style="float: right;" v-if="register">
  33. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  34. </div>
  35. </el-form-item>
  36. </el-form>
  37. <!-- 底部 -->
  38. <div class="el-login-footer">
  39. <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
  40. </div>
  41. </div>
  42. </dv-full-screen-container>
  43. </template>
  44. <script setup>
  45. import { getCodeImg } from "@/api/login";
  46. import Cookies from "js-cookie";
  47. import { encrypt, decrypt } from "@/utils/jsencrypt";
  48. import useUserStore from '@/store/modules/user'
  49. const title = import.meta.env.VITE_APP_TITLE;
  50. const userStore = useUserStore();
  51. const route = useRoute();
  52. const router = useRouter();
  53. const { proxy } = getCurrentInstance();
  54. const loginForm = ref({
  55. username: "admin",
  56. password: "admin123",
  57. rememberMe: false,
  58. code: "",
  59. uuid: ""
  60. });
  61. const loginRules = {
  62. username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  63. password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  64. code: [{ required: true, trigger: "change", message: "请输入验证码" }]
  65. };
  66. const codeUrl = ref("");
  67. const loading = ref(false);
  68. // 验证码开关
  69. const captchaEnabled = ref(true);
  70. // 注册开关
  71. const register = ref(false);
  72. const redirect = ref(undefined);
  73. watch(route, (newRoute) => {
  74. redirect.value = newRoute.query && newRoute.query.redirect;
  75. }, { immediate: true });
  76. function handleLogin() {
  77. proxy.$refs.loginRef.validate(valid => {
  78. if (valid) {
  79. loading.value = true;
  80. // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
  81. if (loginForm.value.rememberMe) {
  82. Cookies.set("username", loginForm.value.username, { expires: 30 });
  83. Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
  84. Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
  85. } else {
  86. // 否则移除
  87. Cookies.remove("username");
  88. Cookies.remove("password");
  89. Cookies.remove("rememberMe");
  90. }
  91. // 调用action的登录方法
  92. userStore.login(loginForm.value).then(() => {
  93. const query = route.query;
  94. const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
  95. if (cur !== "redirect") {
  96. acc[cur] = query[cur];
  97. }
  98. return acc;
  99. }, {});
  100. router.push({ path: redirect.value || "/", query: otherQueryParams });
  101. }).catch(() => {
  102. loading.value = false;
  103. // 重新获取验证码
  104. if (captchaEnabled.value) {
  105. getCode();
  106. }
  107. });
  108. }
  109. });
  110. }
  111. function getCode() {
  112. getCodeImg().then(res => {
  113. captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
  114. if (captchaEnabled.value) {
  115. codeUrl.value = "data:image/gif;base64," + res.img;
  116. loginForm.value.uuid = res.uuid;
  117. }
  118. });
  119. }
  120. function getCookie() {
  121. const username = Cookies.get("username");
  122. const password = Cookies.get("password");
  123. const rememberMe = Cookies.get("rememberMe");
  124. loginForm.value = {
  125. username: username === undefined ? loginForm.value.username : username,
  126. password: password === undefined ? loginForm.value.password : decrypt(password),
  127. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  128. };
  129. }
  130. getCode();
  131. getCookie();
  132. </script>
  133. <style lang='scss' scoped>
  134. .login {
  135. display: flex;
  136. justify-content: center;
  137. align-items: center;
  138. height: 100%;
  139. background: url("@/assets/images/login-bg.svg");
  140. background-size: cover;
  141. }
  142. .title {
  143. margin: 0px auto 30px auto;
  144. text-align: center;
  145. color: #707070;
  146. }
  147. .login-form {
  148. border-radius: 6px;
  149. background: #ffffff;
  150. width: 400px;
  151. padding: 25px 25px 5px 25px;
  152. box-shadow: var(--el-box-shadow-light);
  153. .el-input {
  154. height: 40px;
  155. input {
  156. height: 40px;
  157. }
  158. }
  159. .input-icon {
  160. height: 39px;
  161. width: 14px;
  162. margin-left: 0px;
  163. }
  164. }
  165. .login-tip {
  166. font-size: 13px;
  167. text-align: center;
  168. color: #bfbfbf;
  169. }
  170. .login-code {
  171. width: 33%;
  172. height: 40px;
  173. float: right;
  174. img {
  175. cursor: pointer;
  176. vertical-align: middle;
  177. }
  178. }
  179. .el-login-footer {
  180. height: 40px;
  181. line-height: 40px;
  182. position: fixed;
  183. bottom: 0;
  184. width: 100%;
  185. text-align: center;
  186. color: #fff;
  187. font-family: Arial;
  188. font-size: 12px;
  189. letter-spacing: 1px;
  190. }
  191. .login-code-img {
  192. height: 40px;
  193. padding-left: 12px;
  194. }
  195. </style>