create.ts 2.8 KB

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