From 3b14ab942ff0bfe3b765f01d4e43449e10b74fca Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Tue, 23 Sep 2025 18:01:20 +0800 Subject: [PATCH] Revert "Merge branch 'openapi'" This reverts commit 9a6f0acdf04827330535cc6fcfcdb9200da98638, reversing changes made to 08a81e433bbc5aeb4b9eddc62a24d73628a4af2d. --- openapi-ts-request.config.ts | 2 +- package.json | 2 +- src/hooks/useRequest.ts | 37 +- src/http/http.ts | 4 +- src/http/vue-query.ts | 4 +- src/pages/about/about.vue | 2 - .../about/components/request-openapi.vue | 67 ---- src/service/displayEnumLabel.ts | 13 + src/service/index.ts | 6 +- src/service/info.ts | 18 - src/service/listAll.ts | 18 - src/service/pet.ts | 185 +++++++++ src/service/store.ts | 72 ++++ src/service/types.ts | 353 +++++++++++++++++- src/service/user.ts | 150 ++++++++ 15 files changed, 784 insertions(+), 149 deletions(-) delete mode 100644 src/pages/about/components/request-openapi.vue create mode 100644 src/service/displayEnumLabel.ts delete mode 100644 src/service/info.ts delete mode 100644 src/service/listAll.ts create mode 100644 src/service/pet.ts create mode 100644 src/service/store.ts create mode 100644 src/service/user.ts diff --git a/openapi-ts-request.config.ts b/openapi-ts-request.config.ts index 18ce1dc..84c6ddf 100644 --- a/openapi-ts-request.config.ts +++ b/openapi-ts-request.config.ts @@ -2,7 +2,7 @@ import type { GenerateServiceProps } from 'openapi-ts-request' export default [ { - schemaPath: 'https://ukw0y1.laf.run/unibest-opapi-test.json', + schemaPath: 'http://petstore.swagger.io/v2/swagger.json', serversPath: './src/service', requestLibPath: `import request from '@/http/vue-query';\n import { CustomRequestOptions } from '@/http/types';`, requestOptionsType: 'CustomRequestOptions', diff --git a/package.json b/package.json index 3d8e591..f6b70a1 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,7 @@ "build:quickapp-webview-huawei": "uni build -p quickapp-webview-huawei", "build:quickapp-webview-union": "uni build -p quickapp-webview-union", "type-check": "vue-tsc --noEmit", - "openapi": "openapi-ts", + "openapi-ts-request": "openapi-ts", "prepare": "git init && husky && node ./scripts/create-base-files.js", "docker:prepare": "node ./scripts/create-base-files.js", "lint": "eslint", diff --git a/src/hooks/useRequest.ts b/src/hooks/useRequest.ts index df8d663..7aa7bc6 100644 --- a/src/hooks/useRequest.ts +++ b/src/hooks/useRequest.ts @@ -19,14 +19,14 @@ interface IUseRequestReturn { /** * useRequest是一个定制化的请求钩子,用于处理异步请求和响应。 - * @param func 一个执行异步请求的函数,返回HttpRequestResult或Promise>。 + * @param func 一个执行异步请求的函数,返回一个包含响应数据的Promise。 * @param options 包含请求选项的对象 {immediate, initialData}。 * @param options.immediate 是否立即执行请求,默认为false。 * @param options.initialData 初始化数据,默认为undefined。 * @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。 */ export default function useRequest( - func: (args?: P) => HttpRequestResult | Promise>, + func: (args?: P) => HttpRequestResult, options: IUseRequestOptions = { immediate: false }, ): IUseRequestReturn { const loading = ref(false) @@ -36,24 +36,21 @@ export default function useRequest( const run = async (args?: P) => { loading.value = true - try { - // 支持同步和异步函数 - const result = await func(args) - const { promise, requestTask: task } = result - requestTask = task // Store the requestTask - - const res = await promise - data.value = res - error.value = false - return data.value - } - catch (err) { - error.value = err instanceof Error ? err : new Error('Request failed') - throw err - } - finally { - loading.value = false - } + const { promise, requestTask: task } = func(args) + requestTask = task // Store the requestTask + return promise + .then((res) => { + data.value = res + error.value = false + return data.value + }) + .catch((err) => { + error.value = err + throw err + }) + .finally(() => { + loading.value = false + }) } const cancel = () => { diff --git a/src/http/http.ts b/src/http/http.ts index fad25f3..2e1dd4a 100644 --- a/src/http/http.ts +++ b/src/http/http.ts @@ -1,5 +1,5 @@ import type { IDoubleTokenRes } from '@/api/types/login' -import type { CustomRequestOptions, HttpRequestResult, IResponse } from '@/http/types' +import type { CustomRequestOptions, IResponse } from '@/http/types' import { nextTick } from 'vue' import { LOGIN_PAGE } from '@/router/config' import { useTokenStore } from '@/store/token' @@ -10,7 +10,7 @@ import { ResultEnum } from './tools/enum' let refreshing = false // 防止重复刷新 token 标识 let taskQueue: { resolve: (value: any) => void, reject: (reason?: any) => void, options: CustomRequestOptions }[] = [] as { resolve: (value: any) => void, reject: (reason?: any) => void, options: CustomRequestOptions }[] // 刷新 token 请求队列 -export function http(options: CustomRequestOptions): HttpRequestResult { +export function http(options: CustomRequestOptions) { let requestTask: UniApp.RequestTask | undefined const promise = new Promise((resolve, reject) => { requestTask = uni.request({ diff --git a/src/http/vue-query.ts b/src/http/vue-query.ts index b8c5db4..31d1eb3 100644 --- a/src/http/vue-query.ts +++ b/src/http/vue-query.ts @@ -1,4 +1,4 @@ -import type { CustomRequestOptions, HttpRequestResult } from '@/http/types' +import type { CustomRequestOptions } from '@/http/types' import { http } from './http' /* @@ -10,7 +10,7 @@ export default function request( params?: Record headers?: Record }, -): HttpRequestResult { +) { const requestOptions = { url, ...options, diff --git a/src/pages/about/about.vue b/src/pages/about/about.vue index 0535e04..a4e84f3 100644 --- a/src/pages/about/about.vue +++ b/src/pages/about/about.vue @@ -3,7 +3,6 @@ import { isApp, isAppAndroid, isAppHarmony, isAppIOS, isAppPlus, isH5, isMpWeixi import { LOGIN_PAGE } from '@/router/config' import { useTokenStore } from '@/store' import { tabbarStore } from '@/tabbar/store' -import RequestCompOpenApi from './components/request-openapi.vue' import RequestComp from './components/request.vue' import VBindCss from './components/VBindCss.vue' @@ -114,7 +113,6 @@ onShow(() => { -