记住密码适配小程序

master
lld 2026-02-08 21:58:16 +08:00
parent 973502ad1e
commit 783def7c10
1 changed files with 52 additions and 19 deletions

View File

@ -128,27 +128,55 @@
}
},
// ========== ==========
//
// ========== ==========
getEncryptKey() {
try {
const sys = uni.getSystemInfoSync()
const appId = uni.getAccountInfoSync()?.miniProgram?.appId || 'agri-default'
return CryptoJS.MD5(sys.deviceId + appId + 'agri-pwd-key').toString()
const sys = uni.getSystemInfoSync();
// wx.getAccountInfoSync
const appId = "agri-mini-program-fixed";
const deviceId = sys.deviceId || "default-device";
// MD5crypto
return CryptoJS.MD5(deviceId + appId + "agri-pwd-key").toString();
} catch (e) {
return 'agri-safe-key-2026'
return "agri-safe-key-2026-fixed";
}
},
//
// ========== crypto ==========
encryptPwd(pwd) {
return CryptoJS.AES.encrypt(pwd, this.getEncryptKey()).toString()
try {
const key = this.getEncryptKey();
// CryptoJS
const srcs = CryptoJS.enc.Utf8.parse(pwd);
const keys = CryptoJS.enc.Utf8.parse(key.slice(0, 16)); // 16
const encrypted = CryptoJS.AES.encrypt(srcs, keys, {
mode: CryptoJS.mode.ECB, // ECB
padding: CryptoJS.pad.Pkcs7
});
// base64
return encrypted.toString().replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '~');
} catch (e) {
console.error("加密失败", e);
return "";
}
},
//
// ========== ==========
decryptPwd(encryptedStr) {
try {
const bytes = CryptoJS.AES.decrypt(encryptedStr, this.getEncryptKey())
return bytes.toString(CryptoJS.enc.Utf8) || ''
if (!encryptedStr) return "";
//
const decodeStr = encryptedStr.replace(/-/g, '+').replace(/_/g, '/').replace(/~/g, '=');
const key = this.getEncryptKey();
const keys = CryptoJS.enc.Utf8.parse(key.slice(0, 16));
const decrypt = CryptoJS.AES.decrypt(decodeStr, keys, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypt.toString(CryptoJS.enc.Utf8) || "";
} catch (e) {
return ''
console.error("解密失败", e);
return "";
}
},
//
@ -168,16 +196,18 @@
},
//
savePwd() {
if (!this.remember) {
uni.removeStorageSync(this.STORAGE_KEY)
return
//
const pwd = this.encryptPwd(this.loginForm.password);
if (!this.remember || !pwd) {
uni.removeStorageSync(this.STORAGE_KEY);
return;
}
const saveData = {
username: this.loginForm.username,
encryptedPwd: this.encryptPwd(this.loginForm.password),
encryptedPwd: pwd,
saveTime: Date.now()
}
uni.setStorageSync(this.STORAGE_KEY, JSON.stringify(saveData))
};
uni.setStorageSync(this.STORAGE_KEY, JSON.stringify(saveData));
},
// ========== ==========
@ -225,10 +255,13 @@
// ========== ==========
this.savePwd()
this.loginSuccess()
}).catch(() => {
}).catch((err) => {
this.$modal.closeLoading(); //
this.$modal.msgError('登录失败:' + (err.msg || '账号密码错误')); //
if (this.captchaEnabled) {
this.getCode()
this.getCode();
}
console.error('登录失败详情:', err); // 便
})
},
//