joinCURD.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import request from "./request";
  2. import { normal } from "./requestType";
  3. import { encryptByAes } from "./encrypt";
  4. import { mapValues } from "lodash";
  5. import { SHOW_PLAINTEXT } from '@/constant'
  6. // 请求方式默认POST
  7. const post = (type, data, method = "post", loading = false) => {
  8. const requestUrl = `business/v3/sys/universal/${normal[type]}`;
  9. // 非sql类型要关闭loading动画需传参loading:true
  10. const isLoading = type === "sql" ? loading : !loading;
  11. return request({
  12. url: requestUrl,
  13. method,
  14. data: dealData(data, type),
  15. // 开启表单提交加载,查询接口除外
  16. isLoading
  17. });
  18. };
  19. const replaceNullWithEmpty = obj => {
  20. function replaceValue (value) {
  21. if (value === null) {
  22. return "";
  23. } else if (typeof value === "object") {
  24. if (Array.isArray(value)) {
  25. return value.map(item => replaceValue(item));
  26. } else {
  27. return mapValues(value, v => replaceValue(v));
  28. }
  29. } else {
  30. return value;
  31. }
  32. }
  33. return replaceValue(obj);
  34. };
  35. // // 处理数据
  36. const dealData = (args, type) => {
  37. // sql方法特殊处理
  38. if (type === "sql") {
  39. args = {
  40. sql: args.replace(/\n/g, " ")
  41. };
  42. }
  43. const data = typeof args === "object" ? replaceNullWithEmpty(args) : args;
  44. const plaintext = SHOW_PLAINTEXT ? { plaintext: data } : {}
  45. const res = {
  46. ciphertext: encryptByAes(data),
  47. ...plaintext
  48. };
  49. return JSON.stringify(res);
  50. };
  51. export default post;