http.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { clearToken } from "../api/auth";
  2. interface IOptions {
  3. data?: any;
  4. params?: any;
  5. header?: any;
  6. statusCodeHandle?: boolean;
  7. }
  8. interface IResponse<T> {
  9. code: number;
  10. data: T;
  11. message: string;
  12. }
  13. class Http {
  14. private baseUrl: string;
  15. private header?: any;
  16. constructor(baseUrl?: string, header?: any) {
  17. this.baseUrl = baseUrl || "";
  18. if (header) {
  19. this.header = header;
  20. }
  21. }
  22. request<R = any>(
  23. method: "GET" | "POST" | "PUT",
  24. api: string,
  25. options?: IOptions
  26. ): Promise<R> {
  27. return new Promise((resolve, reject) => {
  28. if (!options) {
  29. options = {
  30. statusCodeHandle: true,
  31. };
  32. }
  33. const url = this.baseUrl + api;
  34. const urlHasParams = url.indexOf("?") >= 0;
  35. const header = {
  36. // 'content-type': 'application/json',
  37. satoken: getApp<any>().globalData.token || "",
  38. ...(this.header || {}),
  39. ...(options.header || {}),
  40. };
  41. const data = options.data || {};
  42. const params = options.params || {};
  43. const query = Object.keys(params)
  44. .map(
  45. (key) =>
  46. `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
  47. )
  48. .join("&");
  49. WxRequest<IResponse<R>>({
  50. url: `${url}${query ? (urlHasParams ? "&" : "?") : ""}${query}`,
  51. method,
  52. data,
  53. header,
  54. })
  55. .then((res) => {
  56. // console.log(res)
  57. // 刷新token 已废弃
  58. // if (res.code === 888) {
  59. // // eslint-disable-next-line promise/no-nesting
  60. // return WxRequest<IResponse<R>>({
  61. // url: `${url}${query ? (urlHasParams ? "&" : "?") : ""}${query}`,
  62. // method,
  63. // data,
  64. // header: {
  65. // ...header,
  66. // Authorization: getApp<any>().globalData.token || "",
  67. // },
  68. // });
  69. // }
  70. if ([21005, 21000, 10001].includes(Number(res.code))) {
  71. clearToken();
  72. setTimeout(() => {
  73. uni.reLaunch({
  74. url: "/pages/map/index",
  75. });
  76. }, 1000);
  77. throw {
  78. errMsg: "请重新登录",
  79. };
  80. }
  81. return res;
  82. })
  83. .then((res) => {
  84. // eslint-disable-next-line no-console
  85. console.log("接口返回", res);
  86. const { message = '' } = res || {
  87. msg: "出现错误",
  88. };
  89. if (res && message === "ok") {
  90. resolve(res.data);
  91. } else {
  92. throw {
  93. errMsg: message,
  94. };
  95. }
  96. })
  97. .catch((err) => {
  98. if (err && err.errMsg && options && options.statusCodeHandle) {
  99. uni.showToast({
  100. title: `${err.errMsg}`,
  101. icon: "none",
  102. });
  103. }
  104. reject(err);
  105. });
  106. });
  107. }
  108. get<R = any>(api: string, options?: IOptions) {
  109. return this.request<R>("GET", api, options);
  110. }
  111. post<R = any>(api: string, options?: IOptions) {
  112. return this.request<R>("POST", api, options);
  113. }
  114. put<R = any>(api: string, options?: IOptions) {
  115. return this.request<R>("PUT", api, options);
  116. }
  117. }
  118. export function WxRequest<T = any>(
  119. option: UniNamespace.RequestOptions
  120. ): Promise<T> {
  121. return new Promise((resolve, reject) => {
  122. uni.request({
  123. ...option,
  124. success(res: any) {
  125. // console.log('微信返回', res)
  126. const { statusCode, header } = res;
  127. // if (header["Authorization"]) {
  128. // setToken(header["Authorization"]);
  129. // resolve({
  130. // ...res.data,
  131. // code: 888,
  132. // });
  133. // return;
  134. // }
  135. if (statusCode > 200) {
  136. reject({
  137. errMsg: `${option.url}:${statusCode}`,
  138. });
  139. }
  140. resolve(res.data);
  141. },
  142. fail: reject,
  143. });
  144. });
  145. }
  146. export default Http;
  147. export const http = new Http();