mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 00:23:53 +08:00
优化登录 token 校验:
1、后端新增 /api/v1/user/info 接口,优化 Oauth2Filter.getRequestToken 逻辑; 2、前端 /api/v1/user/login 登录成功后,保存 token 至浏览器本地; 3、前端请求添加本地 token。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {goToPage, showDanger, showWarning} from '../utils/index'
|
||||
import Constant from '../utils/constant'
|
||||
import Fly from 'flyio/dist/npm/fly';
|
||||
import store from '../store/index'
|
||||
|
||||
const fly = new Fly()
|
||||
// 设置超时
|
||||
@@ -25,7 +26,7 @@ function sendRequest() {
|
||||
_url: '',
|
||||
_responseType: undefined, // 新增响应类型字段
|
||||
'send'() {
|
||||
this._header.token = localStorage.getItem(Constant.STORAGE_KEY.TOKEN)
|
||||
this._header.Authorization = 'Bearer ' + (JSON.parse(store.getters.getToken)).token
|
||||
|
||||
// 打印请求信息
|
||||
fly.request(this._url, this._data, {
|
||||
@@ -43,7 +44,7 @@ function sendRequest() {
|
||||
}
|
||||
}).catch((res) => {
|
||||
// 打印失败响应
|
||||
console.log(res)
|
||||
console.log('catch', res)
|
||||
httpHandlerError(res, this._failCallback)
|
||||
})
|
||||
return this
|
||||
@@ -97,6 +98,7 @@ function sendRequest() {
|
||||
*/
|
||||
// 在错误处理函数中添加日志
|
||||
function httpHandlerError(info, callBack) {
|
||||
console.log('httpHandlerError', info)
|
||||
|
||||
/** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */
|
||||
let networkError = false
|
||||
|
||||
@@ -32,7 +32,7 @@ export default {
|
||||
},
|
||||
// 获取设备信息
|
||||
getHomeList(callback) {
|
||||
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/device/bind`).method('GET')
|
||||
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/device/bind`).method('GET')
|
||||
.success((res) => {
|
||||
RequestService.clearRequestTime()
|
||||
callback(res)
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import Constant from '../utils/constant'
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
export default new Vuex.Store({
|
||||
state: {
|
||||
token: ''
|
||||
},
|
||||
getters: {
|
||||
getToken(state) {
|
||||
if (!state.token) {
|
||||
state.token = localStorage.getItem('token')
|
||||
}
|
||||
return state.token
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
setToken(state, token) {
|
||||
state.token = token
|
||||
localStorage.token = token
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
},
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
</div>
|
||||
<img src="@/assets/home/avatar.png" alt="" style="width: 21px;height: 21px;" />
|
||||
<div class="user-info">
|
||||
{{ userInfo.mobile }}
|
||||
{{ userInfo.username }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -251,7 +251,7 @@ export default {
|
||||
label: '双皮奶'
|
||||
}],
|
||||
userInfo: {
|
||||
mobile: '' // 初始化用户信息
|
||||
username: '' // 初始化用户信息
|
||||
}
|
||||
};
|
||||
},
|
||||
@@ -331,8 +331,8 @@ export default {
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.fetchUserInfo(); // 组件加载时获取用户信息
|
||||
this.getList(); // 初始化设备列表
|
||||
// this.fetchUserInfo(); // 组件加载时获取用户信息
|
||||
// this.getList(); // 初始化设备列表
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -87,18 +87,22 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
fetchCaptcha() {
|
||||
this.captchaUuid = getUUID();
|
||||
if (this.$store.getters.getToken) {
|
||||
goToPage('/home')
|
||||
} else {
|
||||
this.captchaUuid = getUUID();
|
||||
|
||||
Api.user.getCaptcha(this.captchaUuid, (res) => {
|
||||
if (res.status === 200) {
|
||||
const blob = new Blob([res.data], {type: res.data.type});
|
||||
this.captchaUrl = URL.createObjectURL(blob);
|
||||
Api.user.getCaptcha(this.captchaUuid, (res) => {
|
||||
if (res.status === 200) {
|
||||
const blob = new Blob([res.data], {type: res.data.type});
|
||||
this.captchaUrl = URL.createObjectURL(blob);
|
||||
|
||||
} else {
|
||||
console.error('验证码加载异常:', error);
|
||||
showDanger('验证码加载失败,点击刷新')
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.error('验证码加载异常:', error);
|
||||
showDanger('验证码加载失败,点击刷新')
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
async login() {
|
||||
@@ -119,8 +123,12 @@ export default {
|
||||
Api.user.login(this.form, ({data}) => {
|
||||
console.log(data)
|
||||
showSuccess('登陆成功!')
|
||||
|
||||
this.$store.commit('setToken', JSON.stringify(data.data))
|
||||
|
||||
goToPage('/home')
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
this.fetchCaptcha()
|
||||
}, 1000)
|
||||
|
||||
Reference in New Issue
Block a user