utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * @Author:zouwenying
  3. * @Date: 2020-10-23 19:29:07
  4. * @LastEditTime: 2020-11-13 18:43:56
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \vue-manage-system-master\src\utils\utils.js
  8. */
  9. import Vue from 'vue'
  10. import newValidate from './newValidate.js';
  11. import MsgBoxCom from '../components/msgBox';
  12. Vue.prototype.$valid = newValidate;
  13. //全局混入
  14. //mix_path为权限前缀
  15. Vue.mixin({
  16. computed: {
  17. $add() {
  18. return this.$store.getters["hasPermission"](this.mix_path + ":add")
  19. },
  20. $del() {
  21. return this.$store.getters["hasPermission"](this.mix_path + ":del")
  22. },
  23. $edit() {
  24. return this.$store.getters["hasPermission"](this.mix_path + ":edit")
  25. },
  26. $query() {
  27. return this.$store.getters["hasPermission"](this.mix_path + ":query")
  28. },
  29. }
  30. })
  31. /**
  32. * @description: 弹出确认提示框
  33. * @param {String} msg: 主消息
  34. * @param {String} tipMsg: 提示内容
  35. * @param {String} type: 告警类型 ['error', 'warn']
  36. * @param {Object} params: 扩展参数
  37. * @return {type}: null
  38. */
  39. Vue.prototype.$msgBox = (msg = '', tipMsg = '删除后将无法恢复,是否继续?', type = 'error', params = {}) => new Promise((resolve, reject) => {
  40. const config = {
  41. width: '374px',
  42. showCancelButton: true,
  43. confirmButtonText: '确认',
  44. showClose: true,
  45. resolve: resolve,
  46. reject: reject,
  47. }
  48. Object.assign(config, params)
  49. const MsgBox = Vue.extend(MsgBoxCom)
  50. const dom = new MsgBox().$mount()
  51. document.getElementById('app').appendChild(dom.$el)
  52. dom.config = config
  53. dom.msg = msg
  54. dom.tipMsg = tipMsg
  55. dom.type = type
  56. })
  57. /**
  58. * @description: 遍历节点是否存在当前值
  59. * @param {Array} dataArr 数组对象
  60. * @param {String|Number} val 比对值
  61. * @param {String} valStr 比对字段
  62. * @return {Boolean} 返回存在或不存在
  63. */
  64. Vue.prototype.__calleArr = function (dataArr, val, valStr) {
  65. for (let i in dataArr) {
  66. var data = dataArr[i];
  67. if (data[valStr] === val) {
  68. return true;
  69. } else {
  70. if (data.children) {
  71. this.__calleArr(data.children, val, valStr)
  72. }
  73. }
  74. }
  75. }
  76. // 确认提示框
  77. Vue.prototype.__confirm = function (msg = '确认要删除该数据?', title = '提示', settings) {
  78. let sets = Object.assign(settings || {}, {
  79. cancelButtonClass: 'el-button--medium',
  80. confirmButtonClass: 'el-button--medium',
  81. dangerouslyUseHTMLString: true,
  82. })
  83. return this.$confirm(`<p class="text_normal bold">${msg}</p>`, title, sets);
  84. }
  85. /*
  86. 设置弹出组件 datakey 集合下各字段的值,根据params.data
  87. */
  88. Vue.prototype.__setValue = function (datakey) {
  89. let obj = this[datakey];
  90. for (let item in obj) {
  91. const str = this.params.data[item];
  92. obj[item] = str ? str : _.isNumber(str) ? str : ''
  93. }
  94. }
  95. // Excel表格下载
  96. Vue.prototype.__exportExcel = (
  97. url = "Abnormal/getAllAbnormalExcel",
  98. params = {},
  99. token = localStorage.getItem("SC_token")
  100. ) => {
  101. // eslint-disable-next-line no-param-reassign
  102. delete params.pageNum;
  103. // eslint-disable-next-line no-param-reassign
  104. delete params.pageSize;
  105. let link;
  106. if (document.getElementById("exportATag")) {
  107. link = document.getElementById("exportATag");
  108. } else {
  109. link = document.createElement("a");
  110. link.setAttribute("id", "exportATag");
  111. link.style.display = "none";
  112. }
  113. const httpReg = /(http|https):\/\/([\w.]+\/?)\S*/;
  114. let urlStr = httpReg.test(url) ? `${url}&` : `${url}?`;
  115. _.mapKeys(params, (val, key) => {
  116. if (!_.isEmpty(String(val)) && val) {
  117. urlStr += `${key}=${val}&`;
  118. }
  119. });
  120. link.href = `${urlStr}access_token=${token}`;
  121. document.body.appendChild(link);
  122. link.click();
  123. };