login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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">记住密码</el-checkbox>-->
  46. <!-- 同意用户协议和隐私协议复选框(已移除Cookie政策) -->
  47. <el-form-item prop="agree" style="margin-bottom: 20px;">
  48. <el-checkbox v-model="loginForm.agree">
  49. 我已阅读并同意
  50. <el-link type="primary" :underline="false" href="https://rjsd.mychery.com/user_agreement.html" target="_blank">《用户协议》</el-link>
  51. <el-link type="primary" :underline="false" href="https://rjsd.mychery.com/privacy_policy.html" target="_blank">《隐私政策》</el-link>
  52. </el-checkbox>
  53. </el-form-item>
  54. <el-form-item style="width:100%;">
  55. <el-button
  56. :loading="loading"
  57. size="large"
  58. type="primary"
  59. style="width:100%;"
  60. @click.prevent="handleLogin"
  61. >
  62. <span v-if="!loading">登 录</span>
  63. <span v-else>登 录 中...</span>
  64. </el-button>
  65. <div style="float: right;" v-if="register">
  66. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  67. </div>
  68. </el-form-item>
  69. </el-form>
  70. </div>
  71. <!-- Cookie同意横幅(底部,含查看链接和同意按钮) -->
  72. <div v-if="showCookieBanner" class="cookie-banner">
  73. <span class="cookie-text">
  74. 我们将使用Cookie优化您的浏览体验,并通过第三方SDK实现网站功能。
  75. <el-link type="primary" :underline="false" href="https://rjsd.mychery.com/cookie.html" target="_blank">查看《Cookie政策》</el-link>
  76. </span>
  77. <el-button type="primary" size="small" @click="acceptCookies">同意</el-button>
  78. </div>
  79. <!-- 底部版权信息 -->
  80. <div class="el-login-footer">
  81. <span>{{ footerContent }}</span>
  82. </div>
  83. </div>
  84. </template>
  85. <script setup>
  86. import { getCodeImg } from "@/api/login"
  87. import Cookies from "js-cookie"
  88. import { encrypt, decrypt } from "@/utils/jsencrypt"
  89. import useUserStore from '@/store/modules/user'
  90. import defaultSettings from '@/settings'
  91. import { ElMessage } from "element-plus"
  92. import { ref, watch, getCurrentInstance, onMounted } from 'vue'
  93. import { useRoute, useRouter } from 'vue-router'
  94. const title = import.meta.env.VITE_APP_TITLE
  95. const footerContent = defaultSettings.footerContent
  96. const userStore = useUserStore()
  97. const route = useRoute()
  98. const router = useRouter()
  99. const { proxy } = getCurrentInstance()
  100. // 登录表单数据
  101. const loginForm = ref({
  102. username: "",
  103. password: "",
  104. rememberMe: false,
  105. code: "",
  106. uuid: "",
  107. agree: false // 仅用于用户协议和隐私政策
  108. })
  109. // 表单验证规则
  110. const loginRules = {
  111. username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  112. password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  113. code: [{ required: true, trigger: "change", message: "请输入验证码" }],
  114. agree: [{ required: true, trigger: "change", message: "请同意用户协议和隐私政策" }]
  115. }
  116. const codeUrl = ref("")
  117. const loading = ref(false)
  118. const captchaEnabled = ref(true) // 验证码开关
  119. const register = ref(false) // 注册开关
  120. const redirect = ref(undefined)
  121. const showCookieBanner = ref(false) // 控制Cookie横幅显示
  122. // 监听重定向地址
  123. watch(route, (newRoute) => {
  124. redirect.value = newRoute.query && newRoute.query.redirect
  125. }, { immediate: true })
  126. // 登录处理
  127. function handleLogin() {
  128. if (!proxy.$refs.loginRef) return
  129. proxy.$refs.loginRef.validate((valid) => {
  130. // 检查是否已同意用户协议和隐私政策
  131. if (!loginForm.value.agree) {
  132. ElMessage.warning('请先同意用户协议和隐私政策')
  133. return
  134. }
  135. // 检查是否已同意Cookie政策
  136. const cookieConsented = localStorage.getItem('cookieConsent') === 'true'
  137. // if (!cookieConsented) {
  138. // ElMessage.warning('请先同意Cookie政策')
  139. // // 如果未同意,显示横幅引导用户操作
  140. // showCookieBanner.value = true
  141. // return
  142. // }
  143. if (valid) {
  144. loading.value = true
  145. // 记住密码
  146. // if (loginForm.value.rememberMe) {
  147. // Cookies.set("username", loginForm.value.username, { expires: 30 })
  148. // Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
  149. // Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
  150. // } else {
  151. // Cookies.remove("username")
  152. // Cookies.remove("password")
  153. // Cookies.remove("rememberMe")
  154. // }
  155. userStore.login(loginForm.value).then(() => {
  156. const query = route.query
  157. const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
  158. if (cur !== "redirect") {
  159. acc[cur] = query[cur]
  160. }
  161. return acc
  162. }, {})
  163. router.push({ path: redirect.value || "/", query: otherQueryParams })
  164. }).catch(() => {
  165. loading.value = false
  166. if (captchaEnabled.value) {
  167. getCode()
  168. }
  169. })
  170. }
  171. })
  172. }
  173. // 获取验证码
  174. function getCode() {
  175. getCodeImg().then(res => {
  176. captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
  177. if (captchaEnabled.value) {
  178. codeUrl.value = "data:image/gif;base64," + res.img
  179. loginForm.value.uuid = res.uuid
  180. }
  181. })
  182. }
  183. // 从Cookie读取记住的密码
  184. function getCookie() {
  185. try {
  186. const username = Cookies.get("username")
  187. const password = Cookies.get("password")
  188. const rememberMe = Cookies.get("rememberMe")
  189. if (username !== undefined) loginForm.value.username = username
  190. if (password !== undefined) {
  191. try {
  192. loginForm.value.password = decrypt(password)
  193. } catch (e) {
  194. console.warn('密码解密失败', e)
  195. loginForm.value.password = ''
  196. }
  197. }
  198. if (rememberMe !== undefined) loginForm.value.rememberMe = Boolean(rememberMe)
  199. } catch (e) {
  200. console.error('读取Cookie失败', e)
  201. }
  202. }
  203. // 检查Cookie同意状态,控制横幅显示
  204. function checkCookieConsent() {
  205. const consented = localStorage.getItem('cookieConsent') === 'true'
  206. showCookieBanner.value = !consented // 未同意则显示横幅
  207. }
  208. // 同意Cookie(点击横幅按钮)
  209. function acceptCookies() {
  210. localStorage.setItem('cookieConsent', 'true')
  211. showCookieBanner.value = false
  212. // ElMessage.success('感谢您同意Cookie政策')
  213. }
  214. // 初始化
  215. onMounted(() => {
  216. getCode()
  217. // getCookie()
  218. checkCookieConsent()
  219. })
  220. </script>
  221. <style lang='scss' scoped>
  222. .login {
  223. display: flex;
  224. justify-content: center;
  225. align-items: center;
  226. height: 100%;
  227. background-image: url("../assets/images/login-background.png");
  228. background-size: cover;
  229. }
  230. .login-container{
  231. background: #ffffff;
  232. width: 40%;
  233. height: 100%;
  234. position: absolute;
  235. right: 0;
  236. padding-top: 5%;
  237. .title-logo {
  238. margin: 30px auto;
  239. color: #333;
  240. font-size: 24px;
  241. width: 100%;
  242. height: 100%;
  243. font-weight: bold;
  244. }
  245. .title {
  246. margin: 0px auto 30px auto;
  247. text-align: center;
  248. color: #333;
  249. font-size: 24rpx;
  250. line-height: 32rpx;
  251. }
  252. }
  253. .login-form {
  254. z-index: 1;
  255. display: flex;
  256. flex-direction: column;
  257. margin: 0px 30%;
  258. .el-input {
  259. height: 40px;
  260. input {
  261. height: 40px;
  262. }
  263. }
  264. .input-icon {
  265. height: 39px;
  266. width: 14px;
  267. margin-left: 0px;
  268. }
  269. }
  270. .login-tip {
  271. font-size: 13px;
  272. text-align: center;
  273. color: #bfbfbf;
  274. }
  275. .login-code {
  276. width: 33%;
  277. height: 40px;
  278. float: right;
  279. img {
  280. cursor: pointer;
  281. vertical-align: middle;
  282. }
  283. }
  284. .login-code-img {
  285. height: 40px;
  286. }
  287. /* Cookie同意横幅样式:深色字体,浅色背景,固定在底部 */
  288. .cookie-banner {
  289. position: fixed;
  290. bottom: 40px; /* 位于版权信息上方 */
  291. left: 0;
  292. width: 100%;
  293. background-color: #f9f9f9; /* 浅色背景 */
  294. color: #333; /* 深色字体 */
  295. padding: 12px 20px;
  296. display: flex;
  297. justify-content: center;
  298. align-items: center;
  299. gap: 20px;
  300. box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
  301. z-index: 1000;
  302. font-size: 14px;
  303. flex-wrap: wrap;
  304. text-align: center;
  305. }
  306. .cookie-text {
  307. max-width: 70%;
  308. line-height: 1.5;
  309. }
  310. .el-button--small {
  311. background-color: #409EFF;
  312. color: white;
  313. border: none;
  314. padding: 8px 16px;
  315. border-radius: 4px;
  316. cursor: pointer;
  317. font-weight: 500;
  318. }
  319. .el-button--small:hover {
  320. background-color: #66b1ff;
  321. }
  322. /* 底部查看链接样式 */
  323. .cookie-text .el-link {
  324. margin-left: 4px;
  325. font-weight: 500;
  326. }
  327. .el-login-footer {
  328. height: 40px;
  329. line-height: 40px;
  330. position: fixed;
  331. bottom: 0;
  332. width: 100%;
  333. text-align: center;
  334. color: #fff;
  335. font-family: Arial;
  336. font-size: 12px;
  337. letter-spacing: 1px;
  338. }
  339. </style>