common.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { MessageBox, Message, Notification } from '@zjlib/element-ui2'
  2. export function tableDataParsing(fieldBeans) {
  3. return fieldBeans.map((item, index) => {
  4. var tiling =
  5. item.tiling || item.tiling === null || item.tiling === undefined ? true : false;
  6. return {
  7. tiling,
  8. exportField: item,
  9. hidden: item.isShow === null ? false : !item.isShow,
  10. isCopy: item.isCopy || false,
  11. isTotal: item.isTotal || false,
  12. sortNum: item.sortNum || 0,
  13. recordType: item.type,
  14. columnAttributes: {
  15. fixed: item.fixed ? item.fixed : false,
  16. label: item.label || '',
  17. prop: item.jname,
  18. width: item.width || 'auto',
  19. 'min-width': ((label) => label.length * 16 + 64)(item.label || ''),
  20. align: ~['number', 'amount'].indexOf(item.type) ? 'right' : 'left',
  21. },
  22. };
  23. });
  24. }
  25. export const successMsg = msg => {
  26. Message({
  27. showClose: true,
  28. message: msg || '操作成功',
  29. type: 'success'
  30. })
  31. }
  32. export const errorMsg = msg => {
  33. Message({
  34. showClose: true,
  35. message: msg || '操作成功',
  36. type: 'error'
  37. })
  38. }
  39. export const warningNotify = msg => {
  40. Notification({
  41. title: '提示',
  42. message: msg,
  43. type: 'warning',
  44. duration: 2000
  45. })
  46. }
  47. /**
  48. * 查询按钮权限
  49. * @param {*} value 当前按钮对应code
  50. * @param {*} btnRole 当前路由所有按钮权限
  51. * @returns
  52. */
  53. export const checkBtnRole = (value, btnRole) => {
  54. if (!btnRole) {
  55. return true
  56. }
  57. let index = btnRole.indexOf(value)
  58. return index >= 0
  59. }
  60. /**
  61. * table合计方法
  62. * 注意:sums1,sums2: 需要在获取列表数据时遍历传入需要合计的字段名
  63. * 其中:sums1: 数量,不需要toFixed
  64. * sums2: 金额,需要toFixed
  65. * 例如:res.data.items.forEach(item => {
  66. item.sums1 = ['number'];
  67. item.sums2 = ['totalAmount', 'payAmount'];
  68. })
  69. * @param {*} param
  70. * @returns
  71. */
  72. export const getSummaries = param => {
  73. const { columns, data } = param
  74. const sums = []
  75. columns.forEach((column, index) => {
  76. if (index === 0) {
  77. sums[index] = '合计'
  78. return
  79. }
  80. // console.log(columns);
  81. try {
  82. const values = data.map(item => Number(item[column.property]))
  83. if (data[0] && data[0].sums1.includes(column.property)) {
  84. sums[index] = values.reduce((prev, curr) => {
  85. const value = Number(curr)
  86. if (!isNaN(value)) {
  87. return prev + curr
  88. } else {
  89. return prev
  90. }
  91. }, 0)
  92. sums[index]
  93. }
  94. if (data[0] && data[0].sums2.includes(column.property)) {
  95. sums[index] = values.reduce((prev, curr) => {
  96. const value = Number(curr)
  97. if (!isNaN(value)) {
  98. return prev + curr
  99. } else {
  100. return prev
  101. }
  102. }, 0)
  103. sums[index] = numToFixed(sums[index])
  104. sums[index]
  105. }
  106. } catch {
  107. console.log('error')
  108. }
  109. })
  110. return sums
  111. }
  112. /**
  113. * 保留2位小数点
  114. * @param {number} num
  115. * @returns
  116. */
  117. export const numToFixed = num => {
  118. if (!num) return '0.00'
  119. if (isNaN(Number(num))) return num
  120. num = num.toFixed(2)
  121. if (num.includes('.')) {
  122. return num.slice(0, -3).replace(/(\d)(?=(\d{3})+$)/g, '$1,') + num.slice(-3)
  123. }
  124. return num.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
  125. }
  126. /**
  127. * 深拷贝
  128. * @param
  129. * @returns
  130. */
  131. export const deepClone = obj => {
  132. //判断拷贝的要进行深拷贝的是数组还是对象,是数组的话进行数组拷贝,对象的话进行对象拷贝
  133. var objClone = Array.isArray(obj) ? [] : {}
  134. //进行深拷贝的不能为空,并且是对象或者是
  135. if (obj && typeof obj === 'object') {
  136. for (let key in obj) {
  137. if (obj.hasOwnProperty(key)) {
  138. if (obj[key] && typeof obj[key] === 'object') {
  139. objClone[key] = deepClone(obj[key])
  140. } else {
  141. objClone[key] = obj[key]
  142. }
  143. }
  144. }
  145. }
  146. return objClone
  147. }
  148. /**
  149. *
  150. * @param {*} fn 是我们需要包装的事件回调
  151. * @param {*} delay 是每次推迟执行的等待时间
  152. */
  153. export const debounce = (fn, delay = 1000) => {
  154. // 定时器
  155. let timer = null
  156. // 将debounce处理结果当作函数返回
  157. return function () {
  158. // 保留调用时的this上下文
  159. let context = this
  160. // 保留调用时传入的参数
  161. let args = arguments
  162. // 每次事件被触发时,都去清除之前的旧定时器
  163. // console.log(timer)
  164. if (timer) {
  165. clearTimeout(timer)
  166. }
  167. // 设立新定时器
  168. timer = setTimeout(function () {
  169. fn.apply(context, args)
  170. }, delay)
  171. }
  172. }
  173. export default {
  174. successMsg,
  175. errorMsg,
  176. warningNotify,
  177. checkBtnRole,
  178. getSummaries,
  179. numToFixed,
  180. deepClone
  181. }