http.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. accessToken: getApp<any>().globalData.token || "",
  39. ...(this.header || {}),
  40. ...(options.header || {}),
  41. };
  42. const data = options.data || {};
  43. const params = options.params || {};
  44. const query = Object.keys(params)
  45. .map(
  46. (key) =>
  47. `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
  48. )
  49. .join("&");
  50. WxRequest<IResponse<R>>({
  51. url: `${url}${query ? (urlHasParams ? "&" : "?") : ""}${query}`,
  52. method,
  53. data,
  54. header,
  55. })
  56. .then((res) => {
  57. // console.log(res)
  58. // 刷新token 已废弃
  59. // if (res.code === 888) {
  60. // // eslint-disable-next-line promise/no-nesting
  61. // return WxRequest<IResponse<R>>({
  62. // url: `${url}${query ? (urlHasParams ? "&" : "?") : ""}${query}`,
  63. // method,
  64. // data,
  65. // header: {
  66. // ...header,
  67. // Authorization: getApp<any>().globalData.token || "",
  68. // },
  69. // });
  70. // }
  71. if ([21005, 21000, 10001].includes(Number(res.code))) {
  72. const hasToken = getApp<any>().globalData.token;
  73. clearToken();
  74. if (hasToken) {
  75. setTimeout(() => {
  76. uni.reLaunch({
  77. url: "/pages/map/map",
  78. });
  79. }, 1500);
  80. }
  81. throw {
  82. errMsg: "请重新登录",
  83. };
  84. }
  85. return res;
  86. })
  87. .then((res) => {
  88. // eslint-disable-next-line no-console
  89. // console.log("接口返回", res);
  90. const { message = '' } = res || {
  91. msg: "出现错误",
  92. };
  93. if (res && message === "ok") {
  94. resolve(res.data);
  95. } else {
  96. throw {
  97. errMsg: message,
  98. };
  99. }
  100. })
  101. .catch((err) => {
  102. if (err && err.errMsg && options && options.statusCodeHandle) {
  103. uni.showToast({
  104. title: `${err.errMsg}`,
  105. icon: "none",
  106. });
  107. }
  108. reject(err);
  109. });
  110. });
  111. }
  112. get<R = any>(api: string, options?: IOptions) {
  113. return this.request<R>("GET", api, options);
  114. }
  115. post<R = any>(api: string, options?: IOptions) {
  116. return this.request<R>("POST", api, options);
  117. }
  118. put<R = any>(api: string, options?: IOptions) {
  119. return this.request<R>("PUT", api, options);
  120. }
  121. }
  122. export function WxRequest<T = any>(
  123. option: UniNamespace.RequestOptions
  124. ): Promise<T> {
  125. return new Promise((resolve, reject) => {
  126. uni.request({
  127. ...option,
  128. success(res: any) {
  129. // console.log('微信返回', res)
  130. const { statusCode, header } = res;
  131. // if (header["Authorization"]) {
  132. // setToken(header["Authorization"]);
  133. // resolve({
  134. // ...res.data,
  135. // code: 888,
  136. // });
  137. // return;
  138. // }
  139. if (statusCode > 200) {
  140. reject({
  141. errMsg: `${option.url}:${statusCode}`,
  142. });
  143. }
  144. resolve(res.data);
  145. },
  146. fail: reject,
  147. });
  148. });
  149. }
  150. export default Http;
  151. export const http = new Http();