common.js 5.5 KB

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