basicTool.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. let loadingStatus = null;
  2. import { ElMessage } from 'element-plus';
  3. import { ElLoading } from 'element-plus';
  4. export default {
  5. /**
  6. * 页面顶部出现消息提示
  7. * @param {Object} options 传入一个对象为配置项,其中:
  8. * @param {Boolean} showClose 是否显示可手动关闭的 x 于提示框右侧,默认 false
  9. * @param {Boolean} center 消息提示内容是否居中,默认 true
  10. * @param {String} msg 消息提示的内容
  11. * @param {String} type 消息提示的类型,可选值为 ['success(成功)','warning(警告)','error(错误)',或者直接传入空字符串],默认 error
  12. */
  13. showMsg (options) {
  14. ElMessage({
  15. showClose: (options.showClose == true || options.showClose == false) ? options.showClose : false,
  16. center: (options.center == true || options.center == false) ? options.center : true,
  17. message: options.msg,
  18. type: (options.type || options.type === '') ? options.type : 'error'
  19. });
  20. },
  21. /**
  22. * 显示防穿透点击 loading 蒙版
  23. * @param {Objectr} opt 传入一个对象为配置项,其中:
  24. * @param {String} target 此蒙版需要绑定的 DOM 标签 ID 或者 CLASS 或者 TAGNAME,默认绑在 body 上
  25. * @param {Boolean} body 是否插入蒙版至 boyd 上,默认 true
  26. * @param {Boolean} fullscreen 蒙版是否全屏蒙住整个 html 页面,默认 true
  27. * @param {Boolean} lock 蒙版出现时,是否锁定屏幕滚动,默认 false
  28. * @param {String} text 蒙版上显示的提示文本
  29. * @param {String} background 蒙版的背景颜色,写死 50% 透明度的纯黑色
  30. */
  31. showLoading (opt) {
  32. let options = opt || {};
  33. loadingStatus = ElLoading.service({
  34. target: options.target || 'body',
  35. body: (options.body == true || options.body == false) ? options.body : false,
  36. fullscreen: (options.fullscreen == true || options.fullscreen == false) ? options.fullscreen : true,
  37. lock: (options.lock == true || options.lock == false) ? options.lock : false,
  38. text: options.text || '请稍等...',
  39. background: 'rgba(0,0,0,.5)',
  40. });
  41. },
  42. /**
  43. * 获取标签上的自定义属性
  44. * @param {any} node 传入 字符串 或 标准DOM对象 或 jQuery DOM对象 ,函数自动判断传入的类型并返回其 dataset 属性。
  45. */
  46. getCurrentData (node) {
  47. // 如果传入的是 jQuery 对象
  48. if (window.jQuery && node instanceof jQuery) {
  49. return node[0].dataset;
  50. } else {
  51. // 判断传入的是否是标准 DOM 对象
  52. let isDom = (typeof node === 'object') ?
  53. function (obj) {
  54. return obj instanceof HTMLElement;
  55. } :
  56. function (obj) {
  57. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  58. };
  59. // 如果是标准 DOM 对象,输出 dataset
  60. if (isDom(node)) {
  61. return node.dataset;
  62. } else {
  63. // 如果是不是,则表示传入的是字符串,根据字符串取 DOM 后输出 dataset
  64. let dom = document.querySelector(node);
  65. return dom.dataset;
  66. }
  67. }
  68. },
  69. /**
  70. * 关闭loading
  71. */
  72. closeLoading () {
  73. loadingStatus.close();
  74. },
  75. /**
  76. * 深拷贝 json 数组
  77. * @param {Array} jsonArray 传入 Json 数组,返回一个指向新指针拷贝份数据
  78. */
  79. deepCopy (jsonArray) {
  80. return JSON.parse(JSON.stringify(jsonArray));
  81. },
  82. /**
  83. * 根据后端返回的 ID 遍历树形结构包装组件编辑用数据函数
  84. * @param {String} key 需要找到的 ID
  85. * @param {Array} treeData 树形 Array
  86. */
  87. getTreeDeepArr (key, treeData) {
  88. let arr = []; // 在递归时操作的数组
  89. let returnArr = []; // 存放结果的数组
  90. let depth = 0; // 定义全局层级
  91. // 定义递归函数
  92. function childrenEach (childrenData, depthN) {
  93. for (var j = 0; j < childrenData.length; j++) {
  94. depth = depthN; // 将执行的层级赋值 到 全局层级
  95. arr[depthN] = (childrenData[j].id);
  96. if (childrenData[j].id == key) {
  97. // returnArr = arr; // 原写法不行, 因 此赋值存在指针关系
  98. returnArr = arr.slice(0, depthN + 1); //将目前匹配的数组,截断并保存到结果数组
  99. break;
  100. } else {
  101. if (childrenData[j].children) {
  102. depth++;
  103. childrenEach(childrenData[j].children, depth);
  104. }
  105. }
  106. }
  107. return returnArr;
  108. }
  109. return childrenEach(treeData, depth);
  110. },
  111. /**
  112. * 获取数据的类型
  113. * @param {any} options 传入一个数据,返回其类型 (object, array, string, number等)
  114. */
  115. getType (options) {
  116. return Object.prototype.toString.call(options).slice(8, Object.prototype.toString.call(options).length - 1).toLowerCase();
  117. },
  118. /**
  119. * 控制页面滚动到指定位置
  120. * @param {Object} options 传入一个配置项,其中:
  121. * @param {String} el 需要滚动的 DOM 元素选择器,可以为 CLASS 或 ID
  122. * @param {Number} scrollTop 需要滚动到顶部的位置,数值越低滚动的越靠近顶部,默认 0
  123. * @param {Number} scrollLeft 需要滚动到顶部的位置,数值越低滚动的越靠近顶部,默认 0
  124. * @param {Number} speed 滚动到指定位置需要的时间 (动画时间),默认 200
  125. * @param {Function} success 滚动执行完毕后的回调函数
  126. */
  127. scrollTo (options) {
  128. if (!options || !options.el) {
  129. this.showMsg({
  130. msg: 'scrollTo() 方法需要传入 el 属性'
  131. });
  132. return;
  133. }
  134. if ($(options.el)[0] && ($(options.el)[0].scrollHeight > (window.innerHeight || document.documentElement.clientHeight))) {
  135. $(options.el).animate({
  136. scrollTop: options.scrollTop || 0,
  137. scrollLeft: options.scrollLeft || 0,
  138. }, options.speed || 200, () => {
  139. options.success && options.success();
  140. });
  141. } else {
  142. options.success && options.success();
  143. }
  144. },
  145. /**
  146. * 导出 Json 为 excel 表格
  147. * @param {Object} options 传入一个配置项,根据传入的配置项导出对应的 excel 表格,其中:
  148. * @param {Array} tTitle 表格的标题,置空则为默认以日期为标题的表格
  149. * @param {Array} tHeader 表格的表头,必传
  150. * @param {Array} tBody 表格需要展示的字段名,必传
  151. * @param {Array} tMerges 表格标题需要合并的单元格,置空则为默认 A1 格为表格标题显示区域
  152. * @param {Array} tData 表格渲染所用的数据源,必传
  153. * @param {Boolean} autoWidth 表是否根据表格内容自动撑开列宽,置空则为默认 true
  154. * @param {String} exportName 所导出的表格文件名,置空则以时间戳为文件名称进行导出
  155. */
  156. exportExcelForJson (options) {
  157. const {
  158. export_json_to_excel
  159. } = __getExport2Excel();
  160. let title = options.tTitle;
  161. if (!title || !title.length) {
  162. title = []
  163. options.tBody.forEach((ele, index) => {
  164. if (!index) {
  165. title.push(new Date().formatDate("yyyy-MM-dd") + '导出的表格');
  166. } else {
  167. title.push("");
  168. }
  169. });
  170. }
  171. let header = options.tHeader;
  172. let data = options.tData.map(data => options.tBody.map(key => data[key]));
  173. let merges = options.tMerges || [];
  174. let filename = options.exportName || new Date().getTime();
  175. let bookType = "xlsx";
  176. let autoWidth = (options.autoWidth == true || options.autoWidth == false) ? options.autoWidth : true;
  177. data.map(item => {
  178. item.map((i, index) => {
  179. if (!i) item[index] = "";
  180. });
  181. });
  182. export_json_to_excel({
  183. title,
  184. header,
  185. data,
  186. merges,
  187. filename,
  188. bookType,
  189. autoWidth
  190. });
  191. },
  192. /**
  193. * 颜色进制转换 16 <--> 10 互转
  194. * @param {String} colorStr 传入一个颜色字符串, 16进制 或者 10进制 ,返回转换后的结果,例:传入 #1890ff ,返回 rgb(24, 144, 255),反之亦然
  195. */
  196. replaceColor (colorStr) {
  197. if (!colorStr) return '';
  198. let colorString = colorStr.replace(/#|rgb|\(|\)|\|;|\s+/g, "");
  199. if (colorString.indexOf(",") === -1) {
  200. let color10 = [];
  201. for (let i = 0; i < colorString.length; i++) {
  202. if (!((i + 1) % 2)) {
  203. color10.push(parseInt((colorString[i - 1] + colorString[i]), 16));
  204. }
  205. }
  206. return "rgb(" + color10.toString() + ")";
  207. } else {
  208. let colorArray = colorString.split(',');
  209. let color16 = '';
  210. colorArray.forEach(ele => {
  211. color16 += (parseInt(ele).toString(16));
  212. });
  213. return "#" + color16;
  214. }
  215. },
  216. // 正则表达式
  217. regs: {
  218. // 是否为手机号
  219. isPhone: /^1(3|4|5|6|7|8|9)\d{9}$/,
  220. // 是否为合法 15 或 18 位身份证号
  221. isIdentityCard: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
  222. // 是否是邮箱
  223. isMail: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
  224. // 是否是数字
  225. isNumber: /^(-?\d+)(\.\d+)?$/
  226. }
  227. };