新增记住密码

master
lld 2026-02-08 19:35:26 +08:00
parent 4134eef47e
commit c9b61ed275
2 changed files with 96 additions and 7 deletions

View File

@ -1,5 +1,6 @@
{
"dependencies": {
"crypto-js": "^4.2.0",
"mqtt": "^2.18.8"
}
}

View File

@ -13,15 +13,15 @@
<view class="input-item flex align-center" style="position: relative;">
<view class="iconfont icon-password icon"></view>
<input v-if="!isShowPwd"
v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
<input v-else v-model="loginForm.password" type="text" class="input" placeholder="请输入密码" maxlength="20" />
<!-- 替换为 uni-icons 密码图标 -->
<uni-icons
class="pwd-icon"
:type="isShowPwd ? 'eye-filled' : 'eye'"
size="18"
color="#666"
@click="isShowPwd = !isShowPwd"
size="18"
color="#666"
@click="isShowPwd = !isShowPwd"
></uni-icons>
</view>
<view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
@ -31,6 +31,20 @@
<image :src="codeUrl" @click="getCode" class="login-code-img"></image>
</view>
</view>
<!-- ========== 新增uni-data-checkbox 记住密码可正常勾选 ========== -->
<view style="margin: 10px 0 0 10px; text-align: left;">
<uni-data-checkbox
multiple
v-model="value"
:localdata="data"
size="14"
color="#007aff"
@change="onRememberChange"
></uni-data-checkbox>
</view>
<!-- ========== 新增结束 ========== -->
<view class="action-btn">
<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
</view>
@ -65,9 +79,13 @@
import { getCodeImg } from '@/api/login'
import { getToken } from '@/utils/auth'
import UniIcons from "../uni_modules/uni-icons/components/uni-icons/uni-icons.vue";
// ========== uni-data-checkbox ==========
import UniDataCheckbox from "@/uni_modules/uni-data-checkbox/components/uni-data-checkbox/uni-data-checkbox.vue";
import CryptoJS from 'crypto-js'
export default {
components: {UniIcons},
// ========== ==========
components: {UniIcons, UniDataCheckbox},
data() {
return {
isShowPwd: false,
@ -75,13 +93,18 @@
captchaEnabled: true,
//
register: true,
data:[{value: 0, text: '记住密码'}],
globalConfig: getApp().globalData.config,
loginForm: {
username: "",
password: "",
code: "",
uuid: ""
}
},
value:[0],
// ========== ==========
remember: true, //
STORAGE_KEY: 'agri_login_pwd' // key
}
},
created() {
@ -93,8 +116,71 @@
this.$tab.reLaunch('/pages/control/index')
}
//#endif
// ========== ==========
this.loadSavedPwd()
},
methods: {
// ========== uni-data-checkbox ==========
onRememberChange(e) {
this.remember = false;
if (e.detail.value && e.detail.value===0) {
this.remember = true ;
}
},
// ========== ==========
//
getEncryptKey() {
try {
const sys = uni.getSystemInfoSync()
const appId = uni.getAccountInfoSync()?.miniProgram?.appId || 'agri-default'
return CryptoJS.MD5(sys.deviceId + appId + 'agri-pwd-key').toString()
} catch (e) {
return 'agri-safe-key-2026'
}
},
//
encryptPwd(pwd) {
return CryptoJS.AES.encrypt(pwd, this.getEncryptKey()).toString()
},
//
decryptPwd(encryptedStr) {
try {
const bytes = CryptoJS.AES.decrypt(encryptedStr, this.getEncryptKey())
return bytes.toString(CryptoJS.enc.Utf8) || ''
} catch (e) {
return ''
}
},
//
loadSavedPwd() {
try {
const savedData = uni.getStorageSync(this.STORAGE_KEY)
if (!savedData) return
const data = JSON.parse(savedData)
if (data.username && data.encryptedPwd) {
this.loginForm.username = data.username
this.loginForm.password = this.decryptPwd(data.encryptedPwd)
this.remember = true //
}
} catch (e) {
console.error('加载记住密码失败:', e)
}
},
//
savePwd() {
if (!this.remember) {
uni.removeStorageSync(this.STORAGE_KEY)
return
}
const saveData = {
username: this.loginForm.username,
encryptedPwd: this.encryptPwd(this.loginForm.password),
saveTime: Date.now()
}
uni.setStorageSync(this.STORAGE_KEY, JSON.stringify(saveData))
},
// ========== ==========
//
handleUserRegister() {
this.$tab.redirectTo(`/pages/register`)
@ -136,6 +222,8 @@
async pwdLogin() {
this.$store.dispatch('Login', this.loginForm).then(() => {
this.$modal.closeLoading()
// ========== ==========
this.savePwd()
this.loginSuccess()
}).catch(() => {
if (this.captchaEnabled) {
@ -147,7 +235,7 @@
loginSuccess(result) {
//
this.$store.dispatch('GetInfo').then(res => {
// ========== MQTT ==========
// ========== MQTT ==========
// 1. App
const app = getApp()
// 2. tokenauth