index.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * Created by jiachenpan on 16/11/18.
  3. */
  4. export function parseTime(time, cFormat) {
  5. if (arguments.length === 0) {
  6. return null
  7. }
  8. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  9. let date
  10. if (typeof time === 'object') {
  11. date = time
  12. } else {
  13. if (('' + time).length !== 13) time = parseInt(time) * 1000
  14. date = new Date(time)
  15. }
  16. const formatObj = {
  17. y: date.getFullYear(),
  18. m: date.getMonth() + 1,
  19. d: date.getDate(),
  20. h: date.getHours(),
  21. i: date.getMinutes(),
  22. s: date.getSeconds(),
  23. a: date.getDay()
  24. }
  25. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  26. let value = formatObj[key]
  27. if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
  28. if (result.length > 0 && value < 10) {
  29. value = '0' + value
  30. }
  31. return value || 0
  32. })
  33. return time_str
  34. }
  35. export function formatTime(time, option) {
  36. time = +time * 1000
  37. const d = new Date(time)
  38. const now = Date.now()
  39. const diff = (now - d) / 1000
  40. if (diff < 30) {
  41. return '刚刚'
  42. } else if (diff < 3600) { // less 1 hour
  43. return Math.ceil(diff / 60) + '分钟前'
  44. } else if (diff < 3600 * 24) {
  45. return Math.ceil(diff / 3600) + '小时前'
  46. } else if (diff < 3600 * 24 * 2) {
  47. return '1天前'
  48. }
  49. if (option) {
  50. return parseTime(time, option)
  51. } else {
  52. return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
  53. }
  54. }
  55. // 格式化时间
  56. export function getQueryObject(url) {
  57. url = url == null ? window.location.href : url
  58. const search = url.substring(url.lastIndexOf('?') + 1)
  59. const obj = {}
  60. const reg = /([^?&=]+)=([^?&=]*)/g
  61. search.replace(reg, (rs, $1, $2) => {
  62. const name = decodeURIComponent($1)
  63. let val = decodeURIComponent($2)
  64. val = String(val)
  65. obj[name] = val
  66. return rs
  67. })
  68. return obj
  69. }
  70. /**
  71. *get getByteLen
  72. * @param {Sting} val input value
  73. * @returns {number} output value
  74. */
  75. export function getByteLen(val) {
  76. let len = 0
  77. for (let i = 0; i < val.length; i++) {
  78. if (val[i].match(/[^\x00-\xff]/ig) != null) {
  79. len += 1
  80. } else { len += 0.5 }
  81. }
  82. return Math.floor(len)
  83. }
  84. export function cleanArray(actual) {
  85. const newArray = []
  86. for (let i = 0; i < actual.length; i++) {
  87. if (actual[i]) {
  88. newArray.push(actual[i])
  89. }
  90. }
  91. return newArray
  92. }
  93. export function param(json) {
  94. if (!json) return ''
  95. return cleanArray(Object.keys(json).map(key => {
  96. if (json[key] === undefined) return ''
  97. return encodeURIComponent(key) + '=' +
  98. encodeURIComponent(json[key])
  99. })).join('&')
  100. }
  101. export function param2Obj(url) {
  102. const search = url.split('?')[1]
  103. if (!search) {
  104. return {}
  105. }
  106. return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
  107. }
  108. export function html2Text(val) {
  109. const div = document.createElement('div')
  110. div.innerHTML = val
  111. return div.textContent || div.innerText
  112. }
  113. export function objectMerge(target, source) {
  114. /* Merges two objects,
  115. giving the last one precedence */
  116. if (typeof target !== 'object') {
  117. target = {}
  118. }
  119. if (Array.isArray(source)) {
  120. return source.slice()
  121. }
  122. Object.keys(source).forEach((property) => {
  123. const sourceProperty = source[property]
  124. if (typeof sourceProperty === 'object') {
  125. target[property] = objectMerge(target[property], sourceProperty)
  126. } else {
  127. target[property] = sourceProperty
  128. }
  129. })
  130. return target
  131. }
  132. export function scrollTo(element, to, duration) {
  133. if (duration <= 0) return
  134. const difference = to - element.scrollTop
  135. const perTick = difference / duration * 10
  136. setTimeout(() => {
  137. console.log(new Date())
  138. element.scrollTop = element.scrollTop + perTick
  139. if (element.scrollTop === to) return
  140. scrollTo(element, to, duration - 10)
  141. }, 10)
  142. }
  143. export function toggleClass(element, className) {
  144. if (!element || !className) {
  145. return
  146. }
  147. let classString = element.className
  148. const nameIndex = classString.indexOf(className)
  149. if (nameIndex === -1) {
  150. classString += '' + className
  151. } else {
  152. classString = classString.substr(0, nameIndex) + classString.substr(nameIndex + className.length)
  153. }
  154. element.className = classString
  155. }
  156. export const pickerOptions = [
  157. {
  158. text: '今天',
  159. onClick(picker) {
  160. const end = new Date()
  161. const start = new Date(new Date().toDateString())
  162. end.setTime(start.getTime())
  163. picker.$emit('pick', [start, end])
  164. }
  165. }, {
  166. text: '最近一周',
  167. onClick(picker) {
  168. const end = new Date(new Date().toDateString())
  169. const start = new Date()
  170. start.setTime(end.getTime() - 3600 * 1000 * 24 * 7)
  171. picker.$emit('pick', [start, end])
  172. }
  173. }, {
  174. text: '最近一个月',
  175. onClick(picker) {
  176. const end = new Date(new Date().toDateString())
  177. const start = new Date()
  178. start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
  179. picker.$emit('pick', [start, end])
  180. }
  181. }, {
  182. text: '最近三个月',
  183. onClick(picker) {
  184. const end = new Date(new Date().toDateString())
  185. const start = new Date()
  186. start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
  187. picker.$emit('pick', [start, end])
  188. }
  189. }]
  190. export function getTime(type) {
  191. if (type === 'start') {
  192. return new Date().getTime() - 3600 * 1000 * 24 * 90
  193. } else {
  194. return new Date(new Date().toDateString())
  195. }
  196. }
  197. export function debounce(func, wait, immediate) {
  198. let timeout, args, context, timestamp, result
  199. const later = function() {
  200. // 据上一次触发时间间隔
  201. const last = +new Date() - timestamp
  202. // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
  203. if (last < wait && last > 0) {
  204. timeout = setTimeout(later, wait - last)
  205. } else {
  206. timeout = null
  207. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  208. if (!immediate) {
  209. result = func.apply(context, args)
  210. if (!timeout) context = args = null
  211. }
  212. }
  213. }
  214. return function(...args) {
  215. context = this
  216. timestamp = +new Date()
  217. const callNow = immediate && !timeout
  218. // 如果延时不存在,重新设定延时
  219. if (!timeout) timeout = setTimeout(later, wait)
  220. if (callNow) {
  221. result = func.apply(context, args)
  222. context = args = null
  223. }
  224. return result
  225. }
  226. }
  227. /**
  228. * This is just a simple version of deep copy
  229. * Has a lot of edge cases bug
  230. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  231. */
  232. export function deepClone(source) {
  233. if (!source && typeof source !== 'object') {
  234. throw new Error('error arguments', 'shallowClone')
  235. }
  236. const targetObj = source.constructor === Array ? [] : {}
  237. Object.keys(source).forEach((keys) => {
  238. if (source[keys] && typeof source[keys] === 'object') {
  239. targetObj[keys] = deepClone(source[keys])
  240. } else {
  241. targetObj[keys] = source[keys]
  242. }
  243. })
  244. return targetObj
  245. }
  246. export function uniqueArr(arr) {
  247. return Array.from(new Set(arr))
  248. }