basicTool.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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} tableData 用于规定表格表头和内容的 Object
  148. * @param {String} excelName 导出的文件名
  149. */
  150. exportExcel (tableData, excelName) {
  151. const { export_json_to_excel } = require('@tools/excel/Export2Excel.js'); // 注意这个Export2Excel路径
  152. let tHeader = []; // 上面设置Excel的表格第一行的标题
  153. let filterVal = []; // 上面的index、nickName、name是tableData里对象的属性key值
  154. const formatJson = function (filterVal, jsonData) {
  155. return jsonData.map(v => filterVal.map(j => v[j]));
  156. };
  157. tableData.column.forEach(ele => {
  158. tHeader.push(ele.name);
  159. filterVal.push(ele.field);
  160. });
  161. const list = tableData.data; //把要导出的数据tableData存到list
  162. const data = formatJson(filterVal, list);
  163. export_json_to_excel(tHeader, data, (excelName || "导出的Excel")); // 最后一个是表名字
  164. },
  165. // JS 触发全屏功能
  166. requestFullscreen () {
  167. //全屏
  168. const docElm = document.documentElement;
  169. //W3C
  170. if (docElm.requestFullscreen) {
  171. docElm.requestFullscreen();
  172. }
  173. //FireFox
  174. else if (docElm.mozRequestFullScreen) {
  175. docElm.mozRequestFullScreen();
  176. }
  177. //Chrome等
  178. else if (docElm.webkitRequestFullScreen) {
  179. docElm.webkitRequestFullScreen();
  180. }
  181. //IE11
  182. else if (docElm.msRequestFullscreen) {
  183. docElm.msRequestFullscreen();
  184. }
  185. },
  186. /**
  187. * 颜色进制转换 16 <--> 10 互转
  188. * @param {String} colorStr 传入一个颜色字符串, 16进制 或者 10进制 ,返回转换后的结果,例:传入 #1890ff ,返回 rgb(24, 144, 255),反之亦然
  189. */
  190. replaceColor (colorStr) {
  191. if (!colorStr) return '';
  192. let colorString = colorStr.replace(/#|rgb|\(|\)|\|;|\s+/g, "");
  193. if (colorString.indexOf(",") === -1) {
  194. let color10 = [];
  195. for (let i = 0; i < colorString.length; i++) {
  196. if (!((i + 1) % 2)) {
  197. color10.push(parseInt((colorString[i - 1] + colorString[i]), 16));
  198. }
  199. }
  200. return "rgb(" + color10.toString() + ")";
  201. } else {
  202. let colorArray = colorString.split(',');
  203. let color16 = '';
  204. colorArray.forEach(ele => {
  205. color16 += (parseInt(ele).toString(16));
  206. });
  207. return "#" + color16;
  208. }
  209. },
  210. // 正则表达式
  211. regs: {
  212. // 是否为手机号
  213. isPhone: /^1(3|4|5|6|7|8|9)\d{9}$/,
  214. // 是否为合法 15 或 18 位身份证号
  215. 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)$/,
  216. // 是否是邮箱
  217. isMail: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
  218. // 是否是数字
  219. isNumber: /^(-?\d+)(\.\d+)?$/
  220. },
  221. elCkeck: {
  222. isNumber (rule, value, callback) {
  223. if (value === '') {
  224. callback(new Error('该值不可为空'));
  225. } else if (!/^(-?\d+)(\.\d+)?$/.test(value)) {
  226. callback(new Error('输入有误,仅支持输入数字'));
  227. } else {
  228. callback();
  229. }
  230. }
  231. }
  232. }