li.shaoyi 2 gadi atpakaļ
vecāks
revīzija
a9396a4e33
1 mainītis faili ar 68 papildinājumiem un 82 dzēšanām
  1. 68 82
      src/services/http/index@next.ts

+ 68 - 82
src/services/http/index@next.ts

@@ -64,7 +64,7 @@ const httpService = new (class {
         )
     }
 
-    request = async (url: string, method: Method, payload?: unknown) => {
+    private request = async (url: string, method: Method, payload?: unknown) => {
         const config = await service.onReady()
         this.axiosInstance.defaults.baseURL = config.goCommonSearchUrl
         const requestConfig: AxiosRequestConfig = {
@@ -83,14 +83,24 @@ const httpService = new (class {
         return this.axiosInstance(requestConfig)
     }
 
-    httpRequest = async <T>(url: string, method: Method, payload?: unknown, errMsg?: string) => {
+    commonRequest = async <T>(url: string, method: Method, payload: unknown, errMsg?: string) => {
+        return await this.request(url, method, payload).then((res) => {
+            const data = res.data as T
+            return Promise.resolve(data)
+        }).catch((err) => {
+            const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '请求失败,请稍后重试')
+            return Promise.reject(msg)
+        })
+    }
+
+    httpRequest = async <T>(url: string, method: Method, payload: unknown, errMsg?: string) => {
         return await this.request(url, method, payload).then((res) => {
             const data = res.data as HttpResponse<T>
             switch (data.code) {
                 case ResultCode.InvalidToken:
                     return Promise.reject('令牌无效')
                 case ResultCode.Success:
-                    return Promise.resolve(data.data)
+                    return Promise.resolve(data)
                 default:
                     return Promise.reject(data.msg)
             }
@@ -104,92 +114,68 @@ const httpService = new (class {
 export const { httpRequest } = httpService
 
 
-function queryLoginId(params: string) {
-    return httpRequest<Model.LoginQueryRsp>('/User/GetLoginID', 'get', params)
+function queryLoginData(params: Model.LoginQueryReq) {
+    return httpRequest<HttpResponse<Model.LoginQueryRsp>>('/User/LoginQuery', 'get', params, 'LoginQuery');
 }
 
 interface RequestOptions<Req, Rsp> {
     params: Req;
-    pagination: boolean;
+    pagination?: boolean;
     onSuccess?: (res: Rsp) => void;
-    onFail: (err: string) => void;
+    onFail?: (err: string) => void;
+}
+
+function useRequest<Req extends object, Rsp>(runAsync: (params: Req) => Promise<Rsp>, options: RequestOptions<Req, Rsp>) {
+    const loading = ref(false)
+
+    if (options.pagination) {
+        const { dataList, filters, total, pageIndex, pageSize, pageCount } = useDataTable<Rsp>()
+
+        const defaultParams = {
+            page: pageIndex.value,
+            pagesize: pageSize.value,
+            ...options.params
+        }
+
+        const run = (payload: Partial<Req> = {}) => {
+            runAsync({ ...options.params, ...payload }).then((res) => {
+                if (Array.isArray(res)) {
+                    dataList.value = res
+                }
+                options.onSuccess && options.onSuccess(res)
+            })
+        }
+    } else {
+        const data = ref<Rsp>()
+
+    }
+
+
+
+    const cancel = () => {
+        console.log('取消请求')
+    }
+
+    onUnmounted(() => {
+        cancel()
+    })
+
+    return {
+        loading,
+        data,
+        run,
+        cancel,
+    }
 }
 
-// function useRequest<Req, Rsp>(fnAsync: (params: Req) => Promise<Rsp>, options: RequestOptions<Req, Rsp>) {
-//     const loading = ref(false)
-
-//     if (options.pagination) {
-//         const { dataList, filters, total, pageIndex, pageSize, pageCount } = useDataTable<Rsp>()
-
-//         const defaultParams = {
-//             page: pageIndex.value,
-//             pagesize: pageSize.value,
-//             ...options.params
-//         }
-
-//         const run = (payload?: Partial<Req>) => {
-//             fnAsync({ ...defaultParams, ...payload }).then((res) => {
-//                 if (Array.isArray(res)) {
-//                     dataList.value = res
-//                 }
-//                 options.onSuccess && options.onSuccess(res)
-//             })
-//         }
-//     } else {
-//         const data = ref<Rsp>()
-
-//     }
-
-
-
-//     const cancel = () => {
-//         console.log('取消请求')
-//     }
-
-//     onUnmounted(() => {
-//         cancel()
-//     })
-
-//     return {
-//         loading,
-//         data,
-//         run,
-//         cancel,
-//     }
-// }
-
-// function usePaginationRequest<Rsp, Req>(aa: (params: Req) => Promise<Rsp>, options: { params: Req, success?: (res: Rsp) => void }) {
-//     const { dataList, filters, total, pageIndex, pageSize, pageCount } = useDataTable<Rsp>()
-//     const { loading, data, run, cancel } = useRequest(aa, options)
-
-//     return {
-//         loading,
-//         dataList,
-//         filters,
-//         total,
-//         pageIndex,
-//         pageSize,
-//         pageCount,
-//         run,
-//         cancel,
-//     }
-// }
-
-// const { run, data } = useRequest(queryLoginId, {
-//     data: {
-//         page: pageIndex.value,
-//         pagesize: pageSize.value,
-//         pageflag: 1,
-//         accountID: firstAccountId.value.toString(),
-//     },
-//     success: (res) => {
-//         total.value = res.total
-//         dataList.value = res.data
-//     },
-//     complete: () => {
-//         loading.value = false
-//     }
-// })
+const { run, data } = useRequest(queryLoginData, {
+    params: {
+        loginID: 0,
+    },
+    onSuccess: (res) => {
+        console.log(res)
+    },
+})
 
 /**
  * 获取服务配置地址