utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. xhr.open(opt.type.toUpperCase(), opt.url, true);
  60. xhr.responseType = "blob";
  61. // header set
  62. if (opt.header && objTypeOf(opt.header) === "object") {
  63. for (let key in opt.header) {
  64. xhr.setRequestHeader(key, opt.header[key]);
  65. }
  66. }
  67. xhr.onload = function() {
  68. if (this.readyState === 4 && this.status === 200) {
  69. var blob = this.response;
  70. let pdfUrl = "";
  71. let uRl = window.URL || window.webkitURL;
  72. if (uRl && uRl.createObjectURL) {
  73. pdfUrl = uRl.createObjectURL(blob);
  74. } else {
  75. reject("浏览器不兼容!");
  76. }
  77. let a = document.createElement("a");
  78. a.download = opt.fileName;
  79. a.href = pdfUrl;
  80. document.body.appendChild(a);
  81. a.click();
  82. a.parentNode.removeChild(a);
  83. resolve();
  84. } else {
  85. reject(this);
  86. }
  87. };
  88. if (opt.type.toUpperCase() === "POST") {
  89. xhr.send(opt.data);
  90. } else {
  91. xhr.send();
  92. }
  93. });
  94. }
  95. /**
  96. * 构建图表btn
  97. * @param {Function} h createElement
  98. * @param {Array} actions 操作分类数组
  99. */
  100. function tableAction(h, actions) {
  101. return actions.map(item => {
  102. let attr = {
  103. props: {
  104. type: item.type || "primary",
  105. size: "small",
  106. disabled: !!item.disabled
  107. },
  108. style: {},
  109. on: {
  110. click: () => {
  111. item.action();
  112. }
  113. }
  114. };
  115. return h("Button", attr, item.name);
  116. });
  117. }
  118. /**
  119. * 获取随机code,默认获取16位
  120. * @param {Number} len 推荐8的倍数
  121. *
  122. */
  123. function randomCode(len = 16) {
  124. if (len <= 0) return;
  125. let steps = Math.ceil(len / 8);
  126. let stepNums = [];
  127. for (let i = 0; i < steps; i++) {
  128. let ranNum = Math.random()
  129. .toString(32)
  130. .slice(-8);
  131. stepNums.push(ranNum);
  132. }
  133. return stepNums.join("");
  134. }
  135. /**
  136. * 序列化参数
  137. * @param {Object} params 参数对象
  138. */
  139. function qsParams(params) {
  140. return Object.entries(params)
  141. .map(([key, val]) => `${key}=${val}`)
  142. .join("&");
  143. }
  144. /**
  145. *
  146. * @param {String} format 时间格式
  147. * @param {Date} date 需要格式化的时间对象
  148. */
  149. function formatDate(format = "YYYY-MM-DD HH:mm:ss", date = new Date()) {
  150. if (objTypeOf(date) !== "date") return;
  151. const options = {
  152. "Y+": date.getFullYear(),
  153. "M+": date.getMonth() + 1,
  154. "D+": date.getDate(),
  155. "H+": date.getHours(),
  156. "m+": date.getMinutes(),
  157. "s+": date.getSeconds()
  158. };
  159. Object.entries(options).map(([key, val]) => {
  160. if (new RegExp("(" + key + ")").test(format)) {
  161. const zeros = key === "Y+" ? "0000" : "00";
  162. const value = (zeros + val).substr(("" + val).length);
  163. format = format.replace(RegExp.$1, value);
  164. }
  165. });
  166. return format;
  167. }
  168. /**
  169. * 清除html标签
  170. * @param {String} str html字符串
  171. */
  172. function removeHtmlTag(str) {
  173. return str.replace(/<[^>]+>/g, "");
  174. }
  175. /**
  176. * 驼峰命名
  177. * @param {Array} params
  178. */
  179. function humpFormat(params) {
  180. return params
  181. .map(item => {
  182. const lowStr = item.toLowerCase();
  183. return lowStr.slice(0, 1).toUpperCase() + lowStr.slice(1);
  184. })
  185. .join("");
  186. }
  187. function getNavs(routes) {
  188. return routes.map(item => {
  189. return {
  190. name: item.name,
  191. title: item.meta.title
  192. };
  193. });
  194. }
  195. /**
  196. * 计算总数
  197. * @param {Array} dataList 需要统计的数组
  198. */
  199. function calcSum(dataList) {
  200. return dataList.reduce(function(total, item) {
  201. return total + item;
  202. }, 0);
  203. }
  204. export {
  205. objTypeOf,
  206. deepCopy,
  207. objAssign,
  208. download,
  209. tableAction,
  210. randomCode,
  211. qsParams,
  212. formatDate,
  213. removeHtmlTag,
  214. humpFormat,
  215. getNavs,
  216. calcSum
  217. };