utils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. Vue.prototype.$valid = newValidate;
  12. //全局混入
  13. //mix_path为权限前缀
  14. Vue.mixin({
  15. computed: {
  16. $add() {
  17. return this.$store.getters["hasPermission"](this.mix_path + ":add")
  18. },
  19. $del() {
  20. return this.$store.getters["hasPermission"](this.mix_path + ":del")
  21. },
  22. $edit() {
  23. return this.$store.getters["hasPermission"](this.mix_path + ":edit")
  24. },
  25. $query() {
  26. return this.$store.getters["hasPermission"](this.mix_path + ":query")
  27. },
  28. }
  29. })
  30. /**
  31. * @description: 弹出确认提示框
  32. * @param {String} msg: 主消息
  33. * @param {String} tipMsg: 提示内容
  34. * @param {String} type: 告警类型 ['error', 'warn']
  35. * @param {Object} params: 扩展参数
  36. * @return {type}: null
  37. */
  38. Vue.prototype.$msgBox = (msg = '', tipMsg = '删除后将无法恢复,是否继续?', type = 'error', params = {}) => new Promise((resolve, reject) => {
  39. const config = {
  40. width: '374px',
  41. showCancelButton: true,
  42. confirmButtonText: '确认',
  43. showClose: true
  44. }
  45. Object.assign(config, params)
  46. const MsgBox = Vue.extend({
  47. data() {
  48. return {
  49. showMsg: true,
  50. close: false,
  51. config
  52. }
  53. },
  54. methods: {
  55. cancel() {
  56. this.close = true
  57. setTimeout(() => {
  58. this.showMsg = false
  59. }, 300)
  60. reject()
  61. },
  62. confirm() {
  63. this.close = true
  64. setTimeout(() => {
  65. this.showMsg = false
  66. }, 300)
  67. resolve()
  68. }
  69. },
  70. template: `
  71. <div class="alert" v-if='showMsg' style="z-index: 9999">
  72. <div class="message-box" :class='{"up": close}' :style="{width: config.width}">
  73. <div class="row-1">
  74. <i class="dmp-icon-btn icon-${type}"></i>
  75. <span class="title">${msg}</span>
  76. <i class="close" @click='cancel' v-if="config.showClose"></i>
  77. </div>
  78. <div class="row-2 ${type}">${tipMsg}</div>
  79. <div class="row-3">
  80. <el-button @click='cancel' v-if="config.showCancelButton">取消</el-button>
  81. <el-button type='primary' @click='confirm'>{{config.confirmButtonText}}</el-button>
  82. </div>
  83. </div>
  84. </div>
  85. `
  86. })
  87. const dom = new MsgBox().$mount()
  88. document.getElementById('app').appendChild(dom.$el)
  89. })
  90. /**
  91. * @description: 遍历节点是否存在当前值
  92. * @param {Array} dataArr 数组对象
  93. * @param {String|Number} val 比对值
  94. * @param {String} valStr 比对字段
  95. * @return {Boolean} 返回存在或不存在
  96. */
  97. Vue.prototype.__calleArr = function (dataArr, val, valStr) {
  98. for (let i in dataArr) {
  99. var data = dataArr[i];
  100. if (data[valStr] === val) {
  101. return true;
  102. } else {
  103. if (data.children) {
  104. this.__calleArr(data.children, val, valStr)
  105. }
  106. }
  107. }
  108. }
  109. // 确认提示框
  110. Vue.prototype.__confirm = function (msg = '确认要删除该数据?', title = '提示', settings) {
  111. let sets = Object.assign(settings || {}, {
  112. cancelButtonClass: 'el-button--medium',
  113. confirmButtonClass: 'el-button--medium',
  114. dangerouslyUseHTMLString: true,
  115. })
  116. return this.$confirm(`<p class="text_normal bold">${msg}</p>`, title, sets);
  117. }
  118. /*
  119. 设置弹出组件 datakey 集合下各字段的值,根据params.data
  120. */
  121. Vue.prototype.__setValue = function (datakey) {
  122. let obj = this[datakey];
  123. for (let item in obj) {
  124. const str = this.params.data[item];
  125. obj[item] = str ? str : _.isNumber(str) ? str : ''
  126. }
  127. }
  128. // Excel表格下载
  129. Vue.prototype.__exportExcel = (
  130. url = "Abnormal/getAllAbnormalExcel",
  131. params = {},
  132. token = localStorage.getItem("SC_token")
  133. ) => {
  134. // eslint-disable-next-line no-param-reassign
  135. delete params.pageNum;
  136. // eslint-disable-next-line no-param-reassign
  137. delete params.pageSize;
  138. let link;
  139. if (document.getElementById("exportATag")) {
  140. link = document.getElementById("exportATag");
  141. } else {
  142. link = document.createElement("a");
  143. link.setAttribute("id", "exportATag");
  144. link.style.display = "none";
  145. }
  146. const httpReg = /(http|https):\/\/([\w.]+\/?)\S*/;
  147. let urlStr = httpReg.test(url) ? `${url}&` : `${url}?`;
  148. _.mapKeys(params, (val, key) => {
  149. if (!_.isEmpty(String(val)) && (val == 0 || val)) {
  150. urlStr += `${key}=${val}&`;
  151. }
  152. });
  153. link.href = `${urlStr}access_token=${token}`;
  154. document.body.appendChild(link);
  155. link.click();
  156. };