attach.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. throw new Error("请求错误");
  103. }
  104. if (res.code !== Code.SUSSESS) {
  105. throw new Error(res.msg);
  106. } else {
  107. return res.data;
  108. }
  109. },
  110. urls: urls,
  111. });
  112. // sceneCode附加
  113. export const attachSceneCode = <K extends AxiosStatic<Interface>>(
  114. baseAxios: K,
  115. urls: typeof URL.codeURLS
  116. ) =>
  117. baseAxios.addIntercept({
  118. reqHandler() {
  119. return {
  120. paths: { sceneCode: params.m },
  121. };
  122. },
  123. urls,
  124. });
  125. const uploadUrls = [URL.uploadFile] as const;
  126. // 文件上传处理
  127. export const attachUpload = <K extends AxiosStatic<Interface>>(
  128. baseAxios: K,
  129. urls: typeof uploadUrls
  130. ) =>
  131. baseAxios.addIntercept({
  132. reqHandler(config) {
  133. const form = new FormData();
  134. if (config.data instanceof Blob) {
  135. form.append("file", config.data);
  136. } else {
  137. for (const key in config.data as any) {
  138. form.append(key, config.data[key]);
  139. }
  140. }
  141. return {
  142. headers: {
  143. "Content-Type": "application/x-www-form-urlencoded;charset:UTF-8",
  144. },
  145. data: form,
  146. };
  147. },
  148. urls,
  149. });
  150. export default (baseAxios: AxiosStatic<Interface>) => {
  151. attachLoadding(baseAxios, URL.loadURLS as any);
  152. attachError(baseAxios, URL.errorTipURLS as any);
  153. attachAnalysis(baseAxios, URL.disassembleURLS as any);
  154. // attachUploadDown(baseAxios, uploadDownUrls)
  155. // token校验拦截
  156. const axios = baseAxios
  157. .addIntercept({
  158. reqHandler() {
  159. return {
  160. paths: { sceneCode: params.m },
  161. };
  162. },
  163. urls: URL.codeURLS,
  164. })
  165. .addIntercept({
  166. reqHandler(config) {
  167. const form = new FormData();
  168. if (config.data instanceof Blob) {
  169. form.append("file", config.data);
  170. } else {
  171. for (const key in config.data as any) {
  172. form.append(key, config.data[key]);
  173. }
  174. }
  175. return {
  176. headers: {
  177. "Content-Type": "application/x-www-form-urlencoded;charset:UTF-8",
  178. },
  179. data: form,
  180. };
  181. },
  182. urls: [URL.uploadFile] as const,
  183. });
  184. return axios;
  185. };