history.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { TypeZ1dict } from '@/pages/Z_system/Z1dict/type'
  2. import { treeLastIdFindFatherFu } from '@/pages/Z_system/Z6user/data'
  3. import store from '@/store'
  4. import { createHashHistory } from 'history'
  5. import { TypeZ4Tree } from '@/pages/Z_system/Z4organization/type'
  6. import { baseURL } from './http'
  7. import { useLocation } from 'react-router-dom'
  8. import qs from 'query-string'
  9. // import dd from 'gdt-jsapi'
  10. const history = createHashHistory()
  11. // 监听路由
  12. let routerLength = 0
  13. history.listen((_: any, listener: any) => {
  14. if (listener === 'PUSH') {
  15. routerLength += 1
  16. } else if (listener === 'POP') {
  17. if (routerLength >= 1) routerLength -= 1
  18. }
  19. })
  20. // 详情页的返回按钮 如果没有上一级 才push
  21. export const backPageFu = (url: string) => {
  22. routerLength ? history.go(-1) : history.push(url)
  23. }
  24. export default history
  25. // 新窗口打开藏品详情页面
  26. export const openLink = (path: string) => {
  27. const urlAll = window.location.href
  28. const qian = urlAll.split('/#/')[0]
  29. window.open(`${qian}/#${path}`, '_blank')
  30. }
  31. // -------------------级联回显-------------------
  32. let dictAll: TypeZ1dict[] = []
  33. export const resJiLianFu = (idTemp: string, isNull?: string) => {
  34. if (idTemp) {
  35. dictAll = store.getState().Z1dict.dictAll
  36. const idArr = idTemp.split(',')
  37. const id = idArr[idArr.length - 1]
  38. let arr = treeLastIdFindFatherFu(dictAll, id, 'name')
  39. if (arr.length >= 3) arr = arr.slice(2)
  40. if (arr && arr.length) return arr.join(' / ')
  41. else return isNull || '-'
  42. } else return isNull || '-'
  43. }
  44. // -------------------富文本回显-------------------
  45. export const textFu = (val: string) => {
  46. let TxtRes = ''
  47. try {
  48. if (val) {
  49. let txt = JSON.parse(val)
  50. if (txt.txtArr && txt.txtArr.length) {
  51. let txt2: string = txt.txtArr[0].txt
  52. if (txt2) {
  53. const txt3 = txt2.replaceAll('<p></p>', '')
  54. if (txt3) TxtRes = txt2
  55. }
  56. }
  57. }
  58. } catch (error) {}
  59. return TxtRes
  60. }
  61. // -------------------树结构的搜索过滤-------------------
  62. export const filterTreeByName = (tree: TypeZ1dict[], searchTemp: string): TypeZ1dict[] => {
  63. const searchKey = searchTemp.toUpperCase()
  64. const dfs = (node: TypeZ1dict): TypeZ1dict | null => {
  65. // 先递归处理子节点(深度优先)
  66. const filteredChildren = (node.children?.map(dfs).filter(Boolean) as TypeZ1dict[]) || []
  67. // 判断当前节点是否匹配或子节点有匹配项
  68. const txt = node.name.toUpperCase() + (node.num || '').toUpperCase()
  69. const isSelfMatch = txt.includes(searchKey)
  70. // console.log('pppppppp', isSelfMatch, searchKey, node.num)
  71. const hasChildMatch = filteredChildren.length > 0
  72. if (isSelfMatch || hasChildMatch) {
  73. return {
  74. ...node,
  75. children: hasChildMatch ? filteredChildren : undefined
  76. }
  77. }
  78. return null
  79. }
  80. return tree.map(dfs).filter(Boolean) as TypeZ1dict[]
  81. }
  82. // -------------------处理所属部门数据-------------------
  83. let buMenTree: TypeZ4Tree[] = []
  84. export const buMenRes = (list: any[]) => {
  85. buMenTree = store.getState().Z4organization.treeData
  86. let arr = list.map((v: any) => ({
  87. ...v,
  88. deptNameRes: v.deptId
  89. ? treeLastIdFindFatherFu(buMenTree, v.deptId + '', 'name').join(' / ')
  90. : '-'
  91. }))
  92. return arr
  93. }
  94. // --------------生成A标签下载--------------------
  95. export const downFileFu = async (url: string, back?: () => void) => {
  96. try {
  97. const response = await fetch(baseURL + url)
  98. const blob = await response.blob()
  99. const blobUrl = URL.createObjectURL(blob)
  100. const link = document.createElement('a')
  101. link.href = blobUrl
  102. link.download = url.split('/').pop() || 'download'
  103. link.click()
  104. setTimeout(() => URL.revokeObjectURL(blobUrl), 100)
  105. if (back) back()
  106. } catch (error) {
  107. console.error('Download failed:', error)
  108. }
  109. }
  110. export const useQuery = () => {
  111. const { search } = useLocation()
  112. return qs.parse(search)
  113. }
  114. export const loginOutFu = () => {
  115. const urlAll = window.location.href
  116. const urlArr = urlAll.split('/#/')
  117. if (!urlAll.includes('/login')) {
  118. if (urlArr[1]) {
  119. history.push(`/login?back=${urlArr[1]}`)
  120. } else history.push('/login')
  121. }
  122. }
  123. // 简化版本:通过URL下载同域文件
  124. export const downloadFileByUrl = async (fileUrl: string, fileName?: string, back?: () => void) => {
  125. const a = document.createElement('a')
  126. a.href = fileUrl.startsWith('./') ? fileUrl : baseURL + fileUrl
  127. a.target = '_blank'
  128. // 设置下载文件名
  129. if (fileName) {
  130. a.download = fileName
  131. } else {
  132. // 自动从URL提取文件名
  133. const urlParts = fileUrl.split('/')
  134. const originalName = urlParts[urlParts.length - 1]
  135. a.download = originalName || 'download'
  136. }
  137. // 触发下载
  138. a.style.display = 'none'
  139. document.body.appendChild(a)
  140. a.click()
  141. document.body.removeChild(a)
  142. if (back) back()
  143. }