utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // @ts-nocheck
  2. /**
  3. * 深拷贝内容
  4. * @param originData 拷贝对象
  5. * @author crlang(https://crlang.com)
  6. */
  7. export function deepClone(originData) {
  8. const type = Object.prototype.toString.call(originData)
  9. let data
  10. if (type === '[object Array]') {
  11. data = []
  12. for (let i = 0; i < originData.length; i++) {
  13. data.push(deepClone(originData[i]))
  14. }
  15. } else if (type === '[object Object]') {
  16. data = {}
  17. for (const prop in originData) {
  18. // eslint-disable-next-line no-prototype-builtins
  19. if (originData.hasOwnProperty(prop)) { // 非继承属性
  20. data[prop] = deepClone(originData[prop])
  21. }
  22. }
  23. } else {
  24. data = originData
  25. }
  26. return data
  27. }
  28. export function getValueByKey(object, path, defaultVal = undefined) {
  29. console.log('object, path', object, path)
  30. // 先将path处理成统一格式
  31. let newPath = []
  32. if (Array.isArray(path)) {
  33. newPath = path
  34. } else {
  35. // 先将字符串中的'['、']'去除替换为'.',split分割成数组形式
  36. newPath = path.replace(/\[/g, '.').replace(/\]/g, '').split('.')
  37. }
  38. // 递归处理,返回最后结果
  39. return newPath.reduce((o, k) => {
  40. console.log(o, k) // 此处o初始值为下边传入的 object,后续值为每次取的内部值
  41. return (o || {})[k]
  42. }, object) || defaultVal
  43. }
  44. /**
  45. * 处理部分初始数据
  46. * @param data
  47. */
  48. export function checkDataField(options, fields) {
  49. if (!fields || !options || options.length === 0) {
  50. return options
  51. }
  52. for (let i = 0; i < options.length; i++) {
  53. const k = options[i]
  54. k.label = k[fields.label || 'label'] || null
  55. k.value = k[fields.value || 'value'] || null
  56. k.suffix = k[fields.suffix || 'suffix'] || null
  57. k.children = k[fields.children || 'children'] || null
  58. if (k.children?.length) {
  59. k.options = checkDataField(k.options)
  60. }
  61. }
  62. return options
  63. }
  64. /**
  65. * 格式化数值-个位数补零
  66. * @param n 数值
  67. * @author crlang(https://crlang.com)
  68. */
  69. export function formatNumber(n) {
  70. let s = parseInt(n)
  71. if (isNaN(s)) {
  72. s = '0'
  73. } else {
  74. s = s.toString()
  75. }
  76. return s[1] ? s : `0${s}`
  77. }
  78. /**
  79. * 格式化时间
  80. * @param date 时间对象
  81. * @param format 格式
  82. * @author crlang(https://crlang.com)
  83. */
  84. export function formatTime(date, format) {
  85. const daDate = new Date(date.toString().length < 11 ? date * 1000 : date)
  86. const fromatsRule = ['y', 'm', 'd', 'h', 'i', 's']
  87. let tmp = []
  88. const year = daDate.getFullYear()
  89. const month = daDate.getMonth() + 1
  90. const day = daDate.getDate()
  91. const hour = daDate.getHours()
  92. const minute = daDate.getMinutes()
  93. const second = daDate.getSeconds()
  94. if (format) {
  95. tmp.push(year, month, day, hour, minute, second)
  96. tmp = tmp.map(formatNumber)
  97. for (let i = 0; i < tmp.length; i++) {
  98. format = format.toLowerCase().replace(fromatsRule[i], tmp[i])
  99. }
  100. return format
  101. }
  102. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  103. }
  104. /**
  105. * 获取某个时间范围
  106. *
  107. * @param v -1、-7、-14、-30、-60
  108. * @returns object {start: y-m-d,end: y-m-d}
  109. * @author crlang(https://crlang.com)
  110. */
  111. export function getRangeDate(v) {
  112. const now = new Date()
  113. const nowTime = now.getTime()
  114. const oneDay = 24 * 60 * 60 * 1000
  115. const dateRange = { start: '', end: '' }
  116. const nowWeekDay = now.getDay() // 今天本周的第几天
  117. const nowDay = now.getDate() // 当前日
  118. const nowMonth = now.getMonth() // 当前月
  119. const nowYear = now.getFullYear() // 当前年
  120. /**
  121. * 获得某个月的天数
  122. * @param month 当前月份
  123. */
  124. const getMonthDays = function(month) {
  125. const monthStartDate = new Date(nowYear, month, 1)
  126. const monthEndDate = new Date(nowYear, month + 1, 1)
  127. const days = (monthEndDate - monthStartDate) / oneDay
  128. return days
  129. }
  130. // 昨日
  131. if (v === '-1') {
  132. dateRange.start = formatTime(new Date(nowTime - oneDay), 'y-m-d')
  133. dateRange.end = dateRange.start
  134. // 本周
  135. } else if (v === '-7') {
  136. const weekStart = new Date(nowYear, nowMonth, nowDay - nowWeekDay + 1)
  137. const weekEnd = new Date(nowTime + oneDay) // 今日
  138. dateRange.start = formatTime(weekStart, 'y-m-d')
  139. dateRange.end = formatTime(weekEnd, 'y-m-d')
  140. // 上周
  141. } else if (v === '-14') {
  142. const weekStart = new Date(nowYear, nowMonth, nowDay - nowWeekDay - 6)
  143. const weekEnd = new Date(nowYear, nowMonth, nowDay - nowWeekDay)
  144. dateRange.start = formatTime(weekStart, 'y-m-d')
  145. dateRange.end = formatTime(weekEnd, 'y-m-d')
  146. // 本月
  147. } else if (v === '-30') {
  148. const monthStart = new Date(nowYear, nowMonth, 1)
  149. const monthEnd = new Date(nowTime + oneDay)
  150. dateRange.start = formatTime(monthStart, 'y-m-d')
  151. dateRange.end = formatTime(monthEnd, 'y-m-d')
  152. // 上月
  153. } else if (v === '-60') {
  154. const lastMonthDate = new Date() // 上月日期
  155. lastMonthDate.setDate(1)
  156. lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
  157. const lastMonth = lastMonthDate.getMonth()
  158. const lastMonthStart = new Date(nowMonth === 0 ? nowYear - 1 : nowYear, lastMonth, 1)
  159. const lastMonthEnd = new Date(nowMonth === 0 ? nowYear - 1 : nowYear, lastMonth, getMonthDays(lastMonth))
  160. dateRange.start = formatTime(lastMonthStart, 'y-m-d')
  161. dateRange.end = formatTime(lastMonthEnd, 'y-m-d')
  162. } else {
  163. // 传入 v 为整数是即为近 xx 天
  164. if (v > 0) {
  165. dateRange.start = formatTime(new Date(nowTime - oneDay * parseInt(v)), 'y-m-d')
  166. dateRange.end = formatTime(new Date(nowTime - oneDay), 'y-m-d') // 不含今天
  167. }
  168. }
  169. return dateRange
  170. }
  171. export const menuInitOpts = {
  172. cell: {
  173. showArrow: true,
  174. },
  175. click: {
  176. },
  177. sort: {
  178. showSort: true,
  179. },
  180. filter: {
  181. showArrow: true,
  182. },
  183. picker: {
  184. showArrow: true,
  185. },
  186. daterange: {
  187. showQuick: true,
  188. showArrow: true,
  189. },
  190. slot: {
  191. showArrow: true,
  192. },
  193. search: {
  194. showSearch: true,
  195. },
  196. }