From 5bd9b1a2e26f2d1eecb957d4d9dadf97db29e6de Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 4 Dec 2023 21:33:37 +0800 Subject: [PATCH] test --- src/components/HelloWorld.vue | 5 ++-- src/plugins/http.ts | 6 ++-- src/plugins/library.ts | 53 +++++++++++++++++++++++++++++++++++ src/store/config.ts | 25 +++++++++++++++++ src/store/libraries.ts | 12 ++++++++ src/store/user.ts | 39 ++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 src/plugins/library.ts create mode 100644 src/store/config.ts create mode 100644 src/store/libraries.ts create mode 100644 src/store/user.ts diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index 8f712b8..2300e41 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -7,7 +7,6 @@ diff --git a/src/plugins/http.ts b/src/plugins/http.ts index a625fbb..589ca2e 100644 --- a/src/plugins/http.ts +++ b/src/plugins/http.ts @@ -1,8 +1,8 @@ import axios from "axios"; // import router from "@/router"; -import {useUserStore} from "@/stores/user"; -// const config = useConfigStore() -import config from "@/plugins/config" +// import {useUserStore} from "@/stores/user"; +// // const config = useConfigStore() +// import config from "@/plugins/config" let axios_config = { baseURL: config.baseUrl, diff --git a/src/plugins/library.ts b/src/plugins/library.ts new file mode 100644 index 0000000..7c61499 --- /dev/null +++ b/src/plugins/library.ts @@ -0,0 +1,53 @@ +import { DocumentsApi, Configuration } from "../openapi"; + +import axios from "axios"; +import router from "@/router"; +// import {useUserStore} from "@/stores/user"; +// // const config = useConfigStore() +// import config from "@/plugins/config" + + +// instance.interceptors.request.use( +// (config) => { +// if (config.headers === undefined) { +// // config.headers = {}; +// } + +// config.headers["Accept"] = "application/json"; + +// // @ts-ignore +// if (process.env.NODE_ENV === "development") { +// // user.jwt_token 取中间 +// config.headers["X-Jwt-Payload"] = user.jwt_token?.split(".")[1] +// } else { +// config.headers["Authorization"] = "Bearer " + user.jwt_token; +// } + +// return config; +// }, +// (error) => { +// console.error("axios request error", error); +// return Promise.reject(error); +// } +// ); + +// instance.interceptors.response.use( +// (res) => { +// return Promise.resolve(res); +// }, +// (error) => { +// console.error("axios response error", error); + +// return Promise.reject(error); +// } +// ); + +// export default instance; + + +const conf = new Configuration +conf.basePath = "https://document.awa.im/api" +conf.apiKey = "a" +const a = new DocumentsApi(conf); + +export default a diff --git a/src/store/config.ts b/src/store/config.ts new file mode 100644 index 0000000..122287a --- /dev/null +++ b/src/store/config.ts @@ -0,0 +1,25 @@ +import {defineStore} from 'pinia' +// import axios from "axios"; + +export const useConfigStore = defineStore('app', { + state: () => ({ + appName: "资料库", + description: "Leaf Library", + accountServer: "https://httpbin.awa.im", + }), + actions: { + getAppName(): string { + return this.appName !== null ? this.appName : "Leaf Library" + }, + getRefreshUrl(): string { + return this.accountServer + "/public/auth_request/refresh" + }, + getLoginUrl(): string { + let url = new URL(this.accountServer + "/public/auth_request") + url.searchParams.append("description", this.description) + // url.searchParams.append("callback_uri", window.location.href) + url.searchParams.append("attributes[app]", "todo") + return url.toString() + } + }, +}) \ No newline at end of file diff --git a/src/store/libraries.ts b/src/store/libraries.ts new file mode 100644 index 0000000..c2e2101 --- /dev/null +++ b/src/store/libraries.ts @@ -0,0 +1,12 @@ +import {defineStore} from 'pinia' +import axios from "axios"; +import {useConfigStore} from "./config"; + +export const useLibrariesStore = defineStore('libraries', { + state: () => ({ + libraries: [{ + ID: 0, + Name: "", + }], + }), +}) \ No newline at end of file diff --git a/src/store/user.ts b/src/store/user.ts new file mode 100644 index 0000000..c4f9d0d --- /dev/null +++ b/src/store/user.ts @@ -0,0 +1,39 @@ +import {defineStore} from 'pinia' +import axios from "axios"; +import {useConfigStore} from "./config"; + +export const useUserStore = defineStore('user', { + persist: true, + state: () => ({ + refresh_token: null as null | string, + jwt_token: null as null | string, + }), + getters: { + isLoggedIn(): boolean { + return this.jwt_token !== null + } + }, + actions: { + login(refresh_token: string, jwt_token: string) { + this.refresh_token = refresh_token + this.jwt_token = jwt_token + }, + logout() { + console.log("logout") + this.refresh_token = null + this.jwt_token = null + }, + refresh() { + let config = useConfigStore() + + axios.post(config.getRefreshUrl(), { + refresh_token: this.refresh_token + }).then(r => { + this.jwt_token = r.data.token + }).catch(e => { + console.error(e) + this.logout() + }) + } + } +}) \ No newline at end of file