联调后端成功
This commit is contained in:
parent
48616ad69e
commit
dd7801b273
2
App.vue
2
App.vue
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(() => {
|
||||
// 从全局存储中读取新添加的日程数据
|
||||
|
|
|
|||
|
|
@ -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.token(Pinia 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 做异步操作
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user