login.vue 6.7 KB

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