handle.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * request 返回值检测
  3. */
  4. import {
  5. common
  6. } from "../utils";
  7. import {
  8. createSSEParser
  9. } from "../utils/sseParse";
  10. const app = getApp()
  11. const promistHandle = res => {
  12. // 前端判断返回的参数是不是加密数据,是的话先解密
  13. if ((typeof res.data.data == 'string') && res.data.data.constructor == String) {
  14. res.data = JSON.parse(common.desDecrypt(res.data.data, getApp().globalData.apiSecretKey));
  15. }
  16. //有些接口没有返回RespCode 通过resultCode判断
  17. let resp = ""
  18. if (res.data.RespCode == '10000' || res.data.resultCode == '0') {
  19. // 请求成功
  20. resp = []
  21. //getUnitePay res.data.Data是个对象
  22. if (res.data.Data instanceof Array || res.data.Data instanceof Object) {
  23. resp = res.data.Data;
  24. } else if (res.data.WxPayConfigKey || res.data.resultCode == '0' || res.data.configKey || res.data.result || res
  25. .data.clientId) {
  26. resp = res.data;
  27. }
  28. } else {
  29. // 503 - 小程序下线、维护中,跳转到对应页面
  30. if (res.data.RespCode == 503) {
  31. jumpErrorPage()
  32. }
  33. //如果401 token失效 不处理 返回""
  34. if (res.data.RespCode != '401') {
  35. resp = res.data.RespMessage || res.data.errMsg;
  36. }
  37. }
  38. return resp;
  39. }
  40. const catchPromise = (resp, callBack, showModal = true) => {
  41. // 如果不是是object类型 说明是抛出的异常 ""是401
  42. if (resp === "") {
  43. return false;
  44. }
  45. if (typeof resp != 'object') {
  46. if (!showModal) {
  47. return false;
  48. }
  49. common.showModal(resp)
  50. return false;
  51. } else {
  52. // 否则执行回调
  53. if (callBack) {
  54. return callBack()
  55. }
  56. }
  57. }
  58. /**
  59. * 新的处理方法 promistHandleNew 会暴露出res.data 方便医院个性化处理res.data.RespCode逻辑
  60. * catchPromiseNew 添加提示报错信息后的点击确定回调方法
  61. */
  62. /**
  63. * * let resp = handle.promistHandleNew(await request.doPost(url, queryData))
  64. * //showModal 报错是否提示 showModalCallBack 报错提示按钮点击的回调
  65. * return handle.catchPromiseNew(resp, () => resp,{showModal:true,showModalCallBack:()=>{}})
  66. */
  67. const promistHandleNew = res => {
  68. // 前端判断返回的参数是不是加密数据,是的话先解密
  69. if ((typeof res.data.data == 'string') && res.data.data.constructor == String) {
  70. res.data = JSON.parse(common.desDecrypt(res.data.data, getApp().globalData.apiSecretKey));
  71. }
  72. //有些接口没有返回RespCode 通过resultCode判断
  73. let resp = ""
  74. if (res.data.RespCode == '10000' || res.data.resultCode == '0') {
  75. // 请求成功
  76. resp = []
  77. //getUnitePay res.data.Data是个对象
  78. if (res.data.Data instanceof Array || res.data.Data instanceof Object) {
  79. resp = res.data.Data;
  80. } else if (res.data.WxPayConfigKey || res.data.resultCode == '0' || res.data.configKey || res.data.result || res
  81. .data.clientId) {
  82. resp = res.data;
  83. }
  84. } else {
  85. // 503 - 小程序下线、维护中,跳转到对应页面
  86. if (res.data.RespCode == 503) {
  87. jumpErrorPage()
  88. }
  89. //如果401 token失效 不处理 返回""
  90. if (res.data.RespCode != '401') {
  91. resp = res.data.RespMessage || res.data.errMsg;
  92. }
  93. }
  94. return {
  95. resp,
  96. resData: res.data
  97. };
  98. }
  99. const catchPromiseNew = (data, callBack, option = {
  100. showModal: true,
  101. showModalCallBack: () => {}
  102. }) => {
  103. // 如果不是是object类型 说明是抛出的异常 ""是401
  104. if (data.resp === "") {
  105. return {
  106. resp: false,
  107. resData: data.resData
  108. };
  109. }
  110. if (typeof data.resp != 'object') {
  111. if (!option.showModal || data.resData.RespCode == 503) {
  112. return {
  113. resp: false,
  114. resData: data.resData
  115. };
  116. }
  117. common.showModal(data.resp, option.showModalCallBack)
  118. return {
  119. resp: false,
  120. resData: data.resData
  121. };
  122. } else {
  123. // 否则执行回调
  124. if (callBack) {
  125. return callBack()
  126. }
  127. }
  128. }
  129. const jumpErrorPage = () => {
  130. const [currentPage] = getCurrentPages()
  131. const maintainPage = "pages/st1/business/errorPage/maintain/maintain"
  132. const offlinePage = "pages/st1/business/errorPage/offline/offline"
  133. const isMaintainPage = currentPage.route.indexOf(maintainPage) > 0
  134. const isOfflinePage = currentPage.route.indexOf(offlinePage) > 0
  135. let jumpPage = ""
  136. if (res.data.RespCode == "maintain" && !isMaintainPage) {
  137. jumpPage = maintainPage
  138. }
  139. if (res.data.RespCode == "offline" && !isOfflinePage) {
  140. jumpPage = offlinePage
  141. }
  142. jumpPage && uni.reLaunch({
  143. url: `/${jumpPage}`,
  144. })
  145. }
  146. function arrayBufferToString(arrayBuffer) {
  147. // 创建一个 Uint8Array 视图
  148. let binary = '';
  149. const bytes = new Uint8Array(arrayBuffer);
  150. const len = bytes.byteLength;
  151. for (let i = 0; i < len; i++) {
  152. binary += String.fromCharCode(bytes[i]);
  153. }
  154. return decodeURIComponent(escape(binary)); // 处理UTF-8编码
  155. }
  156. const sseChunkDataHandle = (onChunkReceived) => {
  157. let ssePaser = createSSEParser(onChunkReceived);
  158. return (res) => {
  159. try {
  160. let data = arrayBufferToString(res.data);
  161. console.log("==>", data)
  162. ssePaser.parse(data);
  163. } catch (e) {
  164. console.error(e)
  165. }
  166. }
  167. }
  168. export default {
  169. promistHandle,
  170. catchPromise,
  171. promistHandleNew,
  172. catchPromiseNew,
  173. sseChunkDataHandle
  174. }