联调后端成功

This commit is contained in:
WindowBird 2025-11-06 09:07:22 +08:00
parent 48616ad69e
commit dd7801b273
3 changed files with 32 additions and 4 deletions

View File

@ -8,7 +8,7 @@
// token token
const token = uni.getStorageSync('token')
if (!token) {
const testToken = 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjFiOTJmMjU0LTg1NjAtNDI3OC1hNjhkLWU2NzM2OTY5MTQyNiJ9.Zq4WXvpZ84hqoST2AhOzdrSTkpCs_ySzVLGc53bQvJi8Z9rsBDOXx4wv9wfaNw-djzK7rGEwcvh0sl_9koUGfw' // token
const testToken = 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjdlMmE1MjgwLTllYWQtNDVhMS04ZTA0LWYyNWRkZmEzMDYwMiJ9.sJH7nZEzqbfPqTFZYF8FrzT7GwlTO7OggGkxd7381naSfcaflEBgeCoV2xSVUAoKpAl4xeH0vT-e1cr5abcu9A'
uni.setStorageSync('token', testToken)
console.log('已设置测试 token:', testToken)
}

View File

@ -44,6 +44,7 @@
<script setup>
import { ref, computed, watch, onMounted } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import { useUserStore } from '@/store/user';
import FabPlus from '@/components/FabPlus.vue';
import AddEventModal from '@/components/AddEventModal.vue';
@ -123,6 +124,24 @@ function addEvent(e) {
const value=ref(0);
//
onMounted(() => {
// /getInfo auth: true token
const userStore = useUserStore();
console.log('当前 token:', userStore.token);
uni.$uv.http.get('/getInfo', {
custom: {
auth: true // token
}
}).then(res => {
console.log('getInfo 接口返回:', res);
}).catch(err => {
console.error('getInfo 接口请求失败:', err);
});
});
//
onShow(() => {
//

View File

@ -8,7 +8,7 @@ export const Request = () => {
// 初始化请求配置
uni.$uv.http.setConfig((config) => {
/* config 为默认全局配置*/
config.baseURL = 'https://www.example.com'; /* 根域名 */
config.baseURL = 'http://192.168.2.128:4001'; /* 根域名 */
return config
})
@ -21,11 +21,20 @@ export const Request = () => {
if(config?.custom?.auth) {
// 使用 Pinia store 获取 token
const userStore = useUserStore()
const token = userStore.getToken || userStore.token
// 直接使用 state.tokenPinia getter 可以直接访问)
const token = userStore.token
if (token) {
config.header.token = token
// 使用标准的 Authorization header添加 Bearer 前缀
config.header.Authorization = `Bearer ${token}`
} else {
console.warn('请求拦截器token 为空,无法添加认证头')
}
} else {
console.log('请求拦截器:未启用 auth跳过 token 添加')
}
return config
}, config => { // 可使用async await 做异步操作