index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Created by PanJiaChen on 16/11/18.
  3. */
  4. const CryptoJS = require('crypto-js'); // js
  5. /**
  6. * Parse the time to string
  7. * @param {(Object|string|number)} time
  8. * @param {string} cFormat
  9. * @returns {string | null}
  10. */
  11. export function parseTime(time, cFormat) {
  12. if (arguments.length === 0 || !time) {
  13. return null
  14. }
  15. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  16. let date
  17. if (typeof time === 'object') {
  18. date = time
  19. } else {
  20. if ((typeof time === 'string')) {
  21. if ((/^[0-9]+$/.test(time))) {
  22. // support "1548221490638"
  23. time = parseInt(time)
  24. } else {
  25. // support safari
  26. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  27. time = time.replace(new RegExp(/-/gm), '/')
  28. }
  29. }
  30. if ((typeof time === 'number') && (time.toString().length === 10)) {
  31. time = time * 1000
  32. }
  33. date = new Date(time)
  34. }
  35. const formatObj = {
  36. y: date.getFullYear(),
  37. m: date.getMonth() + 1,
  38. d: date.getDate(),
  39. h: date.getHours(),
  40. i: date.getMinutes(),
  41. s: date.getSeconds(),
  42. a: date.getDay()
  43. }
  44. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  45. const value = formatObj[key]
  46. // Note: getDay() returns 0 on Sunday
  47. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
  48. return value.toString().padStart(2, '0')
  49. })
  50. return time_str
  51. }
  52. /**
  53. * @param {number} time
  54. * @param {string} option
  55. * @returns {string}
  56. */
  57. export function formatTime(time, option) {
  58. if (('' + time).length === 10) {
  59. time = parseInt(time) * 1000
  60. } else {
  61. time = +time
  62. }
  63. const d = new Date(time)
  64. const now = Date.now()
  65. const diff = (now - d) / 1000
  66. if (diff < 30) {
  67. return '刚刚'
  68. } else if (diff < 3600) {
  69. // less 1 hour
  70. return Math.ceil(diff / 60) + '分钟前'
  71. } else if (diff < 3600 * 24) {
  72. return Math.ceil(diff / 3600) + '小时前'
  73. } else if (diff < 3600 * 24 * 2) {
  74. return '1天前'
  75. }
  76. if (option) {
  77. return parseTime(time, option)
  78. } else {
  79. return (
  80. d.getMonth() +
  81. 1 +
  82. '月' +
  83. d.getDate() +
  84. '日' +
  85. d.getHours() +
  86. '时' +
  87. d.getMinutes() +
  88. '分'
  89. )
  90. }
  91. }
  92. /**
  93. * @param {string} url
  94. * @returns {Object}
  95. */
  96. export function param2Obj(url) {
  97. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  98. if (!search) {
  99. return {}
  100. }
  101. const obj = {}
  102. const searchArr = search.split('&')
  103. searchArr.forEach(v => {
  104. const index = v.indexOf('=')
  105. if (index !== -1) {
  106. const name = v.substring(0, index)
  107. const val = v.substring(index + 1, v.length)
  108. obj[name] = val
  109. }
  110. })
  111. return obj
  112. }
  113. //获取当前时间
  114. export function getTime(num) {
  115. let s = new Date().getTime() + num * 1000 * 60 * 60 * 24
  116. let date = new Date(s)
  117. let year = date.getFullYear();
  118. let month = date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1);
  119. let day = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();
  120. let hour = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
  121. let minute = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
  122. let second = date.getSeconds() >= 10 ? date.getSeconds() : '0' + date.getSeconds();
  123. let time = `${year}-${month}-${day}`
  124. // let time = `${year}-${month}-${day} ${hour}:${minute}:${second}`
  125. return time;
  126. }
  127. //获取当前时间
  128. export function timeFormat(num) {
  129. let date = new Date(num)
  130. let year = date.getFullYear();
  131. let month = date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1);
  132. let day = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();
  133. let hour = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
  134. let minute = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
  135. let second = date.getSeconds() >= 10 ? date.getSeconds() : '0' + date.getSeconds();
  136. let time = `${year}-${month}-${day} ${hour}:${minute}:${second}`
  137. return time;
  138. }
  139. //防抖debounce代码:
  140. export function debounce(fn,delay=500) {
  141. var timeout = null; // 创建一个标记用来存放定时器的返回值
  142. return function (e) {
  143. // 每当用户输入的时候把前一个 setTimeout clear 掉
  144. clearTimeout(timeout);
  145. // 然后又创建一个新的 setTimeout, 这样就能保证interval 间隔内如果时间持续触发,就不会执行 fn 函数
  146. timeout = setTimeout(() => {
  147. fn.apply(this, arguments);
  148. }, delay);
  149. };
  150. }
  151. /**
  152. * aes加密
  153. * @param {*} word
  154. * @param {*} key
  155. * @returns
  156. */
  157. export function setAes(pas,k) {
  158. var b = CryptoJS.enc.Base64
  159. var key = CryptoJS.enc.Utf8.parse(k) //秘钥
  160. var srcs = CryptoJS.enc.Utf8.parse(pas)
  161. var encrypted = CryptoJS.AES.encrypt(srcs, key, {
  162. mode: CryptoJS.mode.ECB,
  163. padding: CryptoJS.pad.Pkcs7
  164. })
  165. return b.stringify(encrypted.ciphertext)
  166. }
  167. /**
  168. * 根据window.location判断是否是本地环境
  169. * @returns Boolean
  170. */
  171. export function isLocal() {
  172. // 获取当前 URL 的主机部分
  173. const hostname = new URL(window.location.href).hostname;
  174. // 匹配本地地址
  175. const cBlockRegex = /^(localhost|127.0.0.1|192\.168\.|10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)/;
  176. // 检查当前 URL 的主机部分是否匹配 C 段网络
  177. return cBlockRegex.test(hostname);
  178. }