index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  12. var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  13. var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  14. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  15. }
  16. /**
  17. * 判断该文件是否可以预览
  18. * @param {string} fileType
  19. */
  20. export const canPreviewFile = (fileType)=>{
  21. const array = ['.doc', '.docm', '.docx', '.dot', '.dotm', '.dotx', '.epub', '.fodt', '.htm', '.html', '.mht', '.odt', '.ott', '.pdf', '.rtf', '.txt', '.djvu', '.xps','csv', 'fods', 'ods', 'ots', 'xls', 'xlsm', 'xlsx', 'xlt', 'xltm', 'xltx','fodp', 'odp', 'otp', 'pot', 'potm', 'potx', 'pps', 'ppsm', 'ppsx', 'ppt', 'pptm', 'pptx']
  22. if(array.includes(fileType)){
  23. return true
  24. }
  25. return false
  26. }
  27. /**
  28. * 根据传入的文件类型返回图标
  29. * @param {string} fileType
  30. */
  31. export const setIcon = (fileType) => {
  32. switch (fileType) {
  33. case ".docx":
  34. return "src/assets/images/fileType/file_DOC.png";
  35. break;
  36. case ".pdf":
  37. return "src/assets/images/fileType/file_pdf.png";
  38. break;
  39. case ".ppt":
  40. return "src/assets/images/fileType/file_PPT.png";
  41. break;
  42. case ".txt":
  43. return "src/assets/images/fileType/file_TXT.png";
  44. break;
  45. case ".xlsx":
  46. return "src/assets/images/fileType/file_XLSX.png";
  47. break;
  48. case ".csv":
  49. return "src/assets/images/fileType/file_XLSX.png";
  50. break;
  51. case ".png":
  52. return "src/assets/images/fileType/file_pic.png";
  53. break;
  54. case ".mp3":
  55. return "src/assets/images/fileType/file_audio.png";
  56. break;
  57. case ".mp4":
  58. return "src/assets/images/fileType/file_video.png";
  59. break;
  60. case ".zip":
  61. return "src/assets/images/fileType/file_zip.png";
  62. break;
  63. default:
  64. return "src/assets/images/fileType/file_DOC.png";
  65. break;
  66. }
  67. };
  68. /**
  69. * @param {number} time
  70. * @param {string} option
  71. * @returns {string}
  72. */
  73. export function formatTime(time, option) {
  74. if (('' + time).length === 10) {
  75. time = parseInt(time) * 1000
  76. } else {
  77. time = +time
  78. }
  79. const d = new Date(time)
  80. const now = Date.now()
  81. const diff = (now - d) / 1000
  82. if (diff < 30) {
  83. return '刚刚'
  84. } else if (diff < 3600) {
  85. // less 1 hour
  86. return Math.ceil(diff / 60) + '分钟前'
  87. } else if (diff < 3600 * 24) {
  88. return Math.ceil(diff / 3600) + '小时前'
  89. } else if (diff < 3600 * 24 * 2) {
  90. return '1天前'
  91. }
  92. if (option) {
  93. return parseTime(time, option)
  94. } else {
  95. return (
  96. d.getMonth() +
  97. 1 +
  98. '月' +
  99. d.getDate() +
  100. '日' +
  101. d.getHours() +
  102. '时' +
  103. d.getMinutes() +
  104. '分'
  105. )
  106. }
  107. }
  108. /**
  109. * @param {string} url
  110. * @returns {Object}
  111. */
  112. export function getQueryObject(url) {
  113. url = url == null ? window.location.href : url
  114. const search = url.substring(url.lastIndexOf('?') + 1)
  115. const obj = {}
  116. const reg = /([^?&=]+)=([^?&=]*)/g
  117. search.replace(reg, (rs, $1, $2) => {
  118. const name = decodeURIComponent($1)
  119. let val = decodeURIComponent($2)
  120. val = String(val)
  121. obj[name] = val
  122. return rs
  123. })
  124. return obj
  125. }
  126. /**
  127. * @param {string} input value
  128. * @returns {number} output value
  129. */
  130. export function byteLength(str) {
  131. // returns the byte length of an utf8 string
  132. let s = str.length
  133. for (var i = str.length - 1; i >= 0; i--) {
  134. const code = str.charCodeAt(i)
  135. if (code > 0x7f && code <= 0x7ff) s++
  136. else if (code > 0x7ff && code <= 0xffff) s += 2
  137. if (code >= 0xDC00 && code <= 0xDFFF) i--
  138. }
  139. return s
  140. }
  141. /**
  142. * @param {Array} actual
  143. * @returns {Array}
  144. */
  145. export function cleanArray(actual) {
  146. const newArray = []
  147. for (let i = 0; i < actual.length; i++) {
  148. if (actual[i]) {
  149. newArray.push(actual[i])
  150. }
  151. }
  152. return newArray
  153. }
  154. /**
  155. * @param {Object} json
  156. * @returns {Array}
  157. */
  158. export function param(json) {
  159. if (!json) return ''
  160. return cleanArray(
  161. Object.keys(json).map(key => {
  162. if (json[key] === undefined) return ''
  163. return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
  164. })
  165. ).join('&')
  166. }
  167. /**
  168. * @param {string} url
  169. * @returns {Object}
  170. */
  171. export function param2Obj(url) {
  172. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  173. if (!search) {
  174. return {}
  175. }
  176. const obj = {}
  177. const searchArr = search.split('&')
  178. searchArr.forEach(v => {
  179. const index = v.indexOf('=')
  180. if (index !== -1) {
  181. const name = v.substring(0, index)
  182. const val = v.substring(index + 1, v.length)
  183. obj[name] = val
  184. }
  185. })
  186. return obj
  187. }
  188. /**
  189. * @param {string} val
  190. * @returns {string}
  191. */
  192. export function html2Text(val) {
  193. const div = document.createElement('div')
  194. div.innerHTML = val
  195. return div.textContent || div.innerText
  196. }
  197. /**
  198. * Merges two objects, giving the last one precedence
  199. * @param {Object} target
  200. * @param {(Object|Array)} source
  201. * @returns {Object}
  202. */
  203. export function objectMerge(target, source) {
  204. if (typeof target !== 'object') {
  205. target = {}
  206. }
  207. if (Array.isArray(source)) {
  208. return source.slice()
  209. }
  210. Object.keys(source).forEach(property => {
  211. const sourceProperty = source[property]
  212. if (typeof sourceProperty === 'object') {
  213. target[property] = objectMerge(target[property], sourceProperty)
  214. } else {
  215. target[property] = sourceProperty
  216. }
  217. })
  218. return target
  219. }
  220. /**
  221. * @param {HTMLElement} element
  222. * @param {string} className
  223. */
  224. export function toggleClass(element, className) {
  225. if (!element || !className) {
  226. return
  227. }
  228. let classString = element.className
  229. const nameIndex = classString.indexOf(className)
  230. if (nameIndex === -1) {
  231. classString += '' + className
  232. } else {
  233. classString =
  234. classString.substr(0, nameIndex) +
  235. classString.substr(nameIndex + className.length)
  236. }
  237. element.className = classString
  238. }
  239. /**
  240. * @param {string} type
  241. * @returns {Date}
  242. */
  243. export function getTime(type) {
  244. if (type === 'start') {
  245. return new Date().getTime() - 3600 * 1000 * 24 * 90
  246. } else {
  247. return new Date(new Date().toDateString())
  248. }
  249. }
  250. /**
  251. * @param {Function} func
  252. * @param {number} wait
  253. * @param {boolean} immediate
  254. * @return {*}
  255. */
  256. export function debounce(func, wait, immediate) {
  257. let timeout, args, context, timestamp, result
  258. const later = function() {
  259. // 据上一次触发时间间隔
  260. const last = +new Date() - timestamp
  261. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  262. if (last < wait && last > 0) {
  263. timeout = setTimeout(later, wait - last)
  264. } else {
  265. timeout = null
  266. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  267. if (!immediate) {
  268. result = func.apply(context, args)
  269. if (!timeout) context = args = null
  270. }
  271. }
  272. }
  273. return function(...args) {
  274. context = this
  275. timestamp = +new Date()
  276. const callNow = immediate && !timeout
  277. // 如果延时不存在,重新设定延时
  278. if (!timeout) timeout = setTimeout(later, wait)
  279. if (callNow) {
  280. result = func.apply(context, args)
  281. context = args = null
  282. }
  283. return result
  284. }
  285. }
  286. /**
  287. * This is just a simple version of deep copy
  288. * Has a lot of edge cases bug
  289. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  290. * @param {Object} source
  291. * @returns {Object}
  292. */
  293. export function deepClone(source) {
  294. if (!source && typeof source !== 'object') {
  295. throw new Error('error arguments', 'deepClone')
  296. }
  297. const targetObj = source.constructor === Array ? [] : {}
  298. Object.keys(source).forEach(keys => {
  299. if (source[keys] && typeof source[keys] === 'object') {
  300. targetObj[keys] = deepClone(source[keys])
  301. } else {
  302. targetObj[keys] = source[keys]
  303. }
  304. })
  305. return targetObj
  306. }
  307. /**
  308. * @param {Array} arr
  309. * @returns {Array}
  310. */
  311. export function uniqueArr(arr) {
  312. return Array.from(new Set(arr))
  313. }
  314. /**
  315. * @returns {string}
  316. */
  317. export function createUniqueString() {
  318. const timestamp = +new Date() + ''
  319. const randomNum = parseInt((1 + Math.random()) * 65536) + ''
  320. return (+(randomNum + timestamp)).toString(32)
  321. }
  322. /**
  323. * Check if an element has a class
  324. * @param {HTMLElement} elm
  325. * @param {string} cls
  326. * @returns {boolean}
  327. */
  328. export function hasClass(ele, cls) {
  329. return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
  330. }
  331. /**
  332. * Add class to element
  333. * @param {HTMLElement} elm
  334. * @param {string} cls
  335. */
  336. export function addClass(ele, cls) {
  337. if (!hasClass(ele, cls)) ele.className += ' ' + cls
  338. }
  339. /**
  340. * Remove class from element
  341. * @param {HTMLElement} elm
  342. * @param {string} cls
  343. */
  344. export function removeClass(ele, cls) {
  345. if (hasClass(ele, cls)) {
  346. const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
  347. ele.className = ele.className.replace(reg, ' ')
  348. }
  349. }
  350. export function makeMap(str, expectsLowerCase) {
  351. const map = Object.create(null)
  352. const list = str.split(',')
  353. for (let i = 0; i < list.length; i++) {
  354. map[list[i]] = true
  355. }
  356. return expectsLowerCase
  357. ? val => map[val.toLowerCase()]
  358. : val => map[val]
  359. }
  360. export const exportDefault = 'export default '
  361. export const beautifierConf = {
  362. html: {
  363. indent_size: '2',
  364. indent_char: ' ',
  365. max_preserve_newlines: '-1',
  366. preserve_newlines: false,
  367. keep_array_indentation: false,
  368. break_chained_methods: false,
  369. indent_scripts: 'separate',
  370. brace_style: 'end-expand',
  371. space_before_conditional: true,
  372. unescape_strings: false,
  373. jslint_happy: false,
  374. end_with_newline: true,
  375. wrap_line_length: '110',
  376. indent_inner_html: true,
  377. comma_first: false,
  378. e4x: true,
  379. indent_empty_lines: true
  380. },
  381. js: {
  382. indent_size: '2',
  383. indent_char: ' ',
  384. max_preserve_newlines: '-1',
  385. preserve_newlines: false,
  386. keep_array_indentation: false,
  387. break_chained_methods: false,
  388. indent_scripts: 'normal',
  389. brace_style: 'end-expand',
  390. space_before_conditional: true,
  391. unescape_strings: false,
  392. jslint_happy: true,
  393. end_with_newline: true,
  394. wrap_line_length: '110',
  395. indent_inner_html: true,
  396. comma_first: false,
  397. e4x: true,
  398. indent_empty_lines: true
  399. }
  400. }
  401. // 首字母大小
  402. export function titleCase(str) {
  403. return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
  404. }
  405. // 下划转驼峰
  406. export function camelCase(str) {
  407. return str.replace(/_[a-z]/g, str1 => str1.substr(-1).toUpperCase())
  408. }
  409. export function isNumberStr(str) {
  410. return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
  411. }