common.js 5.4 KB

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