login.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. show-password
  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. <!-- <div>
  62. <img src="../assets/images/gxt-release.png" alt="logo"
  63. style="position: fixed; bottom: 10px; right: 10px; width: 120px;" />
  64. <img src="../assets/images/gxt-release-hm.png" alt="logo"
  65. style="position: fixed; bottom: 10px; right: 140px; width: 120px;" />
  66. </div> -->
  67. <!-- 底部 -->
  68. <div class="el-login-footer">
  69. <span>Copyright © 2025-2025 ygtxfj.com All Rights Reserved.</span>
  70. </div>
  71. </div>
  72. <!-- 初始密码修改弹窗 -->
  73. <login-reset-pwd ref="resetPwdRef" :username="loginForm.username" :password="loginForm.password" :code="loginForm.code" :uuid="loginForm.uuid" />
  74. </template>
  75. <script setup>
  76. import { getCodeImg } from "@/api/login"
  77. import Cookies from "js-cookie"
  78. import { encrypt, decrypt } from "@/utils/jsencrypt"
  79. import { encryptAES, decryptAES } from '@/utils/cryptoAES'
  80. // import { getIsKey } from '@/utils/crypto'
  81. import useUserStore from '@/store/modules/user'
  82. import LoginResetPwd from './loginResetPwd.vue'
  83. const title = import.meta.env.VITE_APP_TITLE
  84. const userStore = useUserStore()
  85. const route = useRoute()
  86. const router = useRouter()
  87. const { proxy } = getCurrentInstance()
  88. const loginForm = ref({
  89. username: "",
  90. password: "",
  91. rememberMe: false,
  92. code: "",
  93. uuid: ""
  94. })
  95. const loginRules = {
  96. username: [{ required: true, trigger: "blur", message: "请输入您的工号" }],
  97. password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  98. code: [{ required: true, trigger: "change", message: "请输入验证码" }]
  99. }
  100. const codeUrl = ref("")
  101. const loading = ref(false)
  102. // 验证码开关
  103. const captchaEnabled = ref(false)
  104. // 注册开关
  105. const register = ref(false)
  106. const redirect = ref(undefined)
  107. // 重置密码弹窗引用
  108. const resetPwdRef = ref(null)
  109. watch(route, (newRoute) => {
  110. redirect.value = newRoute.query && newRoute.query.redirect
  111. }, { immediate: true })
  112. function handleLogin() {
  113. proxy.$refs.loginRef.validate(async valid => {
  114. if (valid) {
  115. loading.value = true
  116. // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
  117. if (loginForm.value.rememberMe) {
  118. Cookies.set("username", loginForm.value.username, { expires: 30 })
  119. Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
  120. Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
  121. } else {
  122. // 否则移除
  123. Cookies.remove("username")
  124. Cookies.remove("password")
  125. Cookies.remove("rememberMe")
  126. }
  127. const requestForm = { ...loginForm.value }
  128. try {
  129. // const keyObj = await getIsKey()
  130. const isKey = "1";
  131. if("1" === isKey || 1 === isKey){
  132. requestForm.username = encryptAES(loginForm.value.username)
  133. requestForm.password = encryptAES(loginForm.value.password)
  134. requestForm.code = encryptAES(loginForm.value.code)
  135. }
  136. } catch (error) {}
  137. userStore.login(requestForm).then(response => {
  138. // 检查是否是初始密码
  139. if (response.isInitPassword) {
  140. loading.value = false
  141. proxy.$modal.msgWarning(response.message)
  142. // 显示修改密码弹窗,传递登录信息
  143. resetPwdRef.value.show(loginForm.value)
  144. } else {
  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 = res.captchaEnabled === undefined ? true : res.captchaEnabled
  167. if (captchaEnabled.value) {
  168. codeUrl.value = "data:image/gif;base64," + res.img
  169. loginForm.value.uuid = res.uuid
  170. }
  171. })
  172. }
  173. function getCookie() {
  174. const username = Cookies.get("username")
  175. const password = Cookies.get("password")
  176. const rememberMe = Cookies.get("rememberMe")
  177. loginForm.value = {
  178. username: username === undefined ? loginForm.value.username : username,
  179. password: password === undefined ? loginForm.value.password : decrypt(password),
  180. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  181. }
  182. }
  183. getCode()
  184. getCookie()
  185. </script>
  186. <style lang='scss' scoped>
  187. .login {
  188. display: flex;
  189. justify-content: center;
  190. align-items: center;
  191. height: 100%;
  192. background-image: url("../assets/images/login-background.jpg");
  193. background-size: cover;
  194. }
  195. .title {
  196. margin: 0px auto 30px auto;
  197. text-align: center;
  198. color: #707070;
  199. }
  200. .login-form {
  201. border-radius: 6px;
  202. background: #ffffff;
  203. width: 400px;
  204. padding: 25px 25px 5px 25px;
  205. z-index: 1;
  206. .el-input {
  207. height: 40px;
  208. input {
  209. height: 40px;
  210. }
  211. }
  212. .input-icon {
  213. height: 39px;
  214. width: 14px;
  215. margin-left: 0px;
  216. }
  217. }
  218. .login-tip {
  219. font-size: 13px;
  220. text-align: center;
  221. color: #bfbfbf;
  222. }
  223. .login-code {
  224. width: 33%;
  225. height: 40px;
  226. float: right;
  227. img {
  228. cursor: pointer;
  229. vertical-align: middle;
  230. }
  231. }
  232. .el-login-footer {
  233. height: 40px;
  234. line-height: 40px;
  235. position: fixed;
  236. bottom: 0;
  237. width: 100%;
  238. text-align: center;
  239. color: #fff;
  240. font-family: Arial;
  241. font-size: 12px;
  242. letter-spacing: 1px;
  243. }
  244. .login-code-img {
  245. height: 40px;
  246. padding-left: 12px;
  247. }
  248. </style>