@cainiaofe/cn-ui-m
Version:
69 lines (68 loc) • 3.86 kB
JavaScript
import { __assign, __awaiter, __generator } from "tslib";
import isFunction from 'lodash/isFunction';
import { request } from 'cn-request';
export function handleRequestService(requestConfig, remoteUrl) {
var _this = this;
var _a = requestConfig || {}, service = _a.service, _b = _a.searchKey, searchKey = _b === void 0 ? 'key' : _b, paramsProps = _a.params, dataProps = _a.data, _c = _a.searchFormat, searchFormatProps = _c === void 0 ? function (params) { return params; } : _c, formatParam = _a.formatParam, _d = _a.formatResult, formatResult = _d === void 0 ? function (res) {
var _a, _b, _c, _d;
if (Array.isArray(res)) {
return res;
}
else if (Array.isArray(res === null || res === void 0 ? void 0 : res.data)) {
return res.data;
}
else if (Array.isArray((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.dataSource)) {
return res.data.dataSource;
}
else if (Array.isArray((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.data)) {
return res.data.data;
}
else if (Array.isArray((_d = (_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.dataSource)) {
return res.data.data.dataSource;
}
return [];
} : _d, _e = _a.method, method = _e === void 0 ? 'GET' : _e;
// 配置了service时,promise由service托管
if (isFunction(service)) {
return function (searchConfig) { return __awaiter(_this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
return [2 /*return*/, (_a = service(searchConfig)) === null || _a === void 0 ? void 0 : _a.then(function (data) {
var dataSource = formatResult(data);
return Array.isArray(dataSource) ? dataSource : [];
})];
});
}); };
}
// 配置了remoteUrl,promise由cn-request执行
if (remoteUrl) {
return function (searchConfig) { return __awaiter(_this, void 0, void 0, function () {
var searchFormat, params, data, resultData, dataSource;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
searchFormat = formatParam !== null && formatParam !== void 0 ? formatParam : searchFormatProps;
params = (method === null || method === void 0 ? void 0 : method.toLocaleUpperCase()) === 'GET'
? searchFormat(__assign((_a = {}, _a[searchKey] = searchConfig, _a), paramsProps))
: paramsProps;
data = (method === null || method === void 0 ? void 0 : method.toLocaleUpperCase()) === 'POST'
? searchFormat(__assign((_b = {}, _b[searchKey] = searchConfig, _b), dataProps))
: dataProps;
return [4 /*yield*/, request(__assign(__assign({}, requestConfig), { params: params, data: data, url: remoteUrl }))];
case 1:
resultData = _c.sent();
dataSource = formatResult(resultData);
return [2 /*return*/, (Array.isArray(dataSource) ? dataSource : [])];
}
});
}); };
}
// 什么都没配置,返回一个空promise
// eslint-disable-next-line
return function (searchConfig) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, []];
});
}); };
}