index.js 11 KB

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