trs-ui-app
Version:
TRS 可视化低代码平台 前端移动 UI 组件库 👍
206 lines (203 loc) • 7.64 kB
JavaScript
import { isRef } from 'vue';
import api from './request.js';
import { getResponseData } from './mapData.js';
function getDeepProperty(obj, path) {
const arr = path.split('.');
let curObj = isRef(obj) ? obj.value : obj;
const prop = arr.pop();
while (arr.length) {
const curPath = arr.shift();
curObj = isRef(curObj[curPath]) ? curObj[curPath].value : curObj[curPath];
}
if (!curObj) {
return '';
}
return curObj[prop];
}
function getParamFromURL($route, path) {
return $route.params[path] || $route.query[path];
}
function getParamFromStore(dymicStores, path) {
const arr = path.split('.');
const store = dymicStores[`${arr[0]}`];
return getDeepProperty(store, arr.slice(1).join('.'));
}
function getParamFromComponent(allRefs, path) {
const arr = path.split('.');
const com = allRefs.value.filter(item => item.key === arr[0]);
if (com.length) {
return getDeepProperty(com[0], arr.slice(1).join('.'));
}
return '';
}
function getParamFromSelf(selfStates, path) {
return getDeepProperty(selfStates, path);
}
function getParamFromGlobal(globalData, path) {
return getDeepProperty(globalData, path);
}
function repaceUrlParam(exp, $route, dymicStores, allRefs, selfStates, globalData) {
let result;
const path = exp.replace('{', '').replace('}', '');
const arr = path.split('.');
const type = arr.shift();
const newPath = arr.join('.');
switch (type) {
case 'route':
result = getParamFromURL($route, newPath);
break;
case 'global':
result = getParamFromGlobal(globalData, newPath);
break;
case 'store':
result = getParamFromStore(dymicStores, newPath);
break;
case 'component':
result = getParamFromComponent(allRefs, newPath);
break;
case 'self':
result = getParamFromSelf(selfStates, newPath);
break;
default:
result = type;
}
return result;
}
function handlerUrlPathParam(url, $route, dymicStores, allRefs, selfStates, globalData) {
const myUrl = url.replace(/{.*}/gi, (match) => {
const p = repaceUrlParam(match, $route, dymicStores, allRefs, selfStates, globalData);
return p;
});
return myUrl;
}
function handlerRequestParams(paramsMap, $route, dymicStores, allRefs, selfStates, globalData, otherParams) {
const params = {};
Object.keys(paramsMap).forEach(pak => {
const element = String(paramsMap[pak]);
const path = element.replace('{', '').replace('}', '');
const arr = path.split('.');
const type = arr.shift();
const newPath = arr.join('.');
switch (type) {
case 'route':
params[pak] = getParamFromURL($route, newPath);
break;
case 'store':
params[pak] = getParamFromStore(dymicStores, newPath);
break;
case 'component':
params[pak] = getParamFromComponent(allRefs, newPath);
break;
case 'self':
params[pak] = getParamFromSelf(selfStates, newPath);
break;
case 'global':
params[pak] = getParamFromGlobal(globalData, newPath);
break;
default:
params[pak] = type;
}
});
return Object.assign(params, otherParams);
}
function handlerRequestHeaders(headersMap, $route, dymicStores, allRefs, selfStates, globalData) {
const headers = {};
Object.keys(headersMap).forEach(pak => {
const element = headersMap[pak];
const path = element.replace('{', '').replace('}', '');
const arr = path.split('.');
const type = arr.shift();
const newPath = arr.join('.');
switch (type) {
case 'route':
headers[pak] = getParamFromURL($route, newPath);
break;
case 'store':
headers[pak] = getParamFromStore(dymicStores, newPath);
break;
case 'component':
headers[pak] = getParamFromComponent(allRefs, newPath);
break;
case 'self':
headers[pak] = getParamFromSelf(selfStates, newPath);
break;
case 'global':
headers[pak] = getParamFromGlobal(globalData, newPath);
break;
default:
headers[pak] = type;
}
});
return headers;
}
async function excuteRequest(sourceOptions, dymicStores, allRefs, selfStates, globalData, $route, otherParams, isResutlSingleObj, eventHandlers, logFunction) {
let data;
let url = sourceOptions.url;
const otherParamsCopy = JSON.parse(JSON.stringify(otherParams));
if (/{.*}/gi.test(sourceOptions.url)) {
url = handlerUrlPathParam(url, $route, dymicStores, allRefs, selfStates, globalData);
}
let params = handlerRequestParams(sourceOptions.params, $route, dymicStores, allRefs, selfStates, globalData, otherParamsCopy);
let headers;
if (sourceOptions.headers) {
headers = handlerRequestHeaders(sourceOptions.headers, $route, dymicStores, allRefs, selfStates, globalData);
}
if (sourceOptions.before && eventHandlers[sourceOptions.before]) {
const obj = await eventHandlers[sourceOptions.before](params, url, headers);
params = obj.params;
url = obj.url;
headers = obj.headers;
}
const logFun = (name, msg) => {
if (logFunction) {
logFunction(name, msg);
}
};
try {
if (!url || url === 'undefined') {
return [];
}
logFun(url, params);
const rs = await (sourceOptions.method === 'GET' ? api(url, params, 'GET', headers, logFun) : api(url, params, 'post', headers, logFun));
logFun(url, rs);
const map = sourceOptions.responseMap;
if (parseInt(rs.code, 10) === 200 && rs.data.length && !rs.data[0].error) {
if (Array.isArray(rs.data)) {
data = getResponseData(map, rs.data);
}
if (isResutlSingleObj) {
return data[0];
}
if (otherParams.pageParams || rs.pageInfo) {
return {
data,
pager: rs.pageInfo,
};
}
return data;
}
else {
if (otherParams.pageParams || rs.pageInfo) {
return {
data: [],
pager: rs.pageInfo,
};
}
return [];
}
}
catch (error) {
logFun('requestError', error);
throw new Error(error);
}
}
async function useAPIData(sourceOptions, dymicStores, allRefs, selfStates, globalData, $route, otherParams = {}, isResutlSingleObj = false, eventHandlers = {}, logFunction = () => { }) {
if (logFunction) {
logFunction('sourceOptions', sourceOptions);
logFunction('dymicStores', dymicStores);
logFunction('allRefs', allRefs);
logFunction('selfStates', selfStates);
}
return excuteRequest(sourceOptions, dymicStores, allRefs, selfStates, globalData, $route, otherParams, isResutlSingleObj, eventHandlers, logFunction);
}
export { useAPIData as default };