login.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <div class="login">
  3. <bgTools />
  4. <el-form
  5. ref="loginRef"
  6. :model="loginForm"
  7. :rules="loginRules"
  8. class="login-form"
  9. >
  10. <h3 class="title">数字化图鉴</h3>
  11. <div class="login-content">
  12. <div class="login-welcome">早上好,欢迎登录!</div>
  13. <div class="login-welcome-en">Good morning, welcome to login!</div>
  14. <el-form-item prop="username">
  15. <el-input
  16. v-model="loginForm.username"
  17. type="text"
  18. size="large"
  19. auto-complete="off"
  20. placeholder="账号"
  21. >
  22. <template #prefix
  23. ><svg-icon icon-class="user" class="el-input__icon input-icon"
  24. /></template>
  25. </el-input>
  26. </el-form-item>
  27. <el-form-item prop="password">
  28. <el-input
  29. v-model="loginForm.password"
  30. type="password"
  31. size="large"
  32. auto-complete="off"
  33. placeholder="密码"
  34. @keyup.enter="handleLogin"
  35. >
  36. <template #prefix
  37. ><svg-icon
  38. icon-class="password"
  39. class="el-input__icon input-icon"
  40. /></template>
  41. </el-input>
  42. </el-form-item>
  43. <el-form-item prop="code" v-if="captchaEnabled">
  44. <el-input
  45. v-model="loginForm.code"
  46. size="large"
  47. auto-complete="off"
  48. placeholder="验证码"
  49. style="width: 63%"
  50. @keyup.enter="handleLogin"
  51. >
  52. <template #prefix
  53. ><svg-icon
  54. icon-class="validCode"
  55. class="el-input__icon input-icon"
  56. /></template>
  57. </el-input>
  58. <div class="login-code">
  59. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  60. </div>
  61. </el-form-item>
  62. <el-checkbox
  63. v-model="loginForm.rememberMe"
  64. style="margin: 0px 0px 25px 0px"
  65. >记住密码</el-checkbox
  66. >
  67. <el-form-item style="width: 100%">
  68. <el-button
  69. :loading="loading"
  70. size="large"
  71. type="primary"
  72. style="width: 100%"
  73. @click.prevent="handleLogin"
  74. >
  75. <span v-if="!loading">登 录</span>
  76. <span v-else>登 录 中...</span>
  77. </el-button>
  78. <div style="float: right" v-if="register">
  79. <router-link class="link-type" :to="'/register'"
  80. >立即注册</router-link
  81. >
  82. </div>
  83. </el-form-item>
  84. </div>
  85. </el-form>
  86. </div>
  87. </template>
  88. <script setup>
  89. import { getCodeImg } from "@/api/login";
  90. import Cookies from "js-cookie";
  91. import { encrypt, decrypt } from "@/utils/jsencrypt";
  92. import useUserStore from "@/store/modules/user";
  93. import bgTools from "./components/bgTools";
  94. const userStore = useUserStore();
  95. const route = useRoute();
  96. const router = useRouter();
  97. const { proxy } = getCurrentInstance();
  98. const loginForm = ref({
  99. username: "admin",
  100. password: "admin123",
  101. rememberMe: false,
  102. code: "",
  103. uuid: "",
  104. });
  105. const loginRules = {
  106. username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  107. password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  108. code: [{ required: true, trigger: "change", message: "请输入验证码" }],
  109. };
  110. const codeUrl = ref("");
  111. const loading = ref(false);
  112. // 验证码开关
  113. const captchaEnabled = ref(true);
  114. // 注册开关
  115. const register = ref(false);
  116. const redirect = ref(undefined);
  117. watch(
  118. route,
  119. (newRoute) => {
  120. redirect.value = newRoute.query && newRoute.query.redirect;
  121. },
  122. { immediate: true }
  123. );
  124. function handleLogin() {
  125. proxy.$refs.loginRef.validate((valid) => {
  126. if (valid) {
  127. loading.value = true;
  128. // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
  129. if (loginForm.value.rememberMe) {
  130. Cookies.set("username", loginForm.value.username, { expires: 30 });
  131. Cookies.set("password", encrypt(loginForm.value.password), {
  132. expires: 30,
  133. });
  134. Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
  135. } else {
  136. // 否则移除
  137. Cookies.remove("username");
  138. Cookies.remove("password");
  139. Cookies.remove("rememberMe");
  140. }
  141. // 调用action的登录方法
  142. userStore
  143. .login(loginForm.value)
  144. .then(() => {
  145. const query = route.query;
  146. const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
  147. if (cur !== "redirect") {
  148. acc[cur] = query[cur];
  149. }
  150. return acc;
  151. }, {});
  152. router.push({ path: redirect.value || "/", query: otherQueryParams });
  153. })
  154. .catch(() => {
  155. loading.value = false;
  156. // 重新获取验证码
  157. if (captchaEnabled.value) {
  158. getCode();
  159. }
  160. });
  161. }
  162. });
  163. }
  164. function getCode() {
  165. getCodeImg().then((res) => {
  166. captchaEnabled.value =
  167. res.captchaEnabled === undefined ? true : res.captchaEnabled;
  168. if (captchaEnabled.value) {
  169. codeUrl.value = "data:image/gif;base64," + res.img;
  170. loginForm.value.uuid = res.uuid;
  171. }
  172. });
  173. }
  174. function getCookie() {
  175. const username = Cookies.get("username");
  176. const password = Cookies.get("password");
  177. const rememberMe = Cookies.get("rememberMe");
  178. loginForm.value = {
  179. username: username === undefined ? loginForm.value.username : username,
  180. password:
  181. password === undefined ? loginForm.value.password : decrypt(password),
  182. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  183. };
  184. }
  185. getCode();
  186. getCookie();
  187. </script>
  188. <style lang="scss" scoped>
  189. $greenSeaweed: rgb(9, 88, 255);
  190. $blueQueen: rgb(21, 121, 255);
  191. .login {
  192. width: 100vw;
  193. height: 100vh;
  194. overflow: hidden;
  195. display: flex;
  196. justify-content: flex-end;
  197. align-items: center;
  198. height: 100%;
  199. background-image: url("../assets/images/login-background.png");
  200. background-size: cover;
  201. position: relative;
  202. z-index: 2;
  203. }
  204. .title {
  205. margin: 0px auto 30px auto;
  206. text-align: center;
  207. color: #121212;
  208. font-weight: 600;
  209. background: rgba($color: #ffffff, $alpha: 0.6);
  210. box-shadow: 0px 2px 10px 0px rgba(39, 54, 100, 0.1);
  211. border-radius: 10px 10px 0px 0px;
  212. font-size: 42px;
  213. color: #004ea2;
  214. line-height: 42px;
  215. letter-spacing: 2px;
  216. padding: 20px;
  217. font-style: italic;
  218. }
  219. .login-content {
  220. width: 100%;
  221. box-sizing: border-box;
  222. padding: 42px 60px;
  223. background: rgba(255, 255, 255, 0.6);
  224. box-shadow: 0px 2px 10px 0px rgba(39, 54, 100, 0.1);
  225. backdrop-filter: blur(8px);
  226. .login-welcome{
  227. font-family: Helvetica;
  228. font-size: 24px;
  229. color: #004EA2;
  230. line-height: 32px;
  231. }
  232. .login-welcome-en{
  233. font-weight: 500;
  234. font-size: 18px;
  235. color: rgba(#326fb0, 0.8);
  236. line-height: 32px;
  237. margin-bottom: 32px;
  238. }
  239. &::before,
  240. &::after {
  241. content: "";
  242. position: absolute;
  243. width: 600px;
  244. height: 600px;
  245. border-top-left-radius: 40%;
  246. border-top-right-radius: 45%;
  247. border-bottom-left-radius: 35%;
  248. border-bottom-right-radius: 40%;
  249. z-index: -1;
  250. }
  251. &::before {
  252. left: 45%;
  253. bottom: -75%;
  254. background-color: rgba($blueQueen,0.15);
  255. animation: wawes 10s infinite linear;
  256. }
  257. &::after {
  258. left: 40%;
  259. bottom: -70%;
  260. background-color: rgba($greenSeaweed, 0.2);
  261. animation: wawes 7s infinite;
  262. }
  263. }
  264. .login-form {
  265. width: 480px;
  266. overflow: hidden;
  267. position: relative;
  268. z-index: 2;
  269. right: 8%;
  270. @keyframes wawes {
  271. from {
  272. transform: rotate(0);
  273. }
  274. to {
  275. transform: rotate(360deg);
  276. }
  277. }
  278. .el-input {
  279. height: 40px;
  280. input {
  281. height: 40px;
  282. }
  283. }
  284. .input-icon {
  285. height: 39px;
  286. width: 14px;
  287. margin-left: 0px;
  288. }
  289. }
  290. .login-tip {
  291. font-size: 13px;
  292. text-align: center;
  293. color: #bfbfbf;
  294. }
  295. .login-code {
  296. width: 33%;
  297. height: 40px;
  298. float: right;
  299. img {
  300. cursor: pointer;
  301. vertical-align: middle;
  302. }
  303. }
  304. .el-login-footer {
  305. height: 40px;
  306. line-height: 40px;
  307. position: fixed;
  308. bottom: 0;
  309. width: 100%;
  310. text-align: center;
  311. color: #fff;
  312. font-family: Arial;
  313. font-size: 12px;
  314. letter-spacing: 1px;
  315. }
  316. .login-code-img {
  317. height: 40px;
  318. padding-left: 12px;
  319. }
  320. </style>