| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import { TypeZ1dict } from '@/pages/Z_system/Z1dict/type'
- import { treeLastIdFindFatherFu } from '@/pages/Z_system/Z6user/data'
- import store from '@/store'
- import { createHashHistory } from 'history'
- import { TypeZ4Tree } from '@/pages/Z_system/Z4organization/type'
- import { baseURL } from './http'
- import { useLocation } from 'react-router-dom'
- import qs from 'query-string'
- // import dd from 'gdt-jsapi'
- const history = createHashHistory()
- // 监听路由
- let routerLength = 0
- history.listen((_: any, listener: any) => {
- if (listener === 'PUSH') {
- routerLength += 1
- } else if (listener === 'POP') {
- if (routerLength >= 1) routerLength -= 1
- }
- })
- // 详情页的返回按钮 如果没有上一级 才push
- export const backPageFu = (url: string) => {
- routerLength ? history.go(-1) : history.push(url)
- }
- export default history
- // 新窗口打开藏品详情页面
- export const openLink = (path: string) => {
- const urlAll = window.location.href
- const qian = urlAll.split('/#/')[0]
- window.open(`${qian}/#${path}`, '_blank')
- }
- // -------------------级联回显-------------------
- let dictAll: TypeZ1dict[] = []
- export const resJiLianFu = (idTemp: string, isNull?: string) => {
- if (idTemp) {
- dictAll = store.getState().Z1dict.dictAll
- const idArr = idTemp.split(',')
- const id = idArr[idArr.length - 1]
- let arr = treeLastIdFindFatherFu(dictAll, id, 'name')
- if (arr.length >= 3) arr = arr.slice(2)
- if (arr && arr.length) return arr.join(' / ')
- else return isNull || '-'
- } else return isNull || '-'
- }
- // -------------------富文本回显-------------------
- export const textFu = (val: string) => {
- let TxtRes = ''
- try {
- if (val) {
- let txt = JSON.parse(val)
- if (txt.txtArr && txt.txtArr.length) {
- let txt2: string = txt.txtArr[0].txt
- if (txt2) {
- const txt3 = txt2.replaceAll('<p></p>', '')
- if (txt3) TxtRes = txt2
- }
- }
- }
- } catch (error) {}
- return TxtRes
- }
- // -------------------树结构的搜索过滤-------------------
- export const filterTreeByName = (tree: TypeZ1dict[], searchTemp: string): TypeZ1dict[] => {
- const searchKey = searchTemp.toUpperCase()
- const dfs = (node: TypeZ1dict): TypeZ1dict | null => {
- // 先递归处理子节点(深度优先)
- const filteredChildren = (node.children?.map(dfs).filter(Boolean) as TypeZ1dict[]) || []
- // 判断当前节点是否匹配或子节点有匹配项
- const txt = node.name.toUpperCase() + (node.num || '').toUpperCase()
- const isSelfMatch = txt.includes(searchKey)
- // console.log('pppppppp', isSelfMatch, searchKey, node.num)
- const hasChildMatch = filteredChildren.length > 0
- if (isSelfMatch || hasChildMatch) {
- return {
- ...node,
- children: hasChildMatch ? filteredChildren : undefined
- }
- }
- return null
- }
- return tree.map(dfs).filter(Boolean) as TypeZ1dict[]
- }
- // -------------------处理所属部门数据-------------------
- let buMenTree: TypeZ4Tree[] = []
- export const buMenRes = (list: any[]) => {
- buMenTree = store.getState().Z4organization.treeData
- let arr = list.map((v: any) => ({
- ...v,
- deptNameRes: v.deptId
- ? treeLastIdFindFatherFu(buMenTree, v.deptId + '', 'name').join(' / ')
- : '-'
- }))
- return arr
- }
- // --------------生成A标签下载--------------------
- export const downFileFu = async (url: string, back?: () => void) => {
- try {
- const response = await fetch(baseURL + url)
- const blob = await response.blob()
- const blobUrl = URL.createObjectURL(blob)
- const link = document.createElement('a')
- link.href = blobUrl
- link.download = url.split('/').pop() || 'download'
- link.click()
- setTimeout(() => URL.revokeObjectURL(blobUrl), 100)
- if (back) back()
- } catch (error) {
- console.error('Download failed:', error)
- }
- }
- export const useQuery = () => {
- const { search } = useLocation()
- return qs.parse(search)
- }
- export const loginOutFu = () => {
- const urlAll = window.location.href
- const urlArr = urlAll.split('/#/')
- if (!urlAll.includes('/login')) {
- if (urlArr[1]) {
- history.push(`/login?back=${urlArr[1]}`)
- } else history.push('/login')
- }
- }
- // 简化版本:通过URL下载同域文件
- export const downloadFileByUrl = async (fileUrl: string, fileName?: string, back?: () => void) => {
- const a = document.createElement('a')
- a.href = fileUrl.startsWith('./') ? fileUrl : baseURL + fileUrl
- a.target = '_blank'
- // 设置下载文件名
- if (fileName) {
- a.download = fileName
- } else {
- // 自动从URL提取文件名
- const urlParts = fileUrl.split('/')
- const originalName = urlParts[urlParts.length - 1]
- a.download = originalName || 'download'
- }
- // 触发下载
- a.style.display = 'none'
- document.body.appendChild(a)
- a.click()
- document.body.removeChild(a)
- if (back) back()
- }
|