ming-demo3
Version:
mdf metaui web
323 lines (273 loc) • 8.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.combine = combine;
exports.getMultiplication = exports.getRoundValue = exports.getFormatValue = exports.getPredicateValue = exports.proxy = exports.rebuildTreeData = exports.uniformProxy = exports.auth = exports.genAction = exports.doFetch = exports.catchException = exports.toJSON = exports.genFetchOptions = void 0;
function combine(baseurl, pathname) {
var separator = /\/$/.test(baseurl) === false && /^\//.test(pathname) === false ? '/' : '';
return Array.prototype.slice.call(arguments, 0).join(separator);
}
var genFetchOptions = function genFetchOptions(method, headers, paramsObj) {
var _headers = headers,
_paramsObj = paramsObj;
if (paramsObj === undefined) {
_paramsObj = headers;
_headers = {
'content-type': 'application/json'
};
}
return {
method: method,
headers: _headers,
body: JSON.stringify(_paramsObj),
credentials: 'include'
};
};
exports.genFetchOptions = genFetchOptions;
var toJSON = function toJSON(response) {
if (response.status !== 200) {
return response.text().then(function (text) {
try {
return JSON.parse(text);
} catch (e) {
var items = [];
items.push(response.statusText);
items.push(text);
items.push('(' + response.url + ')');
return {
code: process.env.__CLIENT__ ? response.status : 500,
message: items.join('\r\n')
};
}
});
}
return response.text().then(function (text) {
try {
if (text.charCodeAt(0) === 0xFEFF) text = text.slice(1);
return JSON.parse(text);
} catch (e) {
var items = [];
items.push(e.message);
items.push(text);
items.push('(' + response.url + ')');
return {
code: 500,
message: "\u63A5\u53E3\u8FD4\u56DE\u683C\u5F0F\u4E0D\u662Fapplication/json\u683C\u5F0F\uFF1A".concat(items.join('\r\n'))
};
}
});
};
exports.toJSON = toJSON;
var catchException = function catchException(error) {
var errInfo = error.toString();
console.log(errInfo);
return {
code: 500,
message: process.env.NODE_ENV === 'development' ? errInfo : '网络异常'
};
};
exports.catchException = catchException;
var doFetch = function doFetch(url) {
var options = genFetchOptions('get');
return fetch(url, options).then(toJSON, catchException).then(function (json) {
return {
code: 200,
data: json
};
});
};
exports.doFetch = doFetch;
var genAction = function genAction(type, payload) {
return {
type: type,
payload: payload
};
};
exports.genAction = genAction;
var auth = function auth(json, dispatch) {
if (json.code === 900) {
cb.route.redirectLoginPage();
return;
}
return json;
};
exports.auth = auth;
var uniformProxy = function uniformProxy(config) {
var method = config.method && config.method.trim().toLocaleUpperCase() || 'GET';
var headers = config.headers || {
'Content-Type': 'application/json;charset=utf-8'
};
var credentials = 'include';
var url = config.url;
var args = [];
if (method === 'GET' || method === 'DELETE') {
args.push(url);
args.push({
method: method,
headers: headers,
credentials: credentials
});
} else if (method === 'POST' || method === 'PUT') {
var body = JSON.stringify(config.params);
args.push(url);
args.push({
method: method,
headers: headers,
body: body,
credentials: credentials
});
}
return fetch.apply(null, args).then(toJSON, catchException);
};
exports.uniformProxy = uniformProxy;
var rebuildTreeData = function rebuildTreeData(treeData, orgMenus, storeMenus) {
treeData.forEach(function (item) {
if (item.children) {
if (item.code === 'SJ99') {
item.cols = 2;
item.children = item.children.slice(0, 20);
}
rebuildTreeData(item.children, orgMenus, storeMenus);
} else {
item.disabled = !item.viewType;
item.isShopRelated ? storeMenus.push(item) : orgMenus.push(item);
}
});
};
exports.rebuildTreeData = rebuildTreeData;
var xhr = function xhr(config) {
var url = config.url,
method = config.method,
options = config.options,
params = config.params;
var xhrProxy = cb.rest.DynamicProxy.create({
fetch: {
url: url,
method: method,
options: options
}
});
return new Promise(function (resolve) {
xhrProxy.fetch(params, function (err, result) {
resolve(err ? err : {
code: 200,
data: result
});
});
});
};
var proxy = function proxy(config) {
if (cb.rest.mode === 'xhr' || config.options && config.options.jsonp) return xhr(config);
var mode = config.crossDomain ? 'cors' : null;
var method = config.method && config.method.trim().toLocaleUpperCase() || 'GET';
var headers = config.headers || config.crossDomain ? {} : {
'Content-Type': 'application/json;charset=utf-8'
};
var credentials = config.crossDomain ? null : 'include';
var url = cb.rest._getUrl(config.url, config.options);
var args = [];
if (method === 'GET' || method === 'DELETE') {
url = cb.rest._appendUrl(url, config.params);
args.push(url);
args.push({
method: method,
mode: mode,
headers: headers,
credentials: credentials
});
} else if (method === 'POST' || method === 'PUT') {
var body = JSON.stringify(config.params);
args.push(url);
args.push({
method: method,
mode: mode,
headers: headers,
body: body,
credentials: credentials
});
}
cb.utils.loading(true);
return fetch.apply(null, args).then(toJSON, catchException).then(function (json) {
cb.utils.loading(false);
if (json.code === 900) {
cb.route.redirectLoginPage();
return;
}
return json;
});
};
exports.proxy = proxy;
var getPredicateValue = function getPredicateValue(formatValue) {
var exp = /<%.*?%>/g;
var items = formatValue.match(exp);
if (!items) return formatValue;
var values = {};
var valid = true;
items.forEach(function (item) {
var predicate = item.substring(2, item.length - 2).split('.');
if (predicate.length !== 2) {
valid = false;
return;
}
var obj = cb.rest.AppContext[predicate[0]];
values[item] = obj && (cb.utils.isEmpty(obj[predicate[1]]) ? '' : obj[predicate[1]]);
});
if (!valid) return formatValue;
return formatValue.replace(exp, function (key) {
return values[key];
});
};
exports.getPredicateValue = getPredicateValue;
var getFormatValue = function getFormatValue(value, format) {
var showValue = value,
prefix = null,
suffix = null;
if (!cb.utils.isEmpty(value) && format) {
if (format.before) {
prefix = getPredicateValue(format.before);
showValue = prefix + showValue;
}
if (format.after) {
suffix = getPredicateValue(format.after);
showValue += suffix;
}
}
return {
showValue: showValue,
prefix: prefix,
suffix: suffix
};
};
exports.getFormatValue = getFormatValue;
var getRoundValue = function getRoundValue(value, decimal) {
if (!decimal) return value;
var pow = Math.pow(10, decimal);
var returnValue = Math.round(Math.abs(value) * pow) / pow;
if (value < 0) returnValue = -returnValue;
return returnValue.toFixed(decimal);
};
exports.getRoundValue = getRoundValue;
var getMultiplication = function getMultiplication(first, second, type) {
first = parseFloat(first);
second = parseFloat(second);
var firstArr = first.toString().split('.');
var secondArr = second.toString().split('.');
var first_decimal = firstArr.length > 1 ? firstArr[1].length : 0;
var second_decimal = secondArr.length > 1 ? secondArr[1].length : 0;
var total_decimal = 0;
if (type === 'Multiplication') {
total_decimal = first_decimal + second_decimal;
return parseFloat((first * second).toFixed(total_decimal));
}
if (type === 'add') {
total_decimal = first_decimal >= second_decimal ? first_decimal : second_decimal;
return parseFloat((first + second).toFixed(total_decimal));
}
if (type === 'subduction') {
total_decimal = first_decimal >= second_decimal ? first_decimal : second_decimal;
return parseFloat((first - second).toFixed(total_decimal));
}
};
exports.getMultiplication = getMultiplication;
//# sourceMappingURL=util.js.map
;