index.js 13 KB

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