binaryEncode.js 5.1 KB
Newer Older
1 2 3
import encryptApiList, {
	resBinaryApiList,
} from '@/webPublic/one_stop_public/Base16/encryptApiList';
4 5 6
import { getToken, getUserInfo } from '@/webPublic/one_stop_public/utils/token';
import { queryApiActionPath } from '@/webPublic/one_stop_public/utils/queryConfig';
import { qqCw } from '@/webPublic/one_stop_public/utils/request';
7
import { getHeaders, isJSON } from '@/webPublic/zyd_public/utils/utils';
8
import { deepCopy } from '@/webPublic/one_stop_public/utils/myutils';
钟是志's avatar
钟是志 committed
9
import { countAllValues } from '@/webPublic/one_stop_public/Base16/SplitBase16Index';
钟是志's avatar
钟是志 committed
10
import { getUrlInfo } from '@/webPublic/one_stop_public/DetailForAudit/utils';
钟是志's avatar
钟是志 committed
11 12
import { getTransformApi } from '@/webPublic/one_stop_public/2022beidianke/localstorageTransform';
import { strToBinary } from './strToBinary'; // 字符串转二进制 混淆代码
13 14
import { binaryToStr } from './binaryToStr';
import { getVisitorToken } from '@/webPublic/zyd_public/utils/getHeaders'; // 二进制字符串转JSON字符串 混淆代码
钟是志's avatar
钟是志 committed
15

16
const test = !!getUrlInfo().test;
17 18

function formatDatas(datas, url = '') {
19 20 21 22
	if (url && url.indexOf('getSqlData') > -1 && window.smartFormGlobalProps?.data) {
	  // 按欢哥的要求 getSqlData 接口要加上appId 参数
    // 解决北电科接口越权的问题.
		datas.appId = window.smartFormGlobalProps?.data.appId || window.smartFormGlobalProps?.data.id;
23
		// console.log('getSqlData,AppId', datas.appId);
24 25 26 27
	}
	let datasCode = JSON.stringify(datas);
	const openIsBinary = window.CONFIG?.IS_BINARY;

28 29 30
	// if (test) {
	// 	console.log(url, datas);
	// }
31

32 33 34 35 36 37
	if (openIsBinary) {
		datasCode = strToBinary(datasCode);
	}
	let file = new File([datasCode], 'fileParams.jpeg', {
		type: 'image/jpeg',
	});
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	let datasTrue = {
		fileParams: file,
	};
	if (openIsBinary) {
		datasTrue.bin = true;
		if (window.CONFIG?.ALL_R_BIN) {
			datasTrue.rbin = true;
		} else {
			let findIndexA = resBinaryApiList.findIndex((g) => {
				return url.includes(g.api);
			});
			if (findIndexA >= 0) {
				datasTrue.rbin = true;
			}
		}
	}
	return datasTrue;
56 57
}

钟是志's avatar
钟是志 committed
58
export async function giveFilePostData(datas, url) {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	for (let i in datas) {
		if (
			datas[i] === null ||
			(Array.isArray(datas[i]) && datas[i].length === 0) ||
			datas[i] === undefined
		) {
			delete datas[i];
		}
	}
	if (url && typeof url === 'string') {
		for (let item of encryptApiList) {
			if ((window.CONFIG?.ALLBIN && url.indexOf('/onestop/') > -1) || url.indexOf(item.api) > -1) {
				const roleGroup = getUserInfo().groupsId;
				if (url.indexOf('http') > -1) {
					url = url.replace('/onestop/', `/onestop/safe/${roleGroup}/`);
				} else {
					url = url.replace(url, `/safe/${roleGroup}/` + url);
					url = url.replaceAll('//', '/');
				}
				datas = await countAllValues(datas, item);
				return new Promise((resolve, reject) => {
					resolve({
						datas: formatDatas(datas, url),
						url,
					});
				});
			}
		}
	}
	return new Promise((resolve, reject) => {
		resolve(datas);
		return datas;
	});
92 93
}

94
function testPromise(d) {
95 96 97
	return new Promise((resolve, reject) => {
		resolve(d);
	});
98 99 100
}

export async function requestFileInfo(url, datas) {
101 102 103 104 105 106 107 108 109 110 111 112 113
	// for (let i = 0; i < 100; i++) {
	//   datas = await testPromise(datas);
	// }
	const formData = new FormData();
	if (getVisitorToken()) {
		datas.token = getVisitorToken();
	}
	for (let key in datas) {
		formData.append(key, datas[key]);
	}
	if (!url || typeof url !== 'string') {
		return false;
	}
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	let trueUrl = url && url.indexOf('http') > -1 ? url : queryApiActionPath() + url;
	const { transformApi, headersApi } = await getTransformApi(trueUrl);
	return fetch(transformApi, {
		headers: {
			Accept: 'application/json',
			...getHeaders(transformApi).headers,
			// apis: headersApi,
			// axxx: `select * from user123 where id = 1`,
		},
		method: 'POST',
		credentials: 'omit',
		mode: 'cors',
		body: formData,
	})
		.then((res) => {
			if (res.status === 401) {
				qqCw({
					title: '登录过期401',
					customErrMsg: '登录已过期,请重新登录',
				});
				return false;
			}
			if (res.status === 404) {
				qqCw({
					title: 'http404报错',
					customErrMsg: '请联系系统管理员',
				});
				return false;
			}
			return res.json();
		})
		.then((res) => {
			if (res && typeof res === 'object' && res.rbin) {
				res = binaryToStr(res.rbin);
				if (isJSON(res)) {
					res = JSON.parse(res);
				}
			}
			if (res && res.errCode) {
				qqCw({
					msg: res.errMsg || res.message,
					customErrMsg: res.customErrMsg,
				});
				return false;
			}
160

161 162 163 164 165 166
			// if (test) {
			// 	console.log({
			// 		url,
			// 		res,
			// 	});
			// }
167

168 169
			return res;
		});
170 171 172
}

export function giveFilePostDataInfoForTrue(datas, url) {
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	if (!window.CONFIG?.IS_SAFE) {
		return {
			datas,
			url,
		};
	}
	datas = deepCopy(datas);
	for (let i in datas) {
		if (
			datas[i] === null ||
			(Array.isArray(datas[i]) && datas[i].length === 0) ||
			datas[i] === undefined
		) {
			delete datas[i];
		}
	}
	if (url) {
		const roleGroup = getUserInfo().groupsId;
		if (url.indexOf('http') > -1) {
			url = url.replace('/onestop/', `/onestop/safe/${roleGroup}/`);
		} else {
			url = url.replace(url, `/safe/${roleGroup}/` + url);
			url = url.replaceAll('//', '/');
		}
		return {
			datas: formatDatas(datas, url),
			url,
		};
	}
202
}