login.vue 6.1 KB

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