attach.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { AxiosStatic } from "./setup";
  2. import Interface from "./interfaces";
  3. import * as URL from "./url";
  4. import { Loading } from "@kankan/components/index";
  5. import { useParams } from "@/hook/useParams";
  6. import { encodePassword } from "@/utils";
  7. import { Code, errTip } from "./code";
  8. import { status, StatusEum } from "@/store/setup";
  9. import axios from 'axios'
  10. const params = useParams();
  11. type URLAll = {
  12. [key in keyof Interface]: {
  13. [index in keyof Interface[key]]: "url" extends keyof Interface[key][index]
  14. ? Interface[key][index]["url"]
  15. : never;
  16. }[keyof Interface[key]];
  17. }[keyof Interface];
  18. // 密码加密
  19. export const attachPwdencode = <
  20. T extends readonly URLAll[],
  21. K extends AxiosStatic<Interface>
  22. >(
  23. baseAxios: K,
  24. urls: T
  25. ) =>
  26. baseAxios.addIntercept({
  27. reqHandler(data: any) {
  28. const ret: any = {};
  29. if (data.data.password) {
  30. ret.password = encodePassword(data.data.password);
  31. }
  32. if (data.data.confirmPwd) {
  33. ret.confirmPwd = ret.password;
  34. }
  35. return {
  36. data: ret,
  37. };
  38. },
  39. urls,
  40. });
  41. // loadding
  42. export const attachLoadding = <
  43. T extends readonly URLAll[],
  44. K extends AxiosStatic<Interface>
  45. >(
  46. baseAxios: K,
  47. urls: T
  48. ) =>
  49. baseAxios.addIntercept({
  50. reqHandler() {
  51. console.log("open loadding");
  52. Loading.show();
  53. },
  54. resHandler(data) {
  55. console.log("close loadding");
  56. setTimeout(() => Loading.hide());
  57. return data;
  58. },
  59. errHandler() {
  60. console.log("close loadding");
  61. setTimeout(() => Loading.hide());
  62. },
  63. urls,
  64. });
  65. // 错误提示
  66. export const attachError = <
  67. T extends readonly URLAll[],
  68. K extends AxiosStatic<Interface>
  69. >(
  70. baseAxios: K,
  71. urls: T
  72. ) =>
  73. // 错误提示
  74. baseAxios.addIntercept({
  75. resHandler(res: any) {
  76. if (res.code !== Code.SUSSESS) {
  77. errTip(res.code, res.msg);
  78. return null;
  79. }
  80. return res;
  81. },
  82. errHandler(res) {
  83. if (res.status === -1) {
  84. status.value = StatusEum.disconnect;
  85. } else {
  86. status.value = StatusEum.serverErr;
  87. }
  88. },
  89. urls,
  90. });
  91. // 结构拆解
  92. export const attachAnalysis = <
  93. T extends readonly URLAll[],
  94. K extends AxiosStatic<Interface>
  95. >(
  96. baseAxios: K,
  97. urls: T
  98. ) =>
  99. baseAxios.addIntercept({
  100. resHandler(res: any) {
  101. if (!res) {
  102. console.log("======>", res);
  103. throw new Error("请求错误");
  104. }
  105. if (res.code !== Code.SUSSESS) {
  106. throw new Error(res.msg);
  107. } else {
  108. return res.data;
  109. }
  110. },
  111. urls: urls,
  112. });
  113. // sceneCode附加
  114. export const attachSceneCode = <K extends AxiosStatic<Interface>>(
  115. baseAxios: K,
  116. urls: typeof URL.codeURLS
  117. ) =>
  118. baseAxios.addIntercept({
  119. reqHandler() {
  120. return {
  121. paths: { sceneCode: params.m },
  122. };
  123. },
  124. urls,
  125. });
  126. const uploadUrls = [URL.uploadFile] as const;
  127. // 文件上传处理
  128. export const attachUpload = <K extends AxiosStatic<Interface>>(
  129. baseAxios: K,
  130. urls: typeof uploadUrls
  131. ) =>
  132. baseAxios.addIntercept({
  133. reqHandler(config) {
  134. const form = new FormData();
  135. if (config.data instanceof Blob) {
  136. form.append("file", config.data);
  137. } else {
  138. for (const key in config.data as any) {
  139. form.append(key, config.data[key]);
  140. }
  141. }
  142. return {
  143. headers: {
  144. "Content-Type": "application/x-www-form-urlencoded;charset:UTF-8",
  145. },
  146. data: form,
  147. };
  148. },
  149. urls,
  150. });
  151. export default (baseAxios: AxiosStatic<Interface>) => {
  152. attachLoadding(baseAxios, URL.loadURLS as any);
  153. attachError(baseAxios, URL.errorTipURLS as any);
  154. attachAnalysis(baseAxios, URL.disassembleURLS as any);
  155. // attachUploadDown(baseAxios, uploadDownUrls)
  156. // token校验拦截
  157. const axios = baseAxios
  158. .addIntercept({
  159. reqHandler() {
  160. return {
  161. paths: { sceneCode: params.m },
  162. };
  163. },
  164. urls: URL.codeURLS,
  165. })
  166. .addIntercept({
  167. reqHandler(config) {
  168. const form = new FormData();
  169. if (config.data instanceof Blob) {
  170. form.append("file", config.data);
  171. } else {
  172. for (const key in config.data as any) {
  173. form.append(key, config.data[key]);
  174. }
  175. }
  176. return {
  177. headers: {
  178. "Content-Type": "application/x-www-form-urlencoded;charset:UTF-8",
  179. },
  180. data: form,
  181. };
  182. },
  183. urls: [URL.uploadFile] as const,
  184. });
  185. return axios;
  186. };