request.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { ref } from 'vue';
  2. import type { Ref } from 'vue';
  3. import type { AxiosInstance, AxiosRequestConfig } from 'axios';
  4. import { useBoolean, useLoading } from '@/hooks';
  5. import CustomAxiosInstance from './instance';
  6. type RequestMethod = 'get' | 'post' | 'put' | 'delete';
  7. interface RequestParam {
  8. url: string;
  9. method?: RequestMethod;
  10. data?: any;
  11. axiosConfig?: AxiosRequestConfig;
  12. }
  13. /**
  14. * 创建请求
  15. * @param axiosConfig - axios配置
  16. * @param backendConfig - 后端接口字段配置
  17. */
  18. export function createRequest(axiosConfig: AxiosRequestConfig, backendConfig?: Service.BackendResultConfig) {
  19. console.log( "axiosConfig", axiosConfig )
  20. const customInstance = new CustomAxiosInstance(axiosConfig, backendConfig);
  21. /**
  22. * 异步promise请求
  23. * @param param - 请求参数
  24. * - url: 请求地址
  25. * - method: 请求方法(默认get)
  26. * - data: 请求的body的data
  27. * - axiosConfig: axios配置
  28. * - total
  29. */
  30. async function asyncRequest<T>(param: RequestParam): Promise<Service.RequestResult<T>> {
  31. const { url } = param;
  32. const method = param.method || 'get';
  33. const { instance } = customInstance;
  34. const res = (await getRequestResponse({
  35. instance,
  36. method,
  37. url,
  38. data: param.data,
  39. config: param.axiosConfig
  40. })) as Service.RequestResult<T>;
  41. return res;
  42. }
  43. /**
  44. * get请求
  45. * @param url - 请求地址
  46. * @param config - axios配置
  47. */
  48. function get<T>(url: string, config?: AxiosRequestConfig) {
  49. return asyncRequest<T>({ url, method: 'get', axiosConfig: config });
  50. }
  51. /**
  52. * post请求
  53. * @param url - 请求地址
  54. * @param data - 请求的body的data
  55. * @param config - axios配置
  56. */
  57. function post<T>(url: string, data?: any, config?: AxiosRequestConfig) {
  58. return asyncRequest<T>({ url, method: 'post', data, axiosConfig: config });
  59. }
  60. /**
  61. * put请求
  62. * @param url - 请求地址
  63. * @param data - 请求的body的data
  64. * @param config - axios配置
  65. */
  66. function put<T>(url: string, data?: any, config?: AxiosRequestConfig) {
  67. return asyncRequest<T>({ url, method: 'put', data, axiosConfig: config });
  68. }
  69. /**
  70. * delete请求
  71. * @param url - 请求地址
  72. * @param config - axios配置
  73. */
  74. function handleDelete<T>(url: string, config?: AxiosRequestConfig) {
  75. return asyncRequest<T>({ url, method: 'delete', axiosConfig: config });
  76. }
  77. return {
  78. get,
  79. post,
  80. put,
  81. delete: handleDelete
  82. };
  83. }
  84. interface RequestResultHook<T = any> {
  85. data: Ref<T | null>;
  86. error: Ref<Service.RequestError | null>;
  87. loading: Ref<boolean>;
  88. network: Ref<boolean>;
  89. total:Ref<number>;
  90. }
  91. /**
  92. * 创建hooks请求
  93. * @param axiosConfig - axios配置
  94. * @param backendConfig - 后端接口字段配置
  95. */
  96. export function createHookRequest(axiosConfig: AxiosRequestConfig, backendConfig?: Service.BackendResultConfig) {
  97. const customInstance = new CustomAxiosInstance(axiosConfig, backendConfig);
  98. /**
  99. * hooks请求
  100. * @param param - 请求参数
  101. * - url: 请求地址
  102. * - method: 请求方法(默认get)
  103. * - data: 请求的body的data
  104. * - axiosConfig: axios配置
  105. */
  106. function useRequest<T>(param: RequestParam): RequestResultHook<T> {
  107. const { loading, startLoading, endLoading } = useLoading();
  108. const { bool: network, setBool: setNetwork } = useBoolean(window.navigator.onLine);
  109. startLoading();
  110. const data = ref<T | null>(null) as Ref<T | null>;
  111. const error = ref<Service.RequestError | null>(null);
  112. const total = ref<number>() as Ref<number>;
  113. function handleRequestResult(response: any) {
  114. const res = response as Service.RequestResult<T>;
  115. data.value = res.data;
  116. error.value = res.error;
  117. total.value=res.total as number
  118. endLoading();
  119. setNetwork(window.navigator.onLine);
  120. }
  121. const { url } = param;
  122. const method = param.method || 'get';
  123. const { instance } = customInstance;
  124. getRequestResponse({ instance, method, url, data: param.data, config: param.axiosConfig }).then(
  125. handleRequestResult
  126. );
  127. return {
  128. data,
  129. error,
  130. loading,
  131. network,
  132. total
  133. };
  134. }
  135. /**
  136. * get请求
  137. * @param url - 请求地址
  138. * @param config - axios配置
  139. */
  140. function get<T>(url: string, config?: AxiosRequestConfig) {
  141. return useRequest<T>({ url, method: 'get', axiosConfig: config });
  142. }
  143. /**
  144. * post请求
  145. * @param url - 请求地址
  146. * @param data - 请求的body的data
  147. * @param config - axios配置
  148. */
  149. function post<T>(url: string, data?: any, config?: AxiosRequestConfig) {
  150. return useRequest<T>({ url, method: 'post', data, axiosConfig: config });
  151. }
  152. /**
  153. * put请求
  154. * @param url - 请求地址
  155. * @param data - 请求的body的data
  156. * @param config - axios配置
  157. */
  158. function put<T>(url: string, data?: any, config?: AxiosRequestConfig) {
  159. return useRequest<T>({ url, method: 'put', data, axiosConfig: config });
  160. }
  161. /**
  162. * delete请求
  163. * @param url - 请求地址
  164. * @param config - axios配置
  165. */
  166. function handleDelete<T>(url: string, config: AxiosRequestConfig) {
  167. return useRequest<T>({ url, method: 'delete', axiosConfig: config });
  168. }
  169. return {
  170. get,
  171. post,
  172. put,
  173. delete: handleDelete
  174. };
  175. }
  176. async function getRequestResponse(params: {
  177. instance: AxiosInstance;
  178. method: RequestMethod;
  179. url: string;
  180. data?: any;
  181. config?: AxiosRequestConfig;
  182. }) {
  183. const { instance, method, url, data, config } = params;
  184. let res: any;
  185. if (method === 'get' || method === 'delete') {
  186. res = await instance[method](url, config);
  187. } else {
  188. res = await instance[method](url, data, config);
  189. }
  190. return res;
  191. }