index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var common_vendor = require("../../common/vendor.js");
  3. var store_index = require("../../store/index.js");
  4. var utils_request_upload_upload = require("./upload/upload.js");
  5. var core_config_index = require("../../core/config/index.js");
  6. const apiUrl = core_config_index.config.get("apiUrl");
  7. const $http = new utils_request_upload_upload.fileUpload({
  8. baseUrl: apiUrl,
  9. fileUrl: apiUrl,
  10. defaultUploadUrl: "upload/image",
  11. header: {
  12. "content-type": "application/json;charset=utf-8"
  13. },
  14. timeout: 15e3,
  15. config: {
  16. isPrompt: true,
  17. load: true,
  18. isFactory: true
  19. }
  20. });
  21. let requestNum = 0;
  22. $http.requestStart = (options) => {
  23. if (options.load) {
  24. if (requestNum <= 0) {
  25. common_vendor.index.showLoading({
  26. title: "\u52A0\u8F7D\u4E2D",
  27. mask: true
  28. });
  29. }
  30. requestNum += 1;
  31. }
  32. if (options.method == "FILE" && options.maxSize) {
  33. const maxSize = options.maxSize;
  34. for (let item of options.files) {
  35. if (item.size > maxSize) {
  36. setTimeout(() => {
  37. common_vendor.index.showToast({
  38. title: "\u56FE\u7247\u8FC7\u5927\uFF0C\u8BF7\u91CD\u65B0\u4E0A\u4F20",
  39. icon: "none"
  40. });
  41. });
  42. return false;
  43. }
  44. }
  45. }
  46. options.header["api-name"] = store_index.store.getters.platform;
  47. options.header["token"] = store_index.store.getters.token;
  48. return options;
  49. };
  50. $http.requestEnd = (options) => {
  51. if (options.load) {
  52. if (requestNum > 0) {
  53. common_vendor.index.hideLoading();
  54. }
  55. }
  56. };
  57. $http.dataFactory = async (res) => {
  58. requestNum -= 1;
  59. if (requestNum <= 0) {
  60. common_vendor.index.hideLoading();
  61. }
  62. const httpData = res.response;
  63. if (httpData.code === 0) {
  64. common_vendor.index.showToast({
  65. title: httpData.msg,
  66. icon: "none",
  67. duration: 2500
  68. });
  69. return false;
  70. } else if (httpData.code == 401) {
  71. store_index.store.dispatch("Logout");
  72. common_vendor.index.showToast({
  73. title: "\u767B\u5F55\u72B6\u6001\u9519\u8BEF\u6216\u5DF2\u5931\u6548",
  74. icon: "none",
  75. duration: 2500
  76. });
  77. return false;
  78. } else if (httpData.code === 200) {
  79. return Promise.resolve(httpData);
  80. } else {
  81. common_vendor.index.showToast({
  82. title: "\u670D\u52A1\u7AEF\u9519\u8BEF",
  83. icon: "none",
  84. duration: 2500
  85. });
  86. return false;
  87. }
  88. };
  89. $http.requestError = (e) => {
  90. if (e.statusCode === 0) {
  91. throw e;
  92. } else {
  93. setTimeout(() => showRequestError(e), 10);
  94. }
  95. };
  96. const showRequestError = (e) => {
  97. let errMsg = `\u7F51\u7EDC\u8BF7\u6C42\u51FA\u9519\uFF1A${e.errMsg}`;
  98. if (e.errMsg === "request:fail url not in domain list") {
  99. errMsg = "\u5F53\u524DAPI\u57DF\u540D\u672A\u6DFB\u52A0\u5230\u5FAE\u4FE1\u5C0F\u7A0B\u5E8F\u6388\u6743\u540D\u5355 " + e.errMsg;
  100. }
  101. if (e.errMsg === "request:fail") {
  102. errMsg = "\u7F51\u7EDC\u8BF7\u6C42\u9519\u8BEF\uFF0C\u8BF7\u68C0\u67E5\u60A8\u7684\u7F51\u7EDC\u662F\u5426\u6B63\u5E38\uFF01";
  103. }
  104. common_vendor.index.showToast({
  105. title: errMsg,
  106. icon: "none",
  107. duration: 3500
  108. });
  109. };
  110. exports.$http = $http;