123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- const deepmerge = require("deepmerge");
- /**
- * 判断对象类型
- * @param {*} obj 对象
- */
- function objTypeOf(obj) {
- const toString = Object.prototype.toString;
- const 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)];
- }
- /**
- * 深拷贝
- * @param {Object/Array} data 需要拷贝的数据
- */
- function deepCopy(data, options) {
- const defObj = objTypeOf(data) === "array" ? [] : {};
- return deepmerge(defObj, data, options || {});
- }
- /**
- * 将目标对象中有的属性值与源对象中的属性值合并
- * @param {Object} target 目标对象
- * @param {Object} sources 源对象
- */
- function objAssign(target, sources) {
- let targ = { ...target };
- for (let k in targ) {
- targ[k] = Object.prototype.hasOwnProperty.call(sources, k)
- ? sources[k]
- : targ[k];
- }
- return targ;
- }
- /**
- * 文件流下载
- * @param {Object} option 文件下载设置
- */
- function download(option) {
- let defOpt = {
- type: "get",
- url: "",
- data: "",
- fileName: "",
- header: ""
- };
- let opt = objAssign(defOpt, option);
- return new Promise((resolve, reject) => {
- let xhr = new XMLHttpRequest();
- const requestType = opt.type.toUpperCase();
- const IS_POST = requestType === "POST";
- xhr.open(requestType, opt.url, true);
- if (IS_POST)
- xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
- xhr.responseType = "blob";
- // header set
- if (opt.header && objTypeOf(opt.header) === "object") {
- for (let key in opt.header) {
- xhr.setRequestHeader(key, opt.header[key]);
- }
- }
- xhr.onload = function() {
- if (this.readyState === 4 && this.status === 200) {
- var blob = this.response;
- let pdfUrl = "";
- let uRl = window.URL || window.webkitURL;
- if (uRl && uRl.createObjectURL) {
- pdfUrl = uRl.createObjectURL(blob);
- } else {
- reject("浏览器不兼容!");
- }
- let a = document.createElement("a");
- a.download = opt.fileName;
- a.href = pdfUrl;
- document.body.appendChild(a);
- a.click();
- a.parentNode.removeChild(a);
- resolve();
- } else {
- reject(this);
- }
- };
- if (IS_POST) {
- // let formData = new FormData();
- // for (let key in opt.data) {
- // formData.append(key, opt.data[key]);
- // }
- // xhr.send(formData);
- xhr.send(JSON.stringify(opt.data));
- } else {
- xhr.send();
- }
- });
- }
- /**
- * 构建图表btn
- * @param {Function} h createElement
- * @param {Array} actions 操作分类数组
- */
- function tableAction(h, actions) {
- return actions.map(item => {
- let attr = {
- props: {
- type: item.type || "primary",
- size: "small",
- disabled: !!item.disabled,
- icon: item.icon
- },
- attrs: item.attrs,
- on: {
- click: () => {
- item.action();
- }
- }
- };
- return h("Button", attr, item.name);
- });
- }
- /**
- * 构建图表icon btn
- * @param {Function} h createElement
- * @param {Array} actions 操作分类数组
- */
- function tableIconAction(h, actions) {
- return actions.map(item => {
- let attr = {
- class: item.classes,
- props: {
- size: item.size || 16,
- type: item.icon,
- color: item.color
- },
- attrs: item.attrs,
- on: {
- click: () => {
- item.action();
- }
- }
- };
- return h("Icon", attr, item.name);
- });
- }
- /**
- * 获取随机code,默认获取16位
- * @param {Number} len 推荐8的倍数
- *
- */
- function randomCode(len = 16) {
- if (len <= 0) return;
- let steps = Math.ceil(len / 8);
- let stepNums = [];
- for (let i = 0; i < steps; i++) {
- let ranNum = Math.random()
- .toString(32)
- .slice(-8);
- stepNums.push(ranNum);
- }
- return stepNums.join("");
- }
- /**
- * 序列化参数
- * @param {Object} params 参数对象
- */
- function qsParams(params) {
- return Object.entries(params)
- .map(([key, val]) => `${key}=${val}`)
- .join("&");
- }
- /**
- *
- * @param {String} format 时间格式
- * @param {Date} date 需要格式化的时间对象
- */
- function formatDate(format = "YYYY-MM-DD HH:mm:ss", date = new Date()) {
- if (objTypeOf(date) !== "date") return;
- const options = {
- "Y+": date.getFullYear(),
- "M+": date.getMonth() + 1,
- "D+": date.getDate(),
- "H+": date.getHours(),
- "m+": date.getMinutes(),
- "s+": date.getSeconds()
- };
- Object.entries(options).map(([key, val]) => {
- if (new RegExp("(" + key + ")").test(format)) {
- const zeros = key === "Y+" ? "0000" : "00";
- const value = (zeros + val).substr(("" + val).length);
- format = format.replace(RegExp.$1, value);
- }
- });
- return format;
- }
- /**
- * 清除html标签
- * @param {String} str html字符串
- */
- function removeHtmlTag(str) {
- return str.replace(/<[^>]+>/g, "");
- }
- /**
- * 驼峰命名
- * @param {Array} params
- */
- function humpFormat(params) {
- return params
- .map(item => {
- const lowStr = item.toLowerCase();
- return lowStr.slice(0, 1).toUpperCase() + lowStr.slice(1);
- })
- .join("");
- }
- /**
- * 计算总数
- * @param {Array} dataList 需要统计的数组
- */
- function calcSum(dataList) {
- return dataList.reduce(function(total, item) {
- return total + item;
- }, 0);
- }
- /**
- *
- * @param {Object} obj 对象
- */
- function filterObjNull(obj) {
- let newObj = {};
- Object.keys(obj).map(key => {
- if (obj[key] !== null) newObj[key] = obj[key];
- });
- return newObj;
- }
- export {
- objTypeOf,
- deepCopy,
- objAssign,
- download,
- tableAction,
- tableIconAction,
- randomCode,
- qsParams,
- formatDate,
- removeHtmlTag,
- humpFormat,
- calcSum,
- filterObjNull
- };
|