Quellcode durchsuchen

登录页记住我功能调整

user5 vor 1 Jahr
Ursprung
Commit
1fd2ef78ce
2 geänderte Dateien mit 48 neuen und 0 gelöschten Zeilen
  1. 1 0
      package.json
  2. 47 0
      src/views/sys/login/components/login-form.vue

+ 1 - 0
package.json

@@ -34,6 +34,7 @@
     "jeeplus-flowable-v3": "^2.1.0",
     "jeeplus-form-v3": "^2.1.0",
     "jquery": "^3.6.0",
+    "js-base64": "^3.7.5",
     "js-cookie": "3.0.0",
     "lodash": "^4.17.21",
     "lodash.omit": "^4.5.0",

+ 47 - 0
src/views/sys/login/components/login-form.vue

@@ -169,6 +169,9 @@ import config from "@/config";
 import loginService from "@/api/auth/loginService";
 import userService from "@/api/sys/userService";
 import tenantService from "@/api/sys/tenantService";
+
+//在运用页面引入
+import { Base64 } from 'js-base64' //引入base64加密,用于记住密码
 export default {
 	data() {
 		return {
@@ -251,6 +254,8 @@ export default {
 		this.$store.commit("clearIframeList");
 	},
 	mounted() {
+
+		this.getCookie()
 		// this.getCaptcha();
 		if (this.inputForm.username) {
 			this.getLoginCodeNumber(this.inputForm.username)
@@ -264,6 +269,9 @@ export default {
 	methods: {
 		// 提交表单
 		async login() {
+			var checked = this.checked
+			var password = this.inputForm.password
+			var username = this.inputForm.username
 			this.$refs.loginForm.validate((valid) => {
 				if (valid) {
 					this.loading = true;
@@ -309,6 +317,17 @@ export default {
 							this.$TOOL.data.set("MENU", menuList);
 							this.$TOOL.data.set("PERMISSIONS", permissions);
 							this.$TOOL.data.set("DICT_LIST", dictList);
+
+							// let Base64 = require('js-base64').Base64
+							/* ------ 账号密码的存储 ------ */
+							if (checked) {
+								// eslint-disable-next-line no-undef
+								// let password = Base64.encode(passwordLogin) // base64加密
+								this.setCookie(Base64.encode(username), Base64.encode(password), 7)
+							} else {
+								this.setCookie('', '', -1) // 修改2值都为空,天数为负1天就好了
+							}
+							/* ------ http登录请求 ------ */
 							this.$notify({
 								title: "登录成功",
 								message: `欢迎回来!<br/>你上次的登录IP是 ${loginResp.oldLoginIp},登录时间是 ${loginResp.oldLoginDate}。`,
@@ -370,6 +389,34 @@ export default {
 				}
 			})
 		},
+		// 设置cookie
+		setCookie (username, password, days) {
+			let date = new Date() // 获取时间
+			date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * days) // 保存的天数
+			// 字符串拼接cookie
+			window.document.cookie =
+				'username' + '=' + username + ';path=/;expires=' + date.toGMTString()
+			window.document.cookie =
+				'password' + '=' + password + ';path=/;expires=' + date.toGMTString()
+		},
+
+		// 读取cookie 将用户名和密码回显到input框中
+		getCookie () {
+			// let Base64 = require('js-base64').Base64
+			if (document.cookie.length > 0) {
+				let arr = document.cookie.split('; ') // 分割成一个个独立的“key=value”的形式
+				for (let i = 0; i < arr.length; i++) {
+					let arr2 = arr[i].split('=') // 再次切割,arr2[0]为key值,arr2[1]为对应的value
+					if (arr2[0] === 'username') {
+						this.inputForm.username = Base64.decode(arr2[1])// base64解密
+					} else if (arr2[0] === 'password') {
+						// eslint-disable-next-line no-undef
+						this.inputForm.password = Base64.decode(arr2[1])// base64解密
+						this.checked = true
+					}
+				}
+			}
+		},
 		// 切换语言
 		changeLanguage(language) {
 			this.$TOOL.data.set("APP_LANG", language);