request.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const utils_common = require("../utils/common.js");
  4. require("./handle.js");
  5. const hook_useSmallProgramLogin_index = require("../hook/use-small-program-login/index.js");
  6. let _app;
  7. const httpPost = (url, data, options = {}, complete = void 0) => {
  8. let has = getApp().globalData.accountInfo.miniProgram.envVersion != "develop";
  9. let querData = {};
  10. if (utils_common.isNotEmpty(getApp().globalData.noNeedSecretKeyApiArr)) {
  11. for (let i of getApp().globalData.noNeedSecretKeyApiArr) {
  12. if (url.indexOf(i) != "-1") {
  13. has = false;
  14. break;
  15. }
  16. }
  17. }
  18. if (url.indexOf("callApiJSON") == -1 && url.indexOf("callApi.do") == -1) {
  19. has = false;
  20. }
  21. let showLoading = true;
  22. if (options != void 0 && options.showLoading != void 0) {
  23. showLoading = options.showLoading;
  24. }
  25. if (showLoading) {
  26. utils_common.showLoading();
  27. }
  28. let header = {
  29. "content-type": "application/json",
  30. token: getApp().globalData.token || common_vendor.index.getStorageSync("token"),
  31. "call-appId": common_vendor.index.getStorageSync("frontEndConfig").fixedAppjsGlobalData.appId
  32. };
  33. if (has && utils_common.isNotEmpty(getApp().globalData.apiSecretKey)) {
  34. header.appId = "KASIET_alismaillpro", querData.data = utils_common.desEncrypt(JSON.stringify(data), getApp().globalData.apiSecretKey);
  35. }
  36. if (options) {
  37. const extOptions = [
  38. "showLoading",
  39. // 显示加载中
  40. "onChunkReceived",
  41. // 分块传输监听
  42. "timeout",
  43. // 超时时间, 默认取/app.json里的networkTimeout配置
  44. "responseType"
  45. // 响应结果
  46. ];
  47. Object.keys(options).forEach((key) => {
  48. if (!extOptions.includes(key)) {
  49. header[key] = options[key];
  50. }
  51. });
  52. }
  53. return new Promise((resolve, reject) => {
  54. const enableChunked = options && typeof options.onChunkReceived == "function";
  55. const requestOptions = {
  56. url,
  57. data: utils_common.isEmpty(querData) ? data : querData,
  58. method: "POST",
  59. header,
  60. enableChunked,
  61. // 新增分块数据
  62. success(res) {
  63. if (showLoading) {
  64. utils_common.hideLoading();
  65. }
  66. if (!complete) {
  67. if (options && options.construction) {
  68. resolve({ data: { Data: res.data, RespCode: 1e4 } });
  69. } else {
  70. resolve(res);
  71. }
  72. }
  73. },
  74. fail(error) {
  75. if (showLoading) {
  76. utils_common.hideLoading();
  77. }
  78. if (!complete) {
  79. reject(error);
  80. }
  81. common_vendor.index.__f__("error", "at service/request.ts:83", error);
  82. utils_common.showToast(`连接超时,请重试`);
  83. },
  84. complete(res) {
  85. if (complete) {
  86. resolve(res);
  87. }
  88. }
  89. };
  90. if (options && options.timeout != void 0 && options.timeout != null) {
  91. requestOptions.timeout = options.timeout;
  92. }
  93. if (options && options.responseType != void 0 && options.responseType != null) {
  94. requestOptions.responseType = options.responseType;
  95. }
  96. const wxRequest = common_vendor.index.request(requestOptions);
  97. if (enableChunked) {
  98. wxRequest.onChunkReceived(options.onChunkReceived);
  99. }
  100. });
  101. };
  102. const doPost = async (url, data, options = {}, complete = void 0) => {
  103. const response = httpPost(url, data, options, complete);
  104. let loginAgain = false;
  105. await response.then((resp) => {
  106. if (resp.data.RespCode == 401) {
  107. loginAgain = true;
  108. }
  109. });
  110. if (!loginAgain) {
  111. return response;
  112. }
  113. if (common_vendor.index.getStorageSync("isCall") != 1) {
  114. common_vendor.index.setStorageSync("isCall", 1);
  115. common_vendor.index.setStorageSync("data_expiration", 0);
  116. let retry2 = getApp().globalData.retryNum;
  117. while (retry2 > 0) {
  118. await hook_useSmallProgramLogin_index.useSmallProgramLogin(_app).then((resp) => {
  119. if (resp.data.RespCode != "10000") {
  120. retry2--;
  121. } else {
  122. retry2 = 0;
  123. }
  124. });
  125. if (retry2 != 0) {
  126. await sleep(300);
  127. }
  128. }
  129. }
  130. let retry = getApp().globalData.retryNum;
  131. while (retry > 0) {
  132. if (common_vendor.index.getStorageSync("isCall") != 1) {
  133. return httpPost(url, data, options);
  134. } else {
  135. await sleep(500);
  136. retry--;
  137. }
  138. }
  139. return httpPost(url, data, options);
  140. };
  141. const doGet = async (url, data, options = {}) => {
  142. const response = httpGet(url, data, options);
  143. let loginAgain = false;
  144. await response.then((resp) => {
  145. if (resp.data.RespCode == 401) {
  146. loginAgain = true;
  147. }
  148. });
  149. if (!loginAgain) {
  150. return response;
  151. }
  152. if (common_vendor.index.getStorageSync("isCall") != 1) {
  153. common_vendor.index.setStorageSync("isCall", 1);
  154. common_vendor.index.setStorageSync("data_expiration", 0);
  155. let retry2 = getApp().globalData.retryNum;
  156. while (retry2 > 0) {
  157. await hook_useSmallProgramLogin_index.useSmallProgramLogin(_app).then((resp) => {
  158. if (resp.data.RespCode != "10000") {
  159. retry2--;
  160. } else {
  161. retry2 = 0;
  162. }
  163. });
  164. if (retry2 != 0) {
  165. await sleep(300);
  166. }
  167. }
  168. }
  169. let retry = getApp().globalData.retryNum;
  170. while (retry > 0) {
  171. if (common_vendor.index.getStorageSync("isCall") != 1) {
  172. return httpGet(url, data, options);
  173. } else {
  174. await sleep(500);
  175. retry--;
  176. }
  177. }
  178. return httpGet(url, data, options);
  179. };
  180. const httpGet = (url, data, options = {}) => {
  181. let showLoading = true;
  182. if (options != void 0 && options.showLoading != void 0) {
  183. showLoading = options.showLoading;
  184. }
  185. if (showLoading) {
  186. utils_common.showLoading();
  187. }
  188. let header = {
  189. "content-type": "application/json",
  190. token: getApp().globalData.token || common_vendor.index.getStorageSync("token"),
  191. "call-appId": common_vendor.index.getStorageSync("frontEndConfig").fixedAppjsGlobalData.appId
  192. };
  193. if (options) {
  194. Object.keys(options).forEach((key) => {
  195. if (key != "showLoading") {
  196. header[key] = options[key];
  197. }
  198. });
  199. }
  200. return new Promise((resolve, reject) => {
  201. common_vendor.index.request({
  202. url,
  203. data,
  204. method: "GET",
  205. header,
  206. success(res) {
  207. if (showLoading) {
  208. utils_common.hideLoading();
  209. }
  210. if (options && options.construction) {
  211. resolve({ data: { Data: res.data, RespCode: 1e4 } });
  212. } else {
  213. resolve(res);
  214. }
  215. },
  216. fail(error) {
  217. if (showLoading) {
  218. utils_common.hideLoading();
  219. }
  220. reject(error);
  221. utils_common.showToast(`连接超时,请重试`);
  222. }
  223. });
  224. });
  225. };
  226. const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  227. const request = {
  228. doPost,
  229. doGet
  230. };
  231. exports.request = request;
  232. //# sourceMappingURL=../../.sourcemap/mp-alipay/service/request.js.map