commit:添加单体工程开源版本

This commit is contained in:
Jerry
2020-09-25 10:45:02 +08:00
parent 28ea2247f6
commit 53270e70f8
354 changed files with 40271 additions and 9 deletions

View File

@@ -0,0 +1,56 @@
const defaultLineChartOption = {
grid: {
left: '3%',
right: '4%',
bottom: '20px',
containLabel: true
},
xAxis: {
axisLabel: {
interval: 0,
showMaxLabel: true
}
},
legend: {
top: '3%'
}
}
const defaultBarChartOption = {
grid: {
left: '3%',
right: '4%',
bottom: '20px',
containLabel: true
},
xAxis: {
axisLabel: {
interval: 0,
showMaxLabel: true
}
},
legend: {
top: '3%'
}
}
const defaultPieChartOption = {
grid: {
left: '3%',
right: '4%',
bottom: '20px',
containLabel: true
},
legend: {
top: '3%'
},
series: {
center: ['50%', '60%']
}
}
export {
defaultLineChartOption,
defaultBarChartOption,
defaultPieChartOption
}

View File

@@ -0,0 +1,222 @@
import JSEncrypt from 'jsencrypt';
/**
* 列表数据转换树形数据
* @param {Array} data 要转换的列表
* @param {String} id 主键字段字段名
* @param {String} pid 父字段字段名
* @returns {Array} 转换后的树数据
*/
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
var res = []
var temp = {}
for (var i = 0; i < data.length; i++) {
temp[data[i][id]] = data[i]
}
for (var k = 0; k < data.length; k++) {
if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
if (!temp[data[k][pid]]['children']) {
temp[data[k][pid]]['children'] = []
}
if (!temp[data[k][pid]]['_level']) {
temp[data[k][pid]]['_level'] = 1
}
data[k]['_level'] = temp[data[k][pid]]._level + 1
data[k]['_parent'] = data[k][pid]
temp[data[k][pid]]['children'].push(data[k])
} else {
res.push(data[k])
}
}
return res
}
/**
* 获取字符串字节长度中文算2个字符
* @param {String} str 要获取长度的字符串
*/
export function getStringLength (str) {
return str.replace(/[\u4e00-\u9fa5\uff00-\uffff]/g, '**').length
}
/**
* 获取uuid
*/
export function getUUID () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
});
}
/**
* 大小驼峰变换函数
* @param name 要转换的字符串
* @param type 转换的类型0转换成小驼峰1转换成大驼峰
*/
export function nameTranslate (name, type) {
name = name.toLowerCase();
let nameArray = name.split('_');
nameArray.forEach((item, index) => {
if (index === 0) {
name = type === 1 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item;
} else {
name = name + item.slice(0, 1).toUpperCase() + item.slice(1);
}
});
nameArray = name.split('-');
nameArray.forEach((item, index) => {
if (index === 0) {
name = type === 1 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item;
} else {
name = name + item.slice(0, 1).toUpperCase() + item.slice(1);
}
});
return name;
}
/**
* 通过id从树中获取指定的节点
* @param {Object} node 根节点
* @param {String|Nubmer} id 键值
* @param {Array} list 保存查询路径
* @param {String} idKey 主键字段名
* @param {String} childKey 子节点字段名
*/
function findNode (node, id, list, idKey = 'id', childKey = 'children') {
if (Array.isArray(list)) list.push(node[idKey]);
if (node[idKey] === id) {
return node;
}
if (node[childKey] != null && Array.isArray(node[childKey])) {
for (let i = 0; i < node[childKey].length; i++) {
let tempNode = findNode(node[childKey][i], id, list, idKey, childKey);
if (tempNode) return tempNode;
}
}
if (Array.isArray(list)) list.pop();
}
/**
* 通过id返回从根节点到指定节点的路径
* @param {Array} treeRoot 树根节点数组
* @param {*} id 要查询的节点的id
* @param {*} idKey 主键字段名
* @param {*} childKey 子节点字段名
*/
export function findTreeNodePath (treeRoot, id, idKey = 'id', childKey = 'children') {
let tempList = [];
for (let i = 0; i < treeRoot.length; i++) {
if (findNode(treeRoot[i], id, tempList, idKey, childKey)) {
return tempList;
}
}
return [];
}
/**
* 通过id从树中查找节点
* @param {Array} treeRoot 根节点数组
* @param {*} id 要查找的节点的id
* @param {*} idKey 主键字段名
* @param {*} childKey 子节点字段名
*/
export function findTreeNode (treeRoot, id, idKey = 'id', childKey = 'children') {
for (let i = 0; i < treeRoot.length; i++) {
let tempNode = findNode(treeRoot[i], id, undefined, idKey, childKey);
if (tempNode) return tempNode;
}
}
/**
* 把Object转换成query字符串
* @param {Object} params 要转换的Object
*/
export function objectToQueryString (params) {
if (params == null) {
return null;
} else {
return Object.keys(params).map((key) => {
return `${key}=${params[key]}`;
}).join('&');
}
}
/**
* 从数组中查找某一项
* @param {Array} list 要查找的数组
* @param {String} id 要查找的节点id
* @param {String} idKey 主键字段名如果为null则直接比较
* @param {Boolean} removeItem 是否从数组中移除查找到的节点
* @returns {Object} 找到返回节点没找到返回undefined
*/
export function findItemFromList (list, id, idKey, removeItem = false) {
if (Array.isArray(list) && list.length > 0 && id != null) {
for (let i = 0; i < list.length; i++) {
let item = list[i];
if (((idKey == null || idKey === '') && item === id) || (idKey != null && item[idKey] === id)) {
if (removeItem) list.splice(i, 1);
return item;
}
}
}
return null;
}
/**
* 将数据保存到sessionStorage
* @param {*} key sessionStorage的键值
* @param {*} value 要保存的数据
*/
export function setObjectToSessionStorage (key, value) {
if (key == null || key === '') return false;
if (value == null) {
window.sessionStorage.removeItem(key);
return true;
} else {
let jsonObj = {
data: value
}
window.sessionStorage.setItem(key, JSON.stringify(jsonObj));
return true;
}
}
/**
* 从sessionStorage里面获取数据
* @param {String} key 键值
* @param {*} defaultValue 默认值
*/
export function getObjectFromSessionStorage (key, defaultValue) {
let jsonObj = null;
try {
jsonObj = JSON.parse(window.sessionStorage.getItem(key));
jsonObj = (jsonObj || {}).data;
} catch (e) {
jsonObj = defaultValue;
};
return (jsonObj != null) ? jsonObj : defaultValue;
}
/**
* 判读字符串是否一个数字
* @param {String} str 要判断的字符串
*/
export function isNumber (str) {
let num = Number.parseFloat(str);
if (Number.isNaN(num)) return false;
return num.toString() === str;
}
/**
* 生成随机数
* @param {Integer} min 随机数最小值
* @param {Integer} max 随机数最大值
*/
export function random (min, max) {
let base = Math.random();
return min + base * (max - min);
}
/**
* 加密
* @param {*} value 要加密的字符串
*/
const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpC4QMnbTrQOFriJJCCFFWhlruBJThAEBfRk7pRx1jsAhyNVL3CqJb0tRvpnbCnJhrRAEPdgFHXv5A0RrvFp+5Cw7QoFH6O9rKB8+0H7+aVQeKITMUHf/XMXioymw6Iq4QfWd8RhdtM1KM6eGTy8aU7SO2s69Mc1LXefg/x3yw6wIDAQAB';
export function encrypt (value) {
if (value == null || value === '') return null;
let encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
return encodeURIComponent(encrypt.encrypt(value));
}

View File

@@ -0,0 +1,33 @@
const pattern = {
mobie: /^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/,
english: /^[a-zA-Z]+$/,
englishAndNumber: /^[a-zA-Z0-9]+$/
}
/**
* 邮箱
* @param str
*/
export function isEmail (str) {
return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(str)
}
/**
* 手机号码
* @param str
*/
export function isMobile (str) {
return pattern.mobie.test(str)
}
/**
* 电话号码
* @param str
*/
export function isPhone (str) {
return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(str)
}
export default {
pattern
}

View File

@@ -0,0 +1,297 @@
import { Message } from 'element-ui';
import { treeDataTranslate } from '@/utils';
const DEFAULT_PAGE_SIZE = 10;
export class DropdownWidget {
/**
* 下拉组件Select、Cascade、TreeSelect等
* @param {function () : Promise} loadDropdownData 下拉数据获取函数
* @param {Boolean} isTree 是否是树数据
* @param {String} idKey 键字段字段名
* @param {String} parentIdKey 父字段字段名
*/
constructor (loadDropdownData, isTree = false, idKey = 'id', parentIdKey = 'parentId') {
this.loading = false;
this.dirty = true;
this.dropdownList = [];
this.isTree = isTree;
this.idKey = idKey;
this.parentIdKey = parentIdKey;
this.loadDropdownData = loadDropdownData;
this.setDropdownList = this.setDropdownList.bind(this);
this.onVisibleChange = this.onVisibleChange.bind(this);
}
/**
* 重新获取下拉数据
*/
reloadDropdownData () {
return new Promise((resolve, reject) => {
if (!this.loading) {
if (typeof this.loadDropdownData === 'function') {
this.loading = true;
this.loadDropdownData().then(dataList => {
this.setDropdownList(dataList);
this.loading = false;
this.dirty = false;
resolve(this.dropdownList);
}).catch(e => {
this.setDropdownList([]);
this.loading = false;
reject(this.dropdownList);
});
} else {
reject(new Error('获取下拉数据失败'));
}
} else {
resolve(this.dropdownList);
}
});
}
/**
* 下拉框显示或隐藏时调用
* @param {Boolean} isShow 正在显示或者隐藏
*/
onVisibleChange (isShow) {
return new Promise((resolve, reject) => {
if (isShow && this.dirty && !this.loading) {
this.reloadDropdownData().then(res => {
resolve(res);
}).catch(e => {
reject(e);
});
} else {
resolve(this.dropdownList);
}
});
}
/**
* 设置下拉数据
* @param {Array} dataList 要显示的下拉数据
*/
setDropdownList (dataList) {
if (Array.isArray(dataList)) {
this.dropdownList = this.isTree ? treeDataTranslate(dataList, this.idKey, this.parentIdKey) : dataList;
}
}
}
export class TableWidget {
/**
* 表格组件
* @param {function (params: Object) : Promise} loadTableData 表数据获取函数
* @param {function () : Boolean} verifyTableParameter 表数据获取检验函数
* @param {Boolean} paged 是否支持分页
* @param {String} orderFieldName 默认排序字段
* @param {Boolean} ascending 默认排序方式true为正序false为倒序
* @param {String} dateAggregateBy 默认排序字段的日期统计类型
*/
constructor (loadTableData, verifyTableParameter, paged, orderFieldName, ascending, dateAggregateBy) {
this.oldPage = 0;
this.currentPage = 1;
this.oldPageSize = DEFAULT_PAGE_SIZE;
this.pageSize = DEFAULT_PAGE_SIZE;
this.totalCount = 0;
this.dataList = [];
this.orderInfo = {
fieldName: orderFieldName,
asc: ascending,
dateAggregateBy: dateAggregateBy
};
this.paged = paged;
this.searchVerify = verifyTableParameter || function () { return true; };
this.loadTableData = loadTableData || function () { return Promise.resolve(); };
this.onCurrentPageChange = this.onCurrentPageChange.bind(this);
this.onPageSizeChange = this.onPageSizeChange.bind(this);
this.onSortChange = this.onSortChange.bind(this);
this.getTableIndex = this.getTableIndex.bind(this);
}
/**
* 表格分页变化
* @param {Integer} newCurrentPage 变化后的显示页面
*/
onCurrentPageChange (newCurrentPage) {
this.loadTableDataImpl(newCurrentPage, this.pageSize).then(() => {
this.oldPage = this.currentPage = newCurrentPage;
}).catch(() => {
this.currentPage = this.oldPage;
});
}
/**
* 表格分页每页显示数量变化
* @param {Integer} newPageSize 变化后的每页显示数量
*/
onPageSizeChange (newPageSize) {
this.loadTableDataImpl(1, newPageSize).then(() => {
this.oldPage = this.currentPage = 1;
this.oldPageSize = this.pageSize = newPageSize;
}).catch(e => {
this.currentPage = this.oldPage;
this.pageSize = this.oldPageSize;
});
}
/**
* 表格排序字段变化
* @param {String} prop 排序字段的字段名
* @param {String} order 正序还是倒序
*/
onSortChange ({ prop, order }) {
this.orderInfo.fieldName = prop;
this.orderInfo.asc = (order === 'ascending');
this.refreshTable();
}
/**
* 获取每一行的index信息
* @param {Integer} index 表格在本页位置
*/
getTableIndex (index) {
return this.paged ? ((this.currentPage - 1) * this.pageSize + (index + 1)) : (index + 1);
}
/**
* 获取表格数据
* @param {Integer} pageNum 当前分页
* @param {Integer} pageSize 每页数量
* @param {Boolean} reload 是否重新获取数据
*/
loadTableDataImpl (pageNum, pageSize, reload = false) {
return new Promise((resolve, reject) => {
if (typeof this.loadTableData !== 'function') {
reject();
} else {
// 如果pageSize和pageNum没有变化并且不强制刷新
if (this.paged && !reload && this.oldPage === pageNum && this.oldPageSize === pageSize) {
resolve();
} else {
let params = {};
if (this.orderInfo.fieldName != null) params.orderParam = [this.orderInfo];
if (this.paged) {
params.pageParam = {
pageNum,
pageSize
}
}
this.loadTableData(params).then(tableData => {
this.dataList = tableData.dataList;
this.totalCount = tableData.totalCount;
resolve();
}).catch(e => {
reject(e);
});
}
}
});
}
/**
* 刷新表格数据
* @param {Boolean} research 是否按照新的查询条件重新查询调用verify函数
* @param {Integer} pageNum 当前页面
*/
refreshTable (research = false, pageNum = undefined, showMsg = false) {
let reload = false;
if (research) {
if (typeof this.searchVerify === 'function' && !this.searchVerify()) return;
reload = true;
}
if (Number.isInteger(pageNum) && pageNum !== this.currentPage) {
this.loadTableDataImpl(pageNum, this.pageSize, reload).then(res => {
this.oldPage = this.currentPage = pageNum;
if (research && showMsg) Message.success('查询成功');
}).catch(e => {
this.currentPage = this.oldPage;
});
} else {
this.loadTableDataImpl(this.currentPage, this.pageSize, true).catch(e => {});
}
}
}
export class UploadWidget {
/**
* 上传组件
* @param {Integer} maxCount 最大上传数量
*/
constructor (maxCount = 1) {
this.maxCount = maxCount;
this.fileList = [];
this.onFileChange = this.onFileChange.bind(this);
}
/**
* 上传文件列表改变
* @param {Object} file 改变的文件
* @param {Array} fileList 改变后的文件列表
*/
onFileChange (file, fileList) {
if (Array.isArray(fileList) && fileList.length > 0) {
if (this.maxCount === 1) {
this.fileList = [fileList[fileList.length - 1]];
} else {
this.fileList = fileList;
}
} else {
this.fileList = [];
}
return this.fileList;
}
}
export class ChartWidget {
/**
* 图表组件
* @param {function (params) : Promise} loadTableData chart数据获取函数
* @param {function () : Boolean} verifyTableParameter 数据参数检验函数
* @param {Array} columns 数据列
*/
constructor (loadTableData, verifyTableParameter, columns) {
this.columns = columns;
this.loading = false;
this.dataEmpty = false;
this.chartData = undefined;
this.chartObject = undefined;
this.dimensionMaps = new Map();
this.chartSetting = undefined;
this.searchVerify = verifyTableParameter || function () { return true; };
this.loadTableData = loadTableData || function () { return Promise.resolve(); };
}
/**
* 获取图表数据
* @param {Boolean} reload 是否重新获取数据
*/
loadChartDataImpl (reload = false) {
return new Promise((resolve, reject) => {
if (typeof this.loadTableData !== 'function') {
reject();
} else {
if (!reload) {
resolve();
} else {
this.loading = true;
this.loadTableData().then(tableData => {
this.chartData = {
columns: this.columns,
rows: tableData.dataList
}
this.loading = false;
if (this.chartObject) this.chartObject.resize();
resolve();
}).catch(e => {
console.error(e);
this.loading = false;
reject(e);
});
}
}
});
}
/**
* 刷新图表数据
* @param {Boolean} research 是否按照新的查询条件重新查询调用verify函数
*/
refreshChart (research = false) {
if (research) {
if (typeof this.searchVerify === 'function' && !this.searchVerify()) return;
}
this.loadChartDataImpl(true).catch(e => {});
}
}