index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import { parseTime } from './ruoyi'
  2. /**
  3. * 表格时间格式化
  4. */
  5. export function formatDate(cellValue) {
  6. if (cellValue == null || cellValue == "") return "";
  7. var date = new Date(cellValue)
  8. var year = date.getFullYear()
  9. var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  10. var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  11. return year + '-' + month + '-' + day
  12. }
  13. export function formatDateTime(cellValue) {
  14. if (cellValue == null || cellValue == "") return "";
  15. var date = new Date(cellValue)
  16. var year = date.getFullYear()
  17. var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  18. var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  19. var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  20. var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  21. var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  22. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  23. }
  24. /**
  25. * @param {number} time
  26. * @param {string} option
  27. * @returns {string}
  28. */
  29. export function formatTime(time, option) {
  30. if (('' + time).length === 10) {
  31. time = parseInt(time) * 1000
  32. } else {
  33. time = +time
  34. }
  35. const d = new Date(time)
  36. const now = Date.now()
  37. const diff = (now - d) / 1000
  38. if (diff < 30) {
  39. return '刚刚'
  40. } else if (diff < 3600) {
  41. // less 1 hour
  42. return Math.ceil(diff / 60) + '分钟前'
  43. } else if (diff < 3600 * 24) {
  44. return Math.ceil(diff / 3600) + '小时前'
  45. } else if (diff < 3600 * 24 * 2) {
  46. return '1天前'
  47. }
  48. if (option) {
  49. return parseTime(time, option)
  50. } else {
  51. return (
  52. d.getMonth() +
  53. 1 +
  54. '月' +
  55. d.getDate() +
  56. '日' +
  57. d.getHours() +
  58. '时' +
  59. d.getMinutes() +
  60. '分'
  61. )
  62. }
  63. }
  64. /**
  65. * @param {string} url
  66. * @returns {Object}
  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. * @param {string} input value
  84. * @returns {number} output value
  85. */
  86. export function byteLength(str) {
  87. // returns the byte length of an utf8 string
  88. let s = str.length
  89. for (var i = str.length - 1; i >= 0; i--) {
  90. const code = str.charCodeAt(i)
  91. if (code > 0x7f && code <= 0x7ff) s++
  92. else if (code > 0x7ff && code <= 0xffff) s += 2
  93. if (code >= 0xDC00 && code <= 0xDFFF) i--
  94. }
  95. return s
  96. }
  97. /**
  98. * @param {Array} actual
  99. * @returns {Array}
  100. */
  101. export function cleanArray(actual) {
  102. const newArray = []
  103. for (let i = 0; i < actual.length; i++) {
  104. if (actual[i]) {
  105. newArray.push(actual[i])
  106. }
  107. }
  108. return newArray
  109. }
  110. /**
  111. * @param {Object} json
  112. * @returns {Array}
  113. */
  114. export function param(json) {
  115. if (!json) return ''
  116. return cleanArray(
  117. Object.keys(json).map(key => {
  118. if (json[key] === undefined) return ''
  119. return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
  120. })
  121. ).join('&')
  122. }
  123. /**
  124. * @param {string} url
  125. * @returns {Object}
  126. */
  127. export function param2Obj(url) {
  128. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  129. if (!search) {
  130. return {}
  131. }
  132. const obj = {}
  133. const searchArr = search.split('&')
  134. searchArr.forEach(v => {
  135. const index = v.indexOf('=')
  136. if (index !== -1) {
  137. const name = v.substring(0, index)
  138. const val = v.substring(index + 1, v.length)
  139. obj[name] = val
  140. }
  141. })
  142. return obj
  143. }
  144. /**
  145. * @param {string} val
  146. * @returns {string}
  147. */
  148. export function html2Text(val) {
  149. const div = document.createElement('div')
  150. div.innerHTML = val
  151. return div.textContent || div.innerText
  152. }
  153. /**
  154. * Merges two objects, giving the last one precedence
  155. * @param {Object} target
  156. * @param {(Object|Array)} source
  157. * @returns {Object}
  158. */
  159. export function objectMerge(target, source) {
  160. if (typeof target !== 'object') {
  161. target = {}
  162. }
  163. if (Array.isArray(source)) {
  164. return source.slice()
  165. }
  166. Object.keys(source).forEach(property => {
  167. const sourceProperty = source[property]
  168. if (typeof sourceProperty === 'object') {
  169. target[property] = objectMerge(target[property], sourceProperty)
  170. } else {
  171. target[property] = sourceProperty
  172. }
  173. })
  174. return target
  175. }
  176. /**
  177. * @param {HTMLElement} element
  178. * @param {string} className
  179. */
  180. export function toggleClass(element, className) {
  181. if (!element || !className) {
  182. return
  183. }
  184. let classString = element.className
  185. const nameIndex = classString.indexOf(className)
  186. if (nameIndex === -1) {
  187. classString += '' + className
  188. } else {
  189. classString =
  190. classString.substr(0, nameIndex) +
  191. classString.substr(nameIndex + className.length)
  192. }
  193. element.className = classString
  194. }
  195. /**
  196. * @param {string} type
  197. * @returns {Date}
  198. */
  199. export function getTime(type) {
  200. if (type === 'start') {
  201. return new Date().getTime() - 3600 * 1000 * 24 * 90
  202. } else {
  203. return new Date(new Date().toDateString())
  204. }
  205. }
  206. /**
  207. * @param {Function} func
  208. * @param {number} wait
  209. * @param {boolean} immediate
  210. * @return {*}
  211. */
  212. export function debounce(func, wait, immediate) {
  213. let timeout, args, context, timestamp, result
  214. const later = function() {
  215. // 据上一次触发时间间隔
  216. const last = +new Date() - timestamp
  217. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  218. if (last < wait && last > 0) {
  219. timeout = setTimeout(later, wait - last)
  220. } else {
  221. timeout = null
  222. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  223. if (!immediate) {
  224. result = func.apply(context, args)
  225. if (!timeout) context = args = null
  226. }
  227. }
  228. }
  229. return function(...args) {
  230. context = this
  231. timestamp = +new Date()
  232. const callNow = immediate && !timeout
  233. // 如果延时不存在,重新设定延时
  234. if (!timeout) timeout = setTimeout(later, wait)
  235. if (callNow) {
  236. result = func.apply(context, args)
  237. context = args = null
  238. }
  239. return result
  240. }
  241. }
  242. /**
  243. * This is just a simple version of deep copy
  244. * Has a lot of edge cases bug
  245. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  246. * @param {Object} source
  247. * @returns {Object}
  248. */
  249. export function deepClone(source) {
  250. if (!source && typeof source !== 'object') {
  251. throw new Error('error arguments', 'deepClone')
  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. /**
  264. * @param {Array} arr
  265. * @returns {Array}
  266. */
  267. export function uniqueArr(arr) {
  268. return Array.from(new Set(arr))
  269. }
  270. /**
  271. * @returns {string}
  272. */
  273. export function createUniqueString() {
  274. const timestamp = +new Date() + ''
  275. const randomNum = parseInt((1 + Math.random()) * 65536) + ''
  276. return (+(randomNum + timestamp)).toString(32)
  277. }
  278. /**
  279. * Check if an element has a class
  280. * @param {HTMLElement} elm
  281. * @param {string} cls
  282. * @returns {boolean}
  283. */
  284. export function hasClass(ele, cls) {
  285. return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
  286. }
  287. /**
  288. * Add class to element
  289. * @param {HTMLElement} elm
  290. * @param {string} cls
  291. */
  292. export function addClass(ele, cls) {
  293. if (!hasClass(ele, cls)) ele.className += ' ' + cls
  294. }
  295. /**
  296. * Remove class from element
  297. * @param {HTMLElement} elm
  298. * @param {string} cls
  299. */
  300. export function removeClass(ele, cls) {
  301. if (hasClass(ele, cls)) {
  302. const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
  303. ele.className = ele.className.replace(reg, ' ')
  304. }
  305. }
  306. export function makeMap(str, expectsLowerCase) {
  307. const map = Object.create(null)
  308. const list = str.split(',')
  309. for (let i = 0; i < list.length; i++) {
  310. map[list[i]] = true
  311. }
  312. return expectsLowerCase
  313. ? val => map[val.toLowerCase()]
  314. : val => map[val]
  315. }
  316. export const exportDefault = 'export default '
  317. export const beautifierConf = {
  318. html: {
  319. indent_size: '2',
  320. indent_char: ' ',
  321. max_preserve_newlines: '-1',
  322. preserve_newlines: false,
  323. keep_array_indentation: false,
  324. break_chained_methods: false,
  325. indent_scripts: 'separate',
  326. brace_style: 'end-expand',
  327. space_before_conditional: true,
  328. unescape_strings: false,
  329. jslint_happy: false,
  330. end_with_newline: true,
  331. wrap_line_length: '110',
  332. indent_inner_html: true,
  333. comma_first: false,
  334. e4x: true,
  335. indent_empty_lines: true
  336. },
  337. js: {
  338. indent_size: '2',
  339. indent_char: ' ',
  340. max_preserve_newlines: '-1',
  341. preserve_newlines: false,
  342. keep_array_indentation: false,
  343. break_chained_methods: false,
  344. indent_scripts: 'normal',
  345. brace_style: 'end-expand',
  346. space_before_conditional: true,
  347. unescape_strings: false,
  348. jslint_happy: true,
  349. end_with_newline: true,
  350. wrap_line_length: '110',
  351. indent_inner_html: true,
  352. comma_first: false,
  353. e4x: true,
  354. indent_empty_lines: true
  355. }
  356. }
  357. // 首字母大小
  358. export function titleCase(str) {
  359. return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
  360. }
  361. // 下划转驼峰
  362. export function camelCase(str) {
  363. return str.replace(/_[a-z]/g, str1 => str1.substr(-1).toUpperCase())
  364. }
  365. export function isNumberStr(str) {
  366. return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
  367. }