login.vue 6.5 KB

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