gxd-vue-library
Version:
依赖与element Ui插件库,聚福宝福利PC端插件库
128 lines (114 loc) • 3.07 kB
JavaScript
'use strict';
const {
methodMap,
methodTitleMap,
methodPathMap,
resultKey,
formType
} = require('./../config/config');
/**
* @description 检查变量类型
* @param obj
* @returns {*}
*/
const checkVarType = (obj) => {
let toString = Object.prototype.toString;
let map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object'
};
return map[toString.call(obj)];
};
/**
* @description 检查对象或者数组是否为空
* @param obj
* @return boolean
*/
const isEmpty = (obj) => {
if (checkVarType(obj) === 'array' ||
checkVarType(obj) === 'object'
) {
let str = JSON.stringify(obj);
return str === '{}' || str === '[]';
} else {
console.log('isEmpty.error', obj);
throw new Error('只支持数组与JSON对象格式');
}
};
const getInKey = (method, mode, it, isUpper)=>{
let path = [method, mode.name];
if(it.suffix) path.push(it.suffix);
path = path.map((item,index)=>{
if(isUpper) return item.toLocaleUpperCase();
else {
if(index === 0) return item;
return `${item.slice(0, 1).toLocaleUpperCase() + item.slice(1)}`;
}
});
if(isUpper) return path.join('_');
else return path.join('');
}
/**
* @description 通过方法获取
* @param method
* @param mode 大模块对象
* @param it 小模块对象
* @returns {{method: *, paramsKey: (string)}}
*/
const getMapMethod = (method, mode, it) => {
let paramsKey = methodMap[method] === 'GET' ? 'params' : 'data';
let params = {
method: methodMap[method].toLocaleLowerCase(),
paramsKey: paramsKey,
title: methodTitleMap[method].replace('@title@', mode['title']),
key: getInKey(method, mode, it, true),
fnName: getInKey(method, mode, it, false),
isParams: it.query[method] !== undefined,
disabled: it.disabled,
role: it['roles'][method] || '',
resultKey: it.resultKey || resultKey,
isConsole: it.isConsole || false,
methodKey: method,
formType: formType[method],
};
//state
if(it.state) {
if(it.state[method]){
params['state'] = getInKey(method, mode, it, false) + 'State';
}
}
//设置参数
if(it.query[method] !== undefined) {
params[paramsKey] = it.query[method];
let defaultParams = {};
Object.keys(it.query[method]).map(key => {
if (it.query[method][key].length === 4) {
defaultParams[key] = it.query[method][key][3];
}
});
if (isEmpty(defaultParams)) params['defaultParams'] = null
else params['defaultParams'] = JSON.stringify(defaultParams);
}
return params;
};
/**
* @description 获取请求地址
* @param method
* @param it 小模块对象
* @returns {string}
*/
const getMapUrl = (method , it)=>{
return `${it.path}${methodPathMap[method]}`
};
module.exports = {
getMapMethod,
getMapUrl
}