| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /**
- * request 返回值检测
- */
- import {
- common
- } from "../utils";
- import {
- createSSEParser
- } from "../utils/sseParse";
- const app = getApp()
- const promistHandle = res => {
- // 前端判断返回的参数是不是加密数据,是的话先解密
- if ((typeof res.data.data == 'string') && res.data.data.constructor == String) {
- res.data = JSON.parse(common.desDecrypt(res.data.data, getApp().globalData.apiSecretKey));
- }
- //有些接口没有返回RespCode 通过resultCode判断
- let resp = ""
- if (res.data.RespCode == '10000' || res.data.resultCode == '0') {
- // 请求成功
- resp = []
- //getUnitePay res.data.Data是个对象
- if (res.data.Data instanceof Array || res.data.Data instanceof Object) {
- resp = res.data.Data;
- } else if (res.data.WxPayConfigKey || res.data.resultCode == '0' || res.data.configKey || res.data.result || res
- .data.clientId) {
- resp = res.data;
- }
- } else {
- // 503 - 小程序下线、维护中,跳转到对应页面
- if (res.data.RespCode == 503) {
- jumpErrorPage()
- }
- //如果401 token失效 不处理 返回""
- if (res.data.RespCode != '401') {
- resp = res.data.RespMessage || res.data.errMsg;
- }
- }
- return resp;
- }
- const catchPromise = (resp, callBack, showModal = true) => {
- // 如果不是是object类型 说明是抛出的异常 ""是401
- if (resp === "") {
- return false;
- }
- if (typeof resp != 'object') {
- if (!showModal) {
- return false;
- }
- common.showModal(resp)
- return false;
- } else {
- // 否则执行回调
- if (callBack) {
- return callBack()
- }
- }
- }
- /**
- * 新的处理方法 promistHandleNew 会暴露出res.data 方便医院个性化处理res.data.RespCode逻辑
- * catchPromiseNew 添加提示报错信息后的点击确定回调方法
- */
- /**
- * * let resp = handle.promistHandleNew(await request.doPost(url, queryData))
- * //showModal 报错是否提示 showModalCallBack 报错提示按钮点击的回调
- * return handle.catchPromiseNew(resp, () => resp,{showModal:true,showModalCallBack:()=>{}})
- */
- const promistHandleNew = res => {
- // 前端判断返回的参数是不是加密数据,是的话先解密
- if ((typeof res.data.data == 'string') && res.data.data.constructor == String) {
- res.data = JSON.parse(common.desDecrypt(res.data.data, getApp().globalData.apiSecretKey));
- }
- //有些接口没有返回RespCode 通过resultCode判断
- let resp = ""
- if (res.data.RespCode == '10000' || res.data.resultCode == '0') {
- // 请求成功
- resp = []
- //getUnitePay res.data.Data是个对象
- if (res.data.Data instanceof Array || res.data.Data instanceof Object) {
- resp = res.data.Data;
- } else if (res.data.WxPayConfigKey || res.data.resultCode == '0' || res.data.configKey || res.data.result || res
- .data.clientId) {
- resp = res.data;
- }
- } else {
- // 503 - 小程序下线、维护中,跳转到对应页面
- if (res.data.RespCode == 503) {
- jumpErrorPage()
- }
- //如果401 token失效 不处理 返回""
- if (res.data.RespCode != '401') {
- resp = res.data.RespMessage || res.data.errMsg;
- }
- }
- return {
- resp,
- resData: res.data
- };
- }
- const catchPromiseNew = (data, callBack, option = {
- showModal: true,
- showModalCallBack: () => {}
- }) => {
- // 如果不是是object类型 说明是抛出的异常 ""是401
- if (data.resp === "") {
- return {
- resp: false,
- resData: data.resData
- };
- }
- if (typeof data.resp != 'object') {
- if (!option.showModal || data.resData.RespCode == 503) {
- return {
- resp: false,
- resData: data.resData
- };
- }
- common.showModal(data.resp, option.showModalCallBack)
- return {
- resp: false,
- resData: data.resData
- };
- } else {
- // 否则执行回调
- if (callBack) {
- return callBack()
- }
- }
- }
- const jumpErrorPage = () => {
- const [currentPage] = getCurrentPages()
- const maintainPage = "pages/st1/business/errorPage/maintain/maintain"
- const offlinePage = "pages/st1/business/errorPage/offline/offline"
- const isMaintainPage = currentPage.route.indexOf(maintainPage) > 0
- const isOfflinePage = currentPage.route.indexOf(offlinePage) > 0
- let jumpPage = ""
- if (res.data.RespCode == "maintain" && !isMaintainPage) {
- jumpPage = maintainPage
- }
- if (res.data.RespCode == "offline" && !isOfflinePage) {
- jumpPage = offlinePage
- }
- jumpPage && uni.reLaunch({
- url: `/${jumpPage}`,
- })
- }
- function arrayBufferToString(arrayBuffer) {
- // 创建一个 Uint8Array 视图
- let binary = '';
- const bytes = new Uint8Array(arrayBuffer);
- const len = bytes.byteLength;
- for (let i = 0; i < len; i++) {
- binary += String.fromCharCode(bytes[i]);
- }
- return decodeURIComponent(escape(binary)); // 处理UTF-8编码
- }
- const sseChunkDataHandle = (onChunkReceived) => {
- let ssePaser = createSSEParser(onChunkReceived);
- return (res) => {
- try {
- let data = arrayBufferToString(res.data);
- console.log("==>", data)
- ssePaser.parse(data);
- } catch (e) {
- console.error(e)
- }
- }
- }
- export default {
- promistHandle,
- catchPromise,
- promistHandleNew,
- catchPromiseNew,
- sseChunkDataHandle
- }
|