登录记录缓存

This commit is contained in:
2024-07-15 15:29:29 +08:00
parent 3ead0ef3be
commit 8618130792
3 changed files with 186 additions and 129 deletions

View File

@@ -2,8 +2,8 @@
"name" : "干巷仓库APP",
"appid" : "__UNI__A67E78B",
"description" : "",
"versionName" : "2.2.5",
"versionCode" : 225,
"versionName" : "2.2.6",
"versionCode" : 226,
"transformPx" : false,
"app-plus" : {
"usingComponents" : true,

View File

@@ -9,20 +9,25 @@
<!-- 登录表单 -->
<u--form labelPosition="left" :model="loginForm" ref="uForm">
<u-form-item prop="username" borderBottom>
<u--input v-model="loginForm.username" border="none" placeholder="账号" prefixIcon="account" prefixIconStyle="font-size: 22px;color: #909399"></u--input>
<u--input v-model="loginForm.username" border="none" placeholder="账号" prefixIcon="account"
prefixIconStyle="font-size: 22px;color: #909399"></u--input>
</u-form-item>
<u-form-item prop="password" borderBottom>
<u--input v-model="loginForm.password" border="none" placeholder="密码" prefixIcon="lock" prefixIconStyle="font-size: 22px;color: #909399"></u--input>
<u--input v-model="loginForm.password" border="none" placeholder="密码" prefixIcon="lock"
prefixIconStyle="font-size: 22px;color: #909399"></u--input>
</u-form-item>
<u-form-item prop="BaseUrl" borderBottom>
<u-input v-model="BaseUrl" border="none" placeholder="连接地址,例:127.0.0.1:8888" prefixIcon="ie" prefixIconStyle="font-size: 22px;color: #909399">
<u-input v-model="BaseUrl" border="none" placeholder="连接地址,例:127.0.0.1:8888" prefixIcon="ie"
prefixIconStyle="font-size: 22px;color: #909399">
<template slot="suffix">
<u-button type="primary" size="mini" text="修改地址" @click="handlerBaseUrlConfirm" shape="circle"></u-button>
<u-button type="primary" size="mini" text="修改地址" @click="handlerBaseUrlConfirm"
shape="circle"></u-button>
</template>
</u-input>
</u-form-item>
<u-form-item prop="button" style="margin-top: 60px">
<u-button @click="handleLogin" type="primary" :color="buttonColor" text="登录" shape="circle"></u-button>
<u-button @click="handleLogin" type="primary" :color="buttonColor" text="登录"
shape="circle"></u-button>
</u-form-item>
</u--form>
</view>
@@ -34,131 +39,170 @@
</template>
<script>
import { getCodeImg } from '@/api/login';
import { getToken, setToken, removeToken } from '@/utils/auth';
import { getBaseUrl, setBaseUrl, removeBaseUrl } from '@/utils/baseUrl';
import config from '@/config';
export default {
onLoad() {
const systemInfo = uni.getSystemInfoSync();
this.version = systemInfo.appVersion;
},
data() {
return {
version: '',
codeUrl: '',
captchaEnabled: false,
// 用户注册开关
register: false,
globalConfig: getApp().globalData.config,
BaseUrl: '127.0.0.1:8888',
buttonColor: 'linear-gradient(to right, #3296F9, #235FF5)',
loginForm: {
username: 'admin',
password: '123456',
code: '',
uuid: ''
}
};
},
created() {
this.init();
// this.getCode();
},
methods: {
init() {
// 初始化链接地址设计
let baseUrl = getBaseUrl();
if (!baseUrl) {
baseUrl = '192.168.60.251:8888';
import {
getCodeImg
} from '@/api/login';
import {
getToken,
setToken,
removeToken
} from '@/utils/auth';
import {
getBaseUrl,
setBaseUrl,
removeBaseUrl
} from '@/utils/baseUrl';
import {
getLoginInfo,
setLoginInfo,
removeLoginInfo
} from '@/utils/loginInfo';
import config from '@/config';
export default {
onLoad() {
const systemInfo = uni.getSystemInfoSync();
this.version = systemInfo.appVersion;
},
data() {
return {
version: '',
codeUrl: '',
captchaEnabled: false,
// 用户注册开关
register: false,
globalConfig: getApp().globalData.config,
BaseUrl: '127.0.0.1:8888',
buttonColor: 'linear-gradient(to right, #3296F9, #235FF5)',
loginForm: {
username: '',
password: '',
code: '',
uuid: ''
}
};
},
created() {
this.init();
// this.getCode();
},
methods: {
init() {
// 初始化链接地址设计
let baseUrl = getBaseUrl();
if (!baseUrl) {
baseUrl = '192.168.60.251:8888';
setBaseUrl(baseUrl);
}
this.BaseUrl = baseUrl;
// 初始化用户登录存储
try {
let loginInfoStr = getLoginInfo();
let loginInfo = JSON.parse(loginInfoStr);
if (loginInfo) {
this.loginForm.username = loginInfo.username;
this.loginForm.password = loginInfo.password;
}
} catch (e) {
// console.log(e);
//TODO handle the exception
}
},
// 获取图形验证码
getCode() {
getCodeImg().then((res) => {
if (res.code !== 200) {
uni.showToast({
title: '获取验证码失败!请检查网络!',
icon: 'error'
});
return;
}
const result = res.data;
if (result.captchaOff === 'off') {
this.captchaEnabled = false;
return;
} else {
this.captchaEnabled = true;
}
this.codeUrl = 'data:image/gif;base64,' + result.img;
this.loginForm.uuid = result.uuid;
});
},
// 登录方法
async handleLogin() {
// debug快速登录
if (this.loginForm.username === 'debug') {
setToken('123');
this.$tab.reLaunch('/pages/index/index');
return;
}
if (this.loginForm.username === '') {
this.$modal.msgError('请输入您的账号');
} else if (this.loginForm.password === '') {
this.$modal.msgError('请输入您的密码');
} else if (this.loginForm.code === '' && this.captchaEnabled) {
this.$modal.msgError('请输入验证码');
} else {
this.$modal.loading('登录中,请耐心等待...');
setTimeout(() => {
this.$modal.closeLoading();
}, 30000);
this.pwdLogin();
}
},
// 密码登录
async pwdLogin() {
this.$store
.dispatch('Login', this.loginForm)
.then(() => {
try {
const loginInfo = {
username: this.loginForm.username,
password: this.loginForm.password
}
const loginInfoStr = JSON.stringify(loginInfo);
setLoginInfo(loginInfoStr);
} catch (e) {
// console.log(e);
}
this.$modal.closeLoading();
this.loginSuccess();
})
.catch(() => {
this.$modal.closeLoading();
if (this.captchaEnabled) {
this.getCode();
}
});
},
// 登录成功后,处理函数
loginSuccess(result) {
// 获取户信息
this.$store.dispatch('GetInfo').then((res) => {
this.$tab.reLaunch('/pages/index/index');
});
},
// 确认修改链接地址
handlerBaseUrlConfirm() {
const baseUrl = this.BaseUrl;
// console.log('changeBaseUrl', baseUrl);
uni.showToast({
title: '链接地址已修改:' + baseUrl,
icon: 'none'
});
setBaseUrl(baseUrl);
}
this.BaseUrl = baseUrl;
},
// 获取图形验证码
getCode() {
getCodeImg().then((res) => {
if (res.code !== 200) {
uni.showToast({
title: '获取验证码失败!请检查网络!',
icon: 'error'
});
return;
}
const result = res.data;
if (result.captchaOff === 'off') {
this.captchaEnabled = false;
return;
} else {
this.captchaEnabled = true;
}
this.codeUrl = 'data:image/gif;base64,' + result.img;
this.loginForm.uuid = result.uuid;
});
},
// 登录方法
async handleLogin() {
// debug快速登录
if (this.loginForm.username === 'debug') {
setToken('123');
this.$tab.reLaunch('/pages/index/index');
return;
}
if (this.loginForm.username === '') {
this.$modal.msgError('请输入您的账号');
} else if (this.loginForm.password === '') {
this.$modal.msgError('请输入您的密码');
} else if (this.loginForm.code === '' && this.captchaEnabled) {
this.$modal.msgError('请输入验证码');
} else {
this.$modal.loading('登录中,请耐心等待...');
setTimeout(() => {
this.$modal.closeLoading();
}, 30000);
this.pwdLogin();
}
},
// 密码登录
async pwdLogin() {
this.$store
.dispatch('Login', this.loginForm)
.then(() => {
this.$modal.closeLoading();
this.loginSuccess();
})
.catch(() => {
this.$modal.closeLoading();
if (this.captchaEnabled) {
this.getCode();
}
});
},
// 登录成功后,处理函数
loginSuccess(result) {
// 获取户信息
this.$store.dispatch('GetInfo').then((res) => {
this.$tab.reLaunch('/pages/index/index');
});
},
// 确认修改链接地址
handlerBaseUrlConfirm() {
const baseUrl = this.BaseUrl;
console.log('changeBaseUrl', baseUrl);
uni.showToast({
title: '链接地址已修改:' + baseUrl,
icon: 'none'
});
setBaseUrl(baseUrl);
}
}
};
};
</script>
<style lang="scss">
.bangben {
margin-top: 110px;
margin-left: 35%;
font-size: 0.8rem;
}
@import url('login.scss');
</style>
.bangben {
margin-top: 110px;
margin-left: 35%;
font-size: 0.8rem;
}
@import url('login.scss');
</style>

13
utils/loginInfo.js Normal file
View File

@@ -0,0 +1,13 @@
const key = '_LoginInfo'
export function getLoginInfo() {
return uni.getStorageSync(key)
}
export function setLoginInfo(data) {
return uni.setStorageSync(key, data)
}
export function removeLoginInfo() {
return uni.removeStorageSync(key)
}