baseTool.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.slice(0, depthN + 1); //将目前匹配的数组,截断并保存到结果数组
  98. break;
  99. } else {
  100. if (childrenData[j].children) {
  101. depth++;
  102. childrenEach(childrenData[j].children, depth);
  103. }
  104. }
  105. }
  106. return returnArr;
  107. }
  108. return childrenEach(treeData, depth);
  109. },
  110. /**
  111. * 获取数据的类型
  112. * @param {any} options 传入一个数据,返回其类型 (object, array, string, number等)
  113. */
  114. getType(options) {
  115. return Object.prototype.toString.call(options).slice(8, Object.prototype.toString.call(options).length - 1).toLowerCase();
  116. },
  117. /**
  118. * 控制页面滚动到指定位置
  119. * @param {Object} options 传入一个配置项,其中:
  120. * @param {String} el 需要滚动的 DOM 元素选择器,可以为 CLASS 或 ID
  121. * @param {Number} scrollTop 需要滚动到顶部的位置,数值越低滚动的越靠近顶部,默认 0
  122. * @param {Number} scrollLeft 需要滚动到顶部的位置,数值越低滚动的越靠近顶部,默认 0
  123. * @param {Number} speed 滚动到指定位置需要的时间 (动画时间),默认 200
  124. * @param {Function} success 滚动执行完毕后的回调函数
  125. */
  126. scrollTo(options) {
  127. if (!options || !options.el) {
  128. this.showMsg({
  129. msg: 'scrollTo() 方法需要传入 el 属性'
  130. });
  131. return;
  132. }
  133. if ($(options.el)[0] && ($(options.el)[0].scrollHeight > (window.innerHeight || document.documentElement.clientHeight))) {
  134. $(options.el).animate({
  135. scrollTop: options.scrollTop || 0,
  136. scrollLeft: options.scrollLeft || 0,
  137. }, options.speed || 200, () => {
  138. options.success && options.success();
  139. });
  140. } else {
  141. options.success && options.success();
  142. }
  143. },
  144. /**
  145. * JS 触发全屏功能
  146. */
  147. requestFullscreen() {
  148. //全屏
  149. const docElm = document.documentElement;
  150. //W3C
  151. if (docElm.requestFullscreen) {
  152. docElm.requestFullscreen();
  153. }
  154. //FireFox
  155. else if (docElm.mozRequestFullScreen) {
  156. docElm.mozRequestFullScreen();
  157. }
  158. //Chrome等
  159. else if (docElm.webkitRequestFullScreen) {
  160. docElm.webkitRequestFullScreen();
  161. }
  162. //IE11
  163. else if (docElm.msRequestFullscreen) {
  164. docElm.msRequestFullscreen();
  165. }
  166. },
  167. /**
  168. * 颜色进制转换 16 <--> 10 互转
  169. * @param {String} colorStr 传入一个颜色字符串, 16进制 或者 10进制 ,返回转换后的结果,例:传入 #1890ff ,返回 rgb(24, 144, 255),反之亦然
  170. */
  171. replaceColor(colorStr) {
  172. if (!colorStr) return '';
  173. let colorString = colorStr.replace(/#|rgb|\(|\)|\|;|\s+/g, "");
  174. if (colorString.indexOf(",") === -1) {
  175. let color10 = [];
  176. for (let i = 0; i < colorString.length; i++) {
  177. if (!((i + 1) % 2)) {
  178. color10.push(parseInt((colorString[i - 1] + colorString[i]), 16));
  179. }
  180. }
  181. return "rgb(" + color10.toString() + ")";
  182. } else {
  183. let colorArray = colorString.split(',');
  184. let color16 = '';
  185. colorArray.forEach(ele => {
  186. color16 += (parseInt(ele).toString(16));
  187. });
  188. return "#" + color16;
  189. }
  190. },
  191. /**
  192. * 生成指定范围内的随机数
  193. * @param {Int} minNum 范围下限
  194. * @param {Int} maxNum 范围上限
  195. * @returns
  196. */
  197. randomNum(minNum, maxNum) {
  198. switch (arguments.length) {
  199. case 1:
  200. return parseInt(Math.random() * minNum + 1, 10);
  201. case 2:
  202. return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
  203. default:
  204. return 0;
  205. }
  206. },
  207. // 正则表达式
  208. regs: {
  209. // 是否为手机号
  210. isPhone: /^1(3|4|5|6|7|8|9)\d{9}$/,
  211. // 是否为合法 15 或 18 位身份证号
  212. 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)$/,
  213. // 是否是邮箱
  214. isMail: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
  215. // 是否是数字
  216. isNumber: /^(-?\d+)(\.\d+)?$/
  217. },
  218. // elementUI 自定义表单效验规则
  219. elCkeck: {
  220. isNumber(rule, value, callback) {
  221. if (value === '') {
  222. callback(new Error('该值不可为空'));
  223. } else if (!/^(-?\d+)(\.\d+)?$/.test(value)) {
  224. callback(new Error('输入有误,仅支持输入数字'));
  225. } else {
  226. callback();
  227. }
  228. }
  229. }
  230. }