feat: 完成用户登录界面,api还需完善

This commit is contained in:
539943419 committed 2025-01-14 02:10:39 +08:00
1 parent 4c69f57aaa
commit d7dcc473a9
18 files changed
+681 -129

No files matched your search

+10 -1
View File
@@ -35,6 +35,14 @@ export const useUserStore = defineStore("user", () => {
_token.value = value;
localStorage.setItem("miao-token", value);
};
const logout = () => {
setLoginStatus(false);
_token.value = undefined;
_tokenExpireTime.value = undefined;
localStorage.removeItem("miao-token");
localStorage.removeItem("miao-token-expire-time");
};
return {
isLogin,
setLoginStatus,
@@ -43,7 +51,8 @@ export const useUserStore = defineStore("user", () => {
tokenExpireTime,
setTokenExpireTime,
_tokenExpireTime,
_token
_token,
logout
};
});
-27
View File
@@ -1,27 +0,0 @@
export const uploadPost = (body: {
title: string,
summary: string,
type: string,
tags: string[],
url: string,
date: number,
content: string
}) => $fetch('/api/posts/uploadPost', {
method: 'POST',
body
})
export const updatePost = (body: {
id: number,
title: string,
summary: string,
type: string,
tags: string[],
url: string,
date: number,
content: string
}) => $fetch('/api/posts/uploadPost', {
method: 'POST',
body
})
+106
View File
@@ -0,0 +1,106 @@
import needRegister from "~/server/api/user/needRegister";
import useUserStore from "./pinia/useUserStore"
export default function useFetch() {
const userStore = useUserStore();
return {
post: {
uploadPost: (body: {
title: string,
summary: string,
type: string,
tags: string[],
url: string,
date: number,
content: string
}) => $fetch('/api/posts/uploadPost', {
method: 'POST',
body,
headers: {
'Authorization': `Bearer ${userStore.token}`
}
}),
updatePost: (body: {
id: number,
title: string,
summary: string,
type: string,
tags: string[],
url: string,
date: number,
content: string
}) => $fetch('/api/posts/editPost', {
method: 'POST',
body,
headers: {
'Authorization': `Bearer ${userStore.token}`
}
}),
getPost: (id: number) => $fetch(`/api/posts/getPost?id=${id}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${userStore.token}`
}
}),
getPostList: (body: {
page?: number,
tag?: string
}) => $fetch('/api/posts/getPostList', {
method: 'POST',
body,
headers: {
'Authorization': `Bearer ${userStore.token}`
}
}),
},
user: {
login: (body: {
username: string,
password: string
}) => $fetch('/api/user/login', {
method: 'POST',
body
}),
register: (body: {
username: string,
password: string
}) => $fetch('/api/user/register', {
method: 'POST',
body
}),
needRegister: () => $fetch('/api/user/needRegister'),
refreshToken: () => $fetch('/api/user/refreshToken', {
method: 'POST',
headers: {
'Authorization': `Bearer ${userStore.token}`
}
}),
// 暂未实现
updatePassword: (body: {
oldPassword: string,
newPassword: string
}) => $fetch('/api/user/updatePassword', {
method: 'POST',
body,
headers: {
'Authorization': `Bearer ${userStore.token}`
}
})
},
}
}
export const testAuth = () => {
const userStore = useUserStore();
(async () => {
const res = await $fetch('/api/test/testDir', {
method: 'GET',
headers: {
'Authorization': `Bearer ${userStore.token}`
}
})
console.log(res)
})()
}