index.js 6.8 KB

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