123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- import {mgop} from "@aligov/jssdk-mgop";
- import zlbConfig from "@/common/js/zlbConfig";
- import {httpApi} from "@/common/js/baseUrl";
- import {handleHttpError, handleNormalResponse} from "@/utils/fetch/responseHandler";
- import handleReqLoading from "@/utils/fetch/handleReqLoading";
- import {handleRequest} from "@/utils/fetch/requestHandler";
- function download(url, type = "get", data = {}) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: url,
- method: type,
- responseType: "arraybuffer",
- header: {
- "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
- src: "wechat",
- },
- data: data,
- timeout: 10000,
- success: function (res) {
- console.log("返回结果:", res);
- if (res.statusCode === 200) {
- resolve(res);
- } else {
- reject();
- uni.showToast({title: "服务异常", icon: "none"});
- }
- },
- fail: function (err) {
- uni.showToast({title: "服务异常", icon: "none"});
- reject(err);
- },
- });
- });
- }
- function commonUpload(url, data, config = {}) {
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: httpApi + url,
- ...data,
- ...config,
- header: {
- "content-type": "multipart/form-data",
- Token: '1'
- },
-
- success: function (res) {
- },
- fail: function (res) {
- uni.showToast({
- title: "服务异常",
- icon: "none",
- duration: 2000,
- });
- reject();
- },
- });
- });
- }
- function index(url, type = "get", data = {}, dataType) {
- return new Promise((resolve, reject) => {
- (url && url.startsWith("mgop.alibaba")) ? handleMgop(url, type, data, dataType, resolve, reject) : handleFetch(url, type, data, dataType, resolve, reject);
- });
- }
- function handleMgop(url, type, data, dataType, resolve, reject) {
- let extraHeader = {};
-
- extraHeader.isTestUrl = "1";
- mgop({
- api: url,
-
- dataType: "JSON",
- type: type,
- data: data,
- appKey: zlbConfig.appKey,
- appId:zlbConfig.appId,
- header: {
- "Content-Type":
- dataType === "form"
- ? "application/x-www-form-urlencoded"
- : "application/json",
- src: "wechat",
- ...extraHeader,
- },
- onSuccess: (res) => {
- const code = res.ret[0].split("::")[0];
- if (code === "1000") {
- if (
- res.data.code === 0 ||
- res.data.code === "0" ||
- res.data.status === 0 ||
- res.data.status === "1"
- ) {
- resolve(res.data);
- } else {
-
- if (res.api === "mgop.alibaba.digitalDoorplate.getHomePageOneDoorplateFullInfo") {
- resolve(res.data);
- } else {
- reject(res);
- if (res.data.message || res.data.msg) {
- uni.showToast({
- title: res.data.message || res.data.msg,
- icon: "none",
- duration: 2000,
- });
- }
- }
- }
- } else {
- reject(res);
- uni.showToast({
- title: "服务异常",
- icon: "none",
- duration: 2000,
- });
- }
- },
- onFail: (err) => {
- reject(err);
- },
- });
- }
- function handleFetch(url, type, data, dataType, resolve, reject) {
- let header = {
- "Content-Type":
- dataType === "form"
- ? "application/x-www-form-urlencoded"
- : "application/json",
- Token: '1'
- };
- uni.request({
- url: httpApi + url,
- method: type,
- header,
- data: data,
- timeout: 60000,
- success: function (res) {
- if (res.statusCode === 200) {
- if (
- res.data.code === 0 ||
- res.data.code === "0" ||
- res.data.status === 0 ||
- res.data.status === "1"
- ) {
- resolve(res.data);
- } else {
- reject();
- if (res.data.message || res.data.msg) {
- uni.showToast({
- title: res.data.message || res.data.msg,
- icon: "none",
- duration: 2000,
- });
- }
- }
- } else {
- reject();
- uni.showToast({
- title: "服务异常",
- icon: "none",
- duration: 2000,
- });
- }
- },
- fail: function (err) {
- uni.showToast({
- title: "服务异常",
- icon: "none",
- duration: 2000,
- });
- reject(err);
- },
- complete: function () {
- },
- });
- }
- export const myFetch = async (url, type = "get", data = {}, dataType) => {
- const reqConfig = {
- url: httpApi + url,
- method: type,
- data: data,
- dataType: dataType,
- timeout: 60000,
- header: {
- 'content-type': 'application/json',
- Token: '1',
- appId:'2002407848',
- },
- }
- try {
- handleReqLoading(true);
- const response = await handleRequest(reqConfig);
- const responseData = handleNormalResponse(response);
- handleReqLoading(false);
-
- return responseData;
- }catch (err) {
- handleReqLoading(false);
- return handleHttpError(err);
- }
- }
|