tools.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { arrIndex, clog } from 'js-fast-way'
  2. import config from '~src/config/index'
  3. import { useAppStore } from '~src/store'
  4. const store = useAppStore()
  5. //控制台打印
  6. export const HcLog = (name, tips, data) => {
  7. const title = store.barMenuName ?? ''
  8. if (config.isLog === 'auto') {
  9. if (import.meta.env.DEV) {
  10. clog(title, name, tips, data)
  11. }
  12. } else if (config.isLog === true) {
  13. clog(title, name, tips, data)
  14. }
  15. }
  16. //取数组中的值
  17. export const getRowsValue = (arr, key, key2, value) => {
  18. if (value) {
  19. const index = arrIndex(arr, key, value)
  20. return arr[index][key2]
  21. } else {
  22. return ''
  23. }
  24. }
  25. //获取ID,并转为数字
  26. export const rowsToIdNumArr = (rows) => {
  27. const ids = rowsToId(rows)
  28. let keys = ids ? ids.split(',') : []
  29. for (let i = 0; i < keys.length; i++) {
  30. keys[i] = Number(keys[i])
  31. }
  32. return keys
  33. }
  34. //拼接ID
  35. export const rowsToId = (rows) => {
  36. return rowsToKey(rows, 'id')
  37. }
  38. //拼接字段
  39. export const rowsToKey = (rows, key) => {
  40. return rows.map((obj) => {
  41. return obj[key]
  42. }).join(',')
  43. }
  44. //删除提醒
  45. export const delMessage = (cbk) => {
  46. window?.$messageBox?.alert('请谨慎考虑后,确认是否需要删除?', '删除提醒', {
  47. showCancelButton: true,
  48. confirmButtonText: '确认删除',
  49. cancelButtonText: '取消',
  50. type: 'warning',
  51. callback: (action) => {
  52. if (action === 'confirm') {
  53. cbk()
  54. }
  55. },
  56. })
  57. }
  58. //日期格式化
  59. export const dateFormat = (date, format) => {
  60. format = format || 'yyyy-MM-dd hh:mm:ss'
  61. if (date !== 'Invalid Date') {
  62. let o = {
  63. 'M+': date.getMonth() + 1, //month
  64. 'd+': date.getDate(), //day
  65. 'h+': date.getHours(), //hour
  66. 'm+': date.getMinutes(), //minute
  67. 's+': date.getSeconds(), //second
  68. 'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
  69. 'S': date.getMilliseconds(), //millisecond
  70. }
  71. if (/(y+)/.test(format)) {
  72. format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  73. }
  74. for (let k in o) {
  75. if (new RegExp('(' + k + ')').test(format)) {
  76. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  77. }
  78. }
  79. return format
  80. }
  81. return ''
  82. }
  83. //获取当前域名
  84. export const getTopUrl = () => {
  85. return window.location.href.split('/#/')[0]
  86. }
  87. //设置系统名称
  88. export const setAppName = (name) => {
  89. const title = window.document.title
  90. window.document.title = `${title} - ${name}`
  91. }
  92. //替换http为https
  93. export const setUrlHttps = (url) => {
  94. if (!url) return url
  95. return url.replace('http://', 'https://')
  96. }