index.js 15 KB

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