login.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <h2 class="title" >建科•小智权限管理系统</h2>
  5. <el-form-item prop="userName">
  6. <el-input
  7. v-model="loginForm.userName"
  8. type="text"
  9. auto-complete="off"
  10. placeholder="账号"
  11. >
  12. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <el-input
  17. v-model="loginForm.password"
  18. type="password"
  19. auto-complete="off"
  20. placeholder="密码"
  21. @keyup.enter.native="handleLogin"
  22. >
  23. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  24. </el-input>
  25. </el-form-item>
  26. <el-form-item prop="code" v-if="captchaEnabled">
  27. <el-input
  28. v-model="loginForm.code"
  29. auto-complete="off"
  30. placeholder="验证码"
  31. style="width: 63%"
  32. @keyup.enter.native="handleLogin"
  33. >
  34. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  35. </el-input>
  36. <div class="login-code">
  37. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  38. </div>
  39. </el-form-item>
  40. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  41. <el-form-item style="width:100%;">
  42. <el-button
  43. :loading="loading"
  44. size="medium"
  45. type="primary"
  46. style="width:100%;"
  47. @click.native.prevent="handleLogin"
  48. >
  49. <span v-if="!loading">登 录</span>
  50. <span v-else>登 录 中...</span>
  51. </el-button>
  52. <div style="float: right;" v-if="register">
  53. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  54. </div>
  55. </el-form-item>
  56. </el-form>
  57. <!-- 底部 -->
  58. <div class="el-login-footer">
  59. <span>上海建科工程咨询有限公司 版权所有CopyRight © 2025 ALL RIGHTS RESERVED.</span>
  60. </div>
  61. </div>
  62. </template>
  63. <script>
  64. import { getCodeImg } from "@/api/login";
  65. // ✅ 安全修复:使用 localStorage 替代 Cookies
  66. // import Cookies from "js-cookie";
  67. // ✅ 安全修复:只导入encrypt,不导入decrypt(decrypt已被删除)
  68. import { encrypt } from '@/utils/jsencrypt'
  69. export default {
  70. name: "Login",
  71. data() {
  72. return {
  73. codeUrl: "",
  74. loginForm: {
  75. // ✅ 安全修复:移除默认账号密码
  76. userName: "",
  77. password: "",
  78. rememberMe: false,
  79. code: "",
  80. uuid: ""
  81. },
  82. loginRules: {
  83. userName: [
  84. { required: true, trigger: "blur", message: "请输入您的账号" }
  85. ],
  86. password: [
  87. { required: true, trigger: "blur", message: "请输入您的密码" }
  88. ],
  89. code: [{ required: true, trigger: "change", message: "请输入验证码" }]
  90. },
  91. loading: false,
  92. // 验证码开关
  93. captchaEnabled: true,
  94. // 注册开关
  95. register: false,
  96. redirect: undefined
  97. };
  98. },
  99. watch: {
  100. $route: {
  101. handler: function(route) {
  102. this.redirect = route.query && route.query.redirect;
  103. },
  104. immediate: true
  105. }
  106. },
  107. created() {
  108. this.getCode();
  109. this.loadRememberedUser(); // ✅ 改名,更准确
  110. },
  111. methods: {
  112. getCode() {
  113. getCodeImg().then(res => {
  114. // ✅ 安全修复:移除console.log或仅在开发环境使用
  115. if (process.env.NODE_ENV === 'development') {
  116. console.log('验证码获取成功');
  117. }
  118. this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
  119. if (this.captchaEnabled) {
  120. this.codeUrl = "data:image/gif;base64," + res.data.img;
  121. this.loginForm.uuid = res.data.uuid;
  122. }
  123. });
  124. },
  125. // ✅ 安全修复:只加载用户名,不加载密码(使用 localStorage)
  126. loadRememberedUser() {
  127. const rememberedUser = localStorage.getItem("rememberedUser");
  128. if (rememberedUser) {
  129. this.loginForm.userName = rememberedUser;
  130. this.loginForm.rememberMe = true;
  131. }
  132. },
  133. handleLogin() {
  134. this.$refs.loginForm.validate(valid => {
  135. if (valid) {
  136. this.loading = true;
  137. // ✅ 安全修复:只保存用户名,不保存密码(使用 localStorage)
  138. if (this.loginForm.rememberMe) {
  139. localStorage.setItem("rememberedUser", this.loginForm.userName);
  140. } else {
  141. localStorage.removeItem("rememberedUser");
  142. // 清理旧的存储(如果存在)
  143. localStorage.removeItem("userName");
  144. localStorage.removeItem("password");
  145. localStorage.removeItem("rememberMe");
  146. }
  147. this.$store.dispatch("Login", this.loginForm).then(() => {
  148. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  149. }).catch(() => {
  150. this.loading = false;
  151. if (this.captchaEnabled) {
  152. this.getCode();
  153. }
  154. });
  155. }
  156. });
  157. }
  158. }
  159. };
  160. </script>
  161. <style rel="stylesheet/scss" lang="scss">
  162. .login {
  163. display: flex;
  164. justify-content: center;
  165. align-items: center;
  166. height: 100%;
  167. background-image: url("../assets/images/login-background.jpg");
  168. background-size: cover;
  169. }
  170. .title {
  171. margin: 0px auto 30px auto;
  172. text-align: center;
  173. color: #409EFF;
  174. font-family: Arial;
  175. font-size: 30px;
  176. }
  177. .login-form {
  178. border-radius: 6px;
  179. background: #ffffff;
  180. width: 400px;
  181. padding: 25px 25px 5px 25px;
  182. .el-input {
  183. height: 38px;
  184. input {
  185. height: 38px;
  186. }
  187. }
  188. .input-icon {
  189. height: 39px;
  190. width: 14px;
  191. margin-left: 2px;
  192. }
  193. }
  194. .login-tip {
  195. font-size: 13px;
  196. text-align: center;
  197. color: #bfbfbf;
  198. }
  199. .login-code {
  200. width: 33%;
  201. height: 38px;
  202. float: right;
  203. img {
  204. cursor: pointer;
  205. vertical-align: middle;
  206. }
  207. }
  208. .el-login-footer {
  209. height: 40px;
  210. line-height: 40px;
  211. position: fixed;
  212. bottom: 0;
  213. width: 100%;
  214. text-align: center;
  215. color: #fff;
  216. font-family: Arial;
  217. font-size: 12px;
  218. letter-spacing: 1px;
  219. }
  220. .login-code-img {
  221. height: 38px;
  222. }
  223. </style>