create.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { login } from "../logic";
  2. import { request as _request } from "../wx/network";
  3. import { getAccountInfoSync } from "../wx/open-api";
  4. const shareRequestCache = new Map<string, IRequestData<any>>();
  5. const miniProgram = getAccountInfoSync();
  6. export function createRequest(option: IRequestCreateConfig) {
  7. const { baseURL } = option;
  8. return async function request<R, T>(config: IRequestConfig<R, T>) {
  9. let {
  10. url,
  11. data,
  12. params,
  13. header,
  14. meta,
  15. notTransform = false,
  16. transform = (params: any) => params,
  17. shareRequest,
  18. ..._config
  19. } = config;
  20. if (config.method === "GET") {
  21. data = params;
  22. } else if (params) {
  23. const query = Object.entries(params).map(
  24. ([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`
  25. );
  26. url += `?${query.join("&")}`;
  27. }
  28. const key = url;
  29. if (shareRequest && shareRequestCache.has(key)) {
  30. return shareRequestCache.get(key) as unknown as PromiseLike<
  31. IRequestData<T>
  32. >;
  33. }
  34. header ??= {};
  35. header["Authorization"] = meta?.ignoreToken
  36. ? ""
  37. : (await option.token?.()) ?? "";
  38. header["patientId"] = wx.getStorageSync("patientId") ?? "";
  39. // header['patientId'] = '783';
  40. header["doctorId"] = wx.getStorageSync("doctorId") ?? "";
  41. header["scene"] = wx.getStorageSync("scene") ?? "";
  42. header["appId"] = miniProgram.appId ?? "";
  43. header["version"] = miniProgram.version ?? "";
  44. header["env"] = miniProgram.envVersion ?? "";
  45. const promise = _request<IRequestData<T>>({
  46. url: /https?\:\/\//.test(url) ? url : `${baseURL}${url}`,
  47. header,
  48. data,
  49. ..._config,
  50. }).then((response) => {
  51. if (response.statusCode === 200) {
  52. if (notTransform) return response.data;
  53. if (response.data.code === 200 && response.data.success !== false) {
  54. const data =
  55. Array.isArray(response.data.rows) && response.data.total != null
  56. ? { data: response.data.rows, total: response.data.total }
  57. : response.data.data;
  58. return transform({ data: data as any, header: response.header });
  59. }
  60. throw {
  61. errMsg: response.data.msg || `app:${response.data.code}`,
  62. errno: `060202${response.data.code}`,
  63. };
  64. }
  65. throw {
  66. errMsg: `${response.errMsg}:${response.statusCode}`,
  67. errno: `060201${response.statusCode}`,
  68. };
  69. });
  70. if (shareRequest) shareRequestCache.set(key, <any>promise);
  71. return (promise as any).catch(async (error: { errno: string }) => {
  72. shareRequestCache.delete(key);
  73. if (error.errno === "060201401") {
  74. await login(true, "重新登录中");
  75. wx.showLoading({ title: "重新加载中" });
  76. try {
  77. return await request(config);
  78. } catch (error) {
  79. throw error;
  80. } finally {
  81. wx.hideLoading();
  82. }
  83. }
  84. throw error;
  85. });
  86. };
  87. }