index.js 13 KB

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