request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // /**
  2. // * @description 同步请求封装
  3. // * @param {String} method 请求类型:get,post,delete,put...
  4. // * @param {String} url 请求地址,不包含请求域名,如:/api/test
  5. // * @param {Object} options 请求参数
  6. // * @returns data or error
  7. // */
  8. export const apiUse = async (url, method, options) => {
  9. // 当前locale将被存储在locale变量中
  10. const nuxtApp = useNuxtApp(); // 获取引入nuxt的第三方模块对象
  11. const runConfig = useRuntimeConfig(); // 获取环境变量
  12. const baseUrl = runConfig.public.baseUrl;
  13. const reqUrl = baseUrl + url;
  14. options = {
  15. method,
  16. headers: {
  17. "Content-Type": "application/json",
  18. },
  19. body: { // post 请求参数存放在body内,get请求参数放到params内
  20. ...options,
  21. token: "",
  22. platform: "h5",
  23. language: nuxtApp.$i18n.locale.value,
  24. },
  25. };
  26. const {
  27. data,
  28. error
  29. } = await useFetch(reqUrl, options);
  30. if (error.value) {
  31. return error.value;
  32. } else {
  33. return data.value;
  34. }
  35. };
  36. // /**
  37. // * @description 异步请求封装
  38. // * @param {String} method 请求类型:get,post,delete,put...
  39. // * @param {String} url 请求地址,不包含请求域名,如:/api/test
  40. // * @param {Object} options 请求参数
  41. // * @description key 一定要有,否则刷新会造成接口错误
  42. // */
  43. export const asyncFetchData = async (url, method, options) => {
  44. const nuxtApp = useNuxtApp();
  45. const runConfig = useRuntimeConfig();
  46. const baseUrl = runConfig.public.baseUrl;
  47. const reqUrl = baseUrl + url;
  48. const key = url.split('/')[url.split('/').length - 1] //这里取接口最后的名称做key
  49. const {
  50. data,
  51. pending,
  52. error,
  53. refresh
  54. } = await useAsyncData(key, () => $fetch(reqUrl, {
  55. // useAsyncData也可以用useLazyAsyncData,区别就是useLazyAsyncData会等待SSR渲染完成在执行接口,而useLazyAsyncData是接口执行完成执行SSR渲染
  56. method,
  57. headers: {
  58. "Content-Type": "application/json",
  59. },
  60. body: {
  61. ...options,
  62. token: "",
  63. platform: "h5",
  64. language: nuxtApp.$i18n.locale.value,
  65. },
  66. }));
  67. return {
  68. data,
  69. pending,
  70. error,
  71. refresh
  72. };
  73. }
  74. // /**
  75. // * @description 此为apiUse的封装调用实例
  76. // */
  77. export const fGET = (url, params) => {
  78. return apiUse(url, "GET", params);
  79. };
  80. export const fPOST = (url, params) => {
  81. return apiUse(url, "POST", params);
  82. };
  83. export const fPUT = (url, params) => {
  84. return apiUse(url, "put", params);
  85. };
  86. export const fDELETE = (url, params) => {
  87. return apiUse(url, "delete", params);
  88. };
  89. // /**
  90. // * @description 此为asyncFetchData的调用实例
  91. // */
  92. export const GET = (url, params) => {
  93. return asyncFetchData(url, 'GET', params);
  94. };
  95. export const POST = (url, params) => {
  96. return asyncFetchData(url, "POST", params);
  97. };
  98. export const PUT = (url, params) => {
  99. return asyncFetchData(url, "put", params);
  100. };
  101. export const DELETE = (url, params) => {
  102. return asyncFetchData(url, "delete", params);
  103. };