UNPKG

ty-mobile-uni

Version:

uView UI,是uni-app生态优秀的UI框架,全面的组件和便捷的工具会让您信手拈来,如鱼得水

468 lines (463 loc) 17.7 kB
import Vue from 'vue'; import { baseUrl,token} from '../../../config/env.js'; import apiJson from './api.js'; let tokenVal = token ? token : 'yxTokenWx'; var ajaxJSON = {}; let showLoading = 0; //加载遮罩层 //封装post异步请求 /* 使用说明 1.self,url,data,callbackSuccess 4个参数固定顺序 2.callbackFail,loadFlag,reqMoreFlag 先后顺序不做要求 3.callbackFail:请求返回失败时的回调函数 4.loadFlag:是否弹出遮罩层,默认为true 5.reqMoreFlag:异步请求未返回结果时,该url是否可以发起多次请求, 0:否(默认值,防止手快多次点击);1:是(接口可多次请求) 6.entireResponse:是否返回完整的接口信息 2:否(默认值,返回responseObject内的信息);3:是(返回完成的接口信息,包含resultCode) */ const post = function (self,url,data,callbackSuccess){ //console.log(" start ajax"); let callbackFail = ''; let loadFlag = true; let reqMoreFlag = 0; let entireResponse = 2; if(self.type.fun(arguments[4])){ callbackFail = arguments[4]; }else if(self.type.bool(arguments[4])){ loadFlag = arguments[4]; }else if(self.type.num(arguments[4])){ if(arguments[4]===0 || arguments[4]===1){ reqMoreFlag = arguments[4]; }else if(arguments[4]===2 || arguments[4]===3){ entireResponse = arguments[4]; } } if(self.type.fun(arguments[5])){ callbackFail = arguments[5]; }else if(self.type.bool(arguments[5])){ loadFlag = arguments[5]; }else if(self.type.num(arguments[5])){ // reqMoreFlag = arguments[5]; if(arguments[5]===0 || arguments[5]===1){ reqMoreFlag = arguments[5]; }else if(arguments[5]===2 || arguments[5]===3){ entireResponse = arguments[5]; } } if(self.type.fun(arguments[6])){ callbackFail = arguments[6]; }else if(self.type.bool(arguments[6])){ loadFlag = arguments[6]; }else if(self.type.num(arguments[6])){ // reqMoreFlag = arguments[6]; if(arguments[6]===0 || arguments[6]===1){ reqMoreFlag = arguments[6]; }else if(arguments[6]===2 || arguments[6]===3){ entireResponse = arguments[6]; } } if(self.type.fun(arguments[7])){ callbackFail = arguments[7]; }else if(self.type.bool(arguments[7])){ loadFlag = arguments[7]; }else if(self.type.num(arguments[7])){ // reqMoreFlag = arguments[7]; if(arguments[7]===0 || arguments[7]===1){ reqMoreFlag = arguments[7]; }else if(arguments[7]===2 || arguments[7]===3){ entireResponse = arguments[7]; } } if(reqMoreFlag == 0){ //该接口未返回请求结果时,只能请求一次 if(ajaxJSON[url] == 1){ //console.log('只请求一次') return; } } ajaxJSON[url] = 1; //500ms后才弹出遮罩层 setTimeout(()=>{ if(loadFlag){ showLoading++; //第一个请求超过500ms,弹出加载遮罩层 if(showLoading == 1){ uni.showLoading({ title: '加载中', mask: true, }); } } },500) //console.log(arguments[3]) try { uni.request({ url: baseUrl+url, method: 'POST', data: JSON.stringify({ "contractRoot": { "svcCont": { "requestObject": data }, "tcpCont": { "appKey": "string", "dstSysId": "string", "reqTime": 0, "rspTime": 0, "sign": "string", "svcCode": "string", "transactionId": "string", "version": "string" } } }), dataType: 'json', header:{ 'Accept': 'application/json', 'Content-Type': 'application/json', 'authorization': uni.getStorageSync(tokenVal), 'appId': this.appId, //'authorization': dd.getStorageSync({ key: 'token487192742724767843' }).data }, success: (res)=> { delete ajaxJSON[url]; //console.log(" end ajax success"); if(loadFlag && showLoading){ showLoading--; //最后一个请求关闭加载遮罩层 if(showLoading == 0){ uni.hideLoading(); } } loadFlag = false; let info = res.data.contractRoot.svcCont; /* console.log("本次请求结果。。。") console.log("data=%o", info); console.log(ajaxJSON[url]); console.log("url="+url) console.log("-----------------------------------------------") */ if(entireResponse==3){ callbackSuccess(info); } else if(entireResponse==2){ if(info.resultCode=='0000'){ callbackSuccess(info.responseObject); }else{ if(Object.prototype.toString.call(callbackFail) == "[object Function]"){ //失败回调 callbackFail(info); return; } if(info.resultCode=='10015' || (info.resultCode == '9999' && info.resultMsg == '未登录或者发呆过久,请重新登录!')){ //token无效 // 未登录或者已过期,请重新登录! // uni.showToast({ // title: '未登录或者已过期,请重新登录!', // icon: 'none', // duration: 2000 // }); let that = this; let showModalFlag = this.getSync('showModalFlag'); if(!showModalFlag){ this.setSync("showModalFlag", true); uni.showModal({ content: '未登录或者已过期,请先登录再进行操作!', cancelText: '暂不登录', confirmText: '立即登录', success: function (res) { that.setSync("showModalFlag", false); if (res.confirm) { const pages = getCurrentPages(); const getOptions = pages[pages.length - 1].options;//当前页 let redirectUrl = '../../' + pages[pages.length - 1].route; for(let i in getOptions){ redirectUrl += '&' + i + '=' + getOptions[i]; } if(pages[pages.length - 1].route !='pages/home/home'&&pages[pages.length - 1].route !='pages/study/study'&&pages[pages.length - 1].route !='pages/mine/mine'){ that.navigateTo('../user/index?redirectUrl=' + redirectUrl +'&tabFlag=0'); } else { that.navigateTo('../user/index?redirectUrl=' + redirectUrl +'&tabFlag=1'); } } else if (res.cancel) { console.log('用户点击取消'); } } }); } }else{ //其他错误信息 let ruleEvents = info.ruleEvents; let resultMsg = info.resultMsg; if(ruleEvents && ruleEvents instanceof Array && ruleEvents.length > 0){ let arr = []; ruleEvents.map((item,i)=>{ arr.push(item.ruleMsg) }); //console.log('ccccc'+ arr.join('\n')) uni.showModal({ content: arr.join(','), showCancel: false, confirmText: '我知道了', }); }else{ uni.showModal({ content: resultMsg, showCancel: false, confirmText: '我知道了', }); } } } } }, fail: (res)=> { delete ajaxJSON[url]; //console.log("data=%o", res); if(loadFlag && showLoading){ showLoading--; //最后一个请求关闭加载遮罩层 if(showLoading==0){ uni.hideLoading(); } } loadFlag = false; uni.showModal({ content: res, showCancel: false, confirmText: '我知道了', }); }, complete:()=>{ if(loadFlag){ //最后一个请求关闭加载遮罩层 if(showLoading==0){ uni.hideLoading(); } } //tipStatus = false; } }); }catch (err){ //console.log(" end ajax error"); uni.showModal({ content: "错误:"+JSON.stringify(arguments), showCancel: false, confirmText: '我知道了', }); } }; const postFileStream = function (self,url,data,callbackSuccess){ //console.log(" start ajax"); let callbackFail = ''; let loadFlag = true; let reqMoreFlag = 0; let entireResponse = 2; if(self.type.fun(arguments[4])){ callbackFail = arguments[4]; }else if(self.type.bool(arguments[4])){ loadFlag = arguments[4]; }else if(self.type.num(arguments[4])){ if(arguments[4]===0 || arguments[4]===1){ reqMoreFlag = arguments[4]; }else if(arguments[4]===2 || arguments[4]===3){ entireResponse = arguments[4]; } } if(self.type.fun(arguments[5])){ callbackFail = arguments[5]; }else if(self.type.bool(arguments[5])){ loadFlag = arguments[5]; }else if(self.type.num(arguments[5])){ // reqMoreFlag = arguments[5]; if(arguments[5]===0 || arguments[5]===1){ reqMoreFlag = arguments[5]; }else if(arguments[5]===2 || arguments[5]===3){ entireResponse = arguments[5]; } } if(self.type.fun(arguments[6])){ callbackFail = arguments[6]; }else if(self.type.bool(arguments[6])){ loadFlag = arguments[6]; }else if(self.type.num(arguments[6])){ // reqMoreFlag = arguments[6]; if(arguments[6]===0 || arguments[6]===1){ reqMoreFlag = arguments[6]; }else if(arguments[6]===2 || arguments[6]===3){ entireResponse = arguments[6]; } } if(self.type.fun(arguments[7])){ callbackFail = arguments[7]; }else if(self.type.bool(arguments[7])){ loadFlag = arguments[7]; }else if(self.type.num(arguments[7])){ // reqMoreFlag = arguments[7]; if(arguments[7]===0 || arguments[7]===1){ reqMoreFlag = arguments[7]; }else if(arguments[7]===2 || arguments[7]===3){ entireResponse = arguments[7]; } } if(reqMoreFlag == 0){ //该接口未返回请求结果时,只能请求一次 if(ajaxJSON[url] == 1){ //console.log('只请求一次') return; } } ajaxJSON[url] = 1; //500ms后才弹出遮罩层 setTimeout(()=>{ if(loadFlag){ uni.showLoading({ title: '加载中', mask: true }); } },500) //console.log(arguments[3]) try { uni.request({ //url: 'http://gateway-test.tianyuaninternet.com/apigateway'+url, url: baseUrl+url, method: 'POST', data: JSON.stringify({ "contractRoot": { "svcCont": { "requestObject": data }, "tcpCont": { "appKey": "string", "dstSysId": "string", "reqTime": 0, "rspTime": 0, "sign": "string", "svcCode": "string", "transactionId": "string", "version": "string" } } }), dataType: 'json', header:{ 'Accept': 'application/json', 'Content-Type': 'application/json', 'authorization': uni.getStorageSync(tokenVal), 'appId': this.appId, //'authorization': dd.getStorageSync({ key: 'token487192742724767843' }).data }, responseType: 'arraybuffer', //responseType: 'blob', success: (res)=> { //debugger delete ajaxJSON[url]; //console.log(" end ajax success"); if(loadFlag){ uni.hideLoading(); } loadFlag = false; //debugger let byteLength = res.data.byteLength; //长度1000是定义出来 大于1000估计都是正常的 if(byteLength < 1000){ try{ let str = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(res.data))) let info = str.contractRoot.svcCont; if(Object.prototype.toString.call(callbackFail) == "[object Function]"){ //失败回调 callbackFail(info); return; } if(info.resultCode=='10015'){ // 未登录或者已过期,请重新登录! uni.showToast({ title: '未登录或者已过期,请重新登录!', icon: 'none', duration: 2000 }); this.navigateTo('../user/login') //token无效 }else if(info.resultCode=='9998'){ //9998 比如调用user项目生成二维码失败,获取传的url无效等 uni.showModal({ content: '系统错误', showCancel: false, confirmText: '我知道了', }); }else{ //9999的业务异常,比如场景id无效之类的 uni.showModal({ content: '业务异常', showCancel: false, confirmText: '我知道了', }); } return; }catch(e){ } } // let blob = new Blob([res.data],{type:'image/jpeg'} ) // let reader = new window.FileReader(); // reader.readAsDataURL(blob); // reader.onloadend = function() { // let base64data = reader.result; // callbackSuccess(base64data ); // } let base64data = 'data:image/png;base64,'+ wx.arrayBufferToBase64(res.data); callbackSuccess(base64data ); return; }, fail: (res)=> { delete ajaxJSON[url]; //console.log("data=%o", res); if(loadFlag){ uni.hideLoading(); } loadFlag = false; uni.showModal({ content: res, showCancel: false, confirmText: '我知道了', }); }, complete:()=>{ if(loadFlag){ uni.hideLoading(); } //tipStatus = false; } }); }catch (err){ //console.log(" end ajax error"); uni.showModal({ content: "错误:"+JSON.stringify(arguments), showCancel: false, confirmText: '我知道了', }); } }; const apiObj = {}; for (const key in apiJson) { apiObj[key] = function(){ let args = []; //debugger for (let i in arguments) { if (i == 0) { args.push(Vue.prototype); args.push(apiJson[key].url); args.push(arguments[0]); }else{ args.push(arguments[i]); } } if(apiJson[key].type=='post'){ post.apply(Vue.prototype, args) } if(apiJson[key].type=='postFileStream'){ postFileStream.apply(Vue.prototype, args) } // console.log(function(apiJson[key].type)); } } export default apiObj;