utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. const deepmerge = require("deepmerge");
  2. /**
  3. * 判断对象类型
  4. * @param {*} obj 对象
  5. */
  6. function objTypeOf(obj) {
  7. const toString = Object.prototype.toString;
  8. const map = {
  9. "[object Boolean]": "boolean",
  10. "[object Number]": "number",
  11. "[object String]": "string",
  12. "[object Function]": "function",
  13. "[object Array]": "array",
  14. "[object Date]": "date",
  15. "[object RegExp]": "regExp",
  16. "[object Undefined]": "undefined",
  17. "[object Null]": "null",
  18. "[object Object]": "object"
  19. };
  20. return map[toString.call(obj)];
  21. }
  22. /**
  23. * 深拷贝
  24. * @param {Object/Array} data 需要拷贝的数据
  25. */
  26. function deepCopy(data, options) {
  27. const defObj = objTypeOf(data) === "array" ? [] : {};
  28. return deepmerge(defObj, data, options || {});
  29. }
  30. /**
  31. * 将目标对象中有的属性值与源对象中的属性值合并
  32. * @param {Object} target 目标对象
  33. * @param {Object} sources 源对象
  34. */
  35. function objAssign(target, sources) {
  36. let targ = { ...target };
  37. for (let k in targ) {
  38. targ[k] = Object.prototype.hasOwnProperty.call(sources, k)
  39. ? sources[k]
  40. : targ[k];
  41. }
  42. return targ;
  43. }
  44. /**
  45. * 文件流下载
  46. * @param {Object} option 文件下载设置
  47. */
  48. function download(option) {
  49. let defOpt = {
  50. type: "get",
  51. url: "",
  52. data: "",
  53. fileName: "",
  54. header: ""
  55. };
  56. let opt = objAssign(defOpt, option);
  57. return new Promise((resolve, reject) => {
  58. let xhr = new XMLHttpRequest();
  59. const requestType = opt.type.toUpperCase();
  60. const IS_POST = requestType === "POST";
  61. xhr.open(requestType, opt.url, true);
  62. if (IS_POST)
  63. xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  64. xhr.responseType = "blob";
  65. // header set
  66. if (opt.header && objTypeOf(opt.header) === "object") {
  67. for (let key in opt.header) {
  68. xhr.setRequestHeader(key, opt.header[key]);
  69. }
  70. }
  71. xhr.onload = function() {
  72. if (this.readyState === 4 && this.status === 200) {
  73. var blob = this.response;
  74. let pdfUrl = "";
  75. let uRl = window.URL || window.webkitURL;
  76. if (uRl && uRl.createObjectURL) {
  77. pdfUrl = uRl.createObjectURL(blob);
  78. } else {
  79. reject("浏览器不兼容!");
  80. }
  81. let a = document.createElement("a");
  82. a.download = opt.fileName;
  83. a.href = pdfUrl;
  84. document.body.appendChild(a);
  85. a.click();
  86. a.parentNode.removeChild(a);
  87. resolve();
  88. } else {
  89. reject(this);
  90. }
  91. };
  92. if (IS_POST) {
  93. // let formData = new FormData();
  94. // for (let key in opt.data) {
  95. // formData.append(key, opt.data[key]);
  96. // }
  97. // xhr.send(formData);
  98. xhr.send(JSON.stringify(opt.data));
  99. } else {
  100. xhr.send();
  101. }
  102. });
  103. }
  104. /**
  105. * 构建图表btn
  106. * @param {Function} h createElement
  107. * @param {Array} actions 操作分类数组
  108. */
  109. function tableAction(h, actions) {
  110. return actions.map(item => {
  111. let attr = {
  112. props: {
  113. type: item.type || "primary",
  114. size: "small",
  115. disabled: !!item.disabled,
  116. icon: item.icon
  117. },
  118. attrs: item.attrs,
  119. on: {
  120. click: () => {
  121. item.action();
  122. }
  123. }
  124. };
  125. return h("Button", attr, item.name);
  126. });
  127. }
  128. /**
  129. * 构建图表icon btn
  130. * @param {Function} h createElement
  131. * @param {Array} actions 操作分类数组
  132. */
  133. function tableIconAction(h, actions) {
  134. return actions.map(item => {
  135. let attr = {
  136. class: item.classes,
  137. props: {
  138. size: item.size || 16,
  139. type: item.icon,
  140. color: item.color
  141. },
  142. attrs: item.attrs,
  143. on: {
  144. click: () => {
  145. item.action();
  146. }
  147. }
  148. };
  149. return h("Icon", attr, item.name);
  150. });
  151. }
  152. /**
  153. * 获取随机code,默认获取16位
  154. * @param {Number} len 推荐8的倍数
  155. *
  156. */
  157. function randomCode(len = 16) {
  158. if (len <= 0) return;
  159. let steps = Math.ceil(len / 8);
  160. let stepNums = [];
  161. for (let i = 0; i < steps; i++) {
  162. let ranNum = Math.random()
  163. .toString(32)
  164. .slice(-8);
  165. stepNums.push(ranNum);
  166. }
  167. return stepNums.join("");
  168. }
  169. /**
  170. * 序列化参数
  171. * @param {Object} params 参数对象
  172. */
  173. function qsParams(params) {
  174. return Object.entries(params)
  175. .map(([key, val]) => `${key}=${val}`)
  176. .join("&");
  177. }
  178. /**
  179. *
  180. * @param {String} format 时间格式
  181. * @param {Date} date 需要格式化的时间对象
  182. */
  183. function formatDate(format = "YYYY-MM-DD HH:mm:ss", date = new Date()) {
  184. if (objTypeOf(date) !== "date") return;
  185. const options = {
  186. "Y+": date.getFullYear(),
  187. "M+": date.getMonth() + 1,
  188. "D+": date.getDate(),
  189. "H+": date.getHours(),
  190. "m+": date.getMinutes(),
  191. "s+": date.getSeconds()
  192. };
  193. Object.entries(options).map(([key, val]) => {
  194. if (new RegExp("(" + key + ")").test(format)) {
  195. const zeros = key === "Y+" ? "0000" : "00";
  196. const value = (zeros + val).substr(("" + val).length);
  197. format = format.replace(RegExp.$1, value);
  198. }
  199. });
  200. return format;
  201. }
  202. /**
  203. * 清除html标签
  204. * @param {String} str html字符串
  205. */
  206. function removeHtmlTag(str) {
  207. return str.replace(/<[^>]+>/g, "");
  208. }
  209. /**
  210. * 驼峰命名
  211. * @param {Array} params
  212. */
  213. function humpFormat(params) {
  214. return params
  215. .map(item => {
  216. const lowStr = item.toLowerCase();
  217. return lowStr.slice(0, 1).toUpperCase() + lowStr.slice(1);
  218. })
  219. .join("");
  220. }
  221. /**
  222. * 计算总数
  223. * @param {Array} dataList 需要统计的数组
  224. */
  225. function calcSum(dataList) {
  226. return dataList.reduce(function(total, item) {
  227. return total + item;
  228. }, 0);
  229. }
  230. /**
  231. *
  232. * @param {Object} obj 对象
  233. */
  234. function filterObjNull(obj) {
  235. let newObj = {};
  236. Object.keys(obj).map(key => {
  237. if (obj[key] !== null) newObj[key] = obj[key];
  238. });
  239. return newObj;
  240. }
  241. export {
  242. objTypeOf,
  243. deepCopy,
  244. objAssign,
  245. download,
  246. tableAction,
  247. tableIconAction,
  248. randomCode,
  249. qsParams,
  250. formatDate,
  251. removeHtmlTag,
  252. humpFormat,
  253. calcSum,
  254. filterObjNull
  255. };