handle.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const utils_common = require("../utils/common.js");
  4. const utils_sseParse = require("../utils/sseParse.js");
  5. getApp();
  6. const promistHandle = (res2) => {
  7. if (typeof res2.data.data == "string" && res2.data.data.constructor == String) {
  8. res2.data = JSON.parse(utils_common.desDecrypt(res2.data.data, getApp().globalData.apiSecretKey));
  9. }
  10. let resp = "";
  11. if (res2.data.RespCode == "10000" || res2.data.resultCode == "0") {
  12. resp = [];
  13. if (res2.data.Data instanceof Array || res2.data.Data instanceof Object) {
  14. resp = res2.data.Data;
  15. } else if (res2.data.WxPayConfigKey || res2.data.resultCode == "0" || res2.data.configKey || res2.data.result || res2.data.clientId) {
  16. resp = res2.data;
  17. }
  18. } else {
  19. if (res2.data.RespCode == 503) {
  20. jumpErrorPage();
  21. }
  22. if (res2.data.RespCode != "401") {
  23. resp = res2.data.RespMessage || res2.data.errMsg;
  24. }
  25. }
  26. return resp;
  27. };
  28. const catchPromise = (resp, callBack, showModal = true) => {
  29. if (resp === "") {
  30. return false;
  31. }
  32. if (typeof resp != "object") {
  33. if (!showModal) {
  34. return false;
  35. }
  36. utils_common.showModal(resp);
  37. return false;
  38. } else {
  39. if (callBack) {
  40. return callBack();
  41. }
  42. }
  43. };
  44. const promistHandleNew = (res2) => {
  45. if (typeof res2.data.data == "string" && res2.data.data.constructor == String) {
  46. res2.data = JSON.parse(utils_common.desDecrypt(res2.data.data, getApp().globalData.apiSecretKey));
  47. }
  48. let resp = "";
  49. if (res2.data.RespCode == "10000" || res2.data.resultCode == "0") {
  50. resp = [];
  51. if (res2.data.Data instanceof Array || res2.data.Data instanceof Object) {
  52. resp = res2.data.Data;
  53. } else if (res2.data.WxPayConfigKey || res2.data.resultCode == "0" || res2.data.configKey || res2.data.result || res2.data.clientId) {
  54. resp = res2.data;
  55. }
  56. } else {
  57. if (res2.data.RespCode == 503) {
  58. jumpErrorPage();
  59. }
  60. if (res2.data.RespCode != "401") {
  61. resp = res2.data.RespMessage || res2.data.errMsg;
  62. }
  63. }
  64. return {
  65. resp,
  66. resData: res2.data
  67. };
  68. };
  69. const catchPromiseNew = (data, callBack, option = {
  70. showModal: true,
  71. showModalCallBack: () => {
  72. }
  73. }) => {
  74. if (data.resp === "") {
  75. return {
  76. resp: false,
  77. resData: data.resData
  78. };
  79. }
  80. if (typeof data.resp != "object") {
  81. if (!option.showModal || data.resData.RespCode == 503) {
  82. return {
  83. resp: false,
  84. resData: data.resData
  85. };
  86. }
  87. utils_common.showModal(data.resp, option.showModalCallBack);
  88. return {
  89. resp: false,
  90. resData: data.resData
  91. };
  92. } else {
  93. if (callBack) {
  94. return callBack();
  95. }
  96. }
  97. };
  98. const jumpErrorPage = () => {
  99. const [currentPage] = getCurrentPages();
  100. const maintainPage = "pages/st1/business/errorPage/maintain/maintain";
  101. const offlinePage = "pages/st1/business/errorPage/offline/offline";
  102. const isMaintainPage = currentPage.route.indexOf(maintainPage) > 0;
  103. const isOfflinePage = currentPage.route.indexOf(offlinePage) > 0;
  104. let jumpPage = "";
  105. if (res.data.RespCode == "maintain" && !isMaintainPage) {
  106. jumpPage = maintainPage;
  107. }
  108. if (res.data.RespCode == "offline" && !isOfflinePage) {
  109. jumpPage = offlinePage;
  110. }
  111. jumpPage && common_vendor.index.reLaunch({
  112. url: `/${jumpPage}`
  113. });
  114. };
  115. function arrayBufferToString(arrayBuffer) {
  116. let binary = "";
  117. const bytes = new Uint8Array(arrayBuffer);
  118. const len = bytes.byteLength;
  119. for (let i = 0; i < len; i++) {
  120. binary += String.fromCharCode(bytes[i]);
  121. }
  122. return decodeURIComponent(escape(binary));
  123. }
  124. const sseChunkDataHandle = (onChunkReceived) => {
  125. let ssePaser = utils_sseParse.createSSEParser(onChunkReceived);
  126. return (res2) => {
  127. try {
  128. let data = arrayBufferToString(res2.data);
  129. common_vendor.index.__f__("log", "at service/handle.js:173", "==>", data);
  130. ssePaser.parse(data);
  131. } catch (e) {
  132. common_vendor.index.__f__("error", "at service/handle.js:176", e);
  133. }
  134. };
  135. };
  136. const handle = {
  137. promistHandle,
  138. catchPromise,
  139. promistHandleNew,
  140. catchPromiseNew,
  141. sseChunkDataHandle
  142. };
  143. exports.handle = handle;
  144. //# sourceMappingURL=../../.sourcemap/mp-alipay/service/handle.js.map