index.js 12 KB

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