diff --git a/openapi-ts-request.config.ts b/openapi-ts-request.config.ts index 84c6ddf..18ce1dc 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: 'http://petstore.swagger.io/v2/swagger.json', + schemaPath: 'https://ukw0y1.laf.run/unibest-opapi-test.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 f6b70a1..3d8e591 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-ts-request": "openapi-ts", + "openapi": "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 7aa7bc6..df8d663 100644 --- a/src/hooks/useRequest.ts +++ b/src/hooks/useRequest.ts @@ -19,14 +19,14 @@ interface IUseRequestReturn { /** * useRequest是一个定制化的请求钩子,用于处理异步请求和响应。 - * @param func 一个执行异步请求的函数,返回一个包含响应数据的Promise。 + * @param func 一个执行异步请求的函数,返回HttpRequestResult或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, + func: (args?: P) => HttpRequestResult | Promise>, options: IUseRequestOptions = { immediate: false }, ): IUseRequestReturn { const loading = ref(false) @@ -36,21 +36,24 @@ export default function useRequest( const run = async (args?: P) => { loading.value = true - 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 - }) + 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 cancel = () => { diff --git a/src/http/http.ts b/src/http/http.ts index 2e1dd4a..fad25f3 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, IResponse } from '@/http/types' +import type { CustomRequestOptions, HttpRequestResult, 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) { +export function http(options: CustomRequestOptions): HttpRequestResult { 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 31d1eb3..b8c5db4 100644 --- a/src/http/vue-query.ts +++ b/src/http/vue-query.ts @@ -1,4 +1,4 @@ -import type { CustomRequestOptions } from '@/http/types' +import type { CustomRequestOptions, HttpRequestResult } 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 a4e84f3..0535e04 100644 --- a/src/pages/about/about.vue +++ b/src/pages/about/about.vue @@ -3,6 +3,7 @@ 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' @@ -113,6 +114,7 @@ onShow(() => { +