index.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import useCopy from "@/Hook/useCopy"
  2. import React, { useCallback, useEffect, useState } from "react"
  3. import { Carousel, Image, message, Pagination, Popconfirm, Spin } from 'antd'
  4. import style from './index.less'
  5. import FileModal from './components/fileModal'
  6. import { useModel } from "umi"
  7. import ImgModal from "./components/imgModal"
  8. import TreeBox from "./components/tree"
  9. import SortModal from "./components/fileModal/sortModal"
  10. interface News {
  11. id: number,
  12. sysMediaId: number,
  13. sortIndex: number,
  14. title: string,
  15. thumbUrl: string,
  16. author: string,
  17. digest: string,
  18. showCoverPic: boolean,
  19. content: string,//内容只在调取详情时获取
  20. contentSourceUrl: string,//文章地址
  21. needOpenComment: boolean,
  22. onlyFansCanComment: boolean
  23. }
  24. interface Item {
  25. id: number,
  26. folder: boolean,//是否是文件夹
  27. mediaType: 'VIDEO' | 'IMG' | 'PAGE',//类型
  28. number: string,//编号
  29. title: string,//名称
  30. url: string,//链接
  31. belongType: string,//所属类型
  32. createTime: string,//创建时间
  33. description: string,//k图文描述
  34. news?: News[]//图文内容
  35. knewsThumbUrl: string, // 图片链接
  36. knewsThumbId: number,//本地图片ID
  37. knewsThumbInfo: any,//k客服详情
  38. videoTitle: string,//视频标题
  39. width: number, // 宽
  40. height: number, // 高
  41. pageSpecsList?: any[],
  42. }
  43. interface Props {
  44. isAll?: boolean,//是否允许全选默认开启
  45. height?: any,//当使用为弹窗组件时设置高度以免太高
  46. noFile?: boolean,//是否禁止选择文件夹
  47. setPage?: (type: 0 | 1 | 2 | 3, data?: any) => void, // 0 创建 1 查看 2 复制
  48. isBack?: boolean, // 是否需要回填
  49. }
  50. function FlieBox(props: Props) {
  51. const { isAll = true, height, noFile = false, setPage, isBack = false } = props
  52. const { state, set, dels, list, get, onFile, allFile, delPupOn, delPupOff, changeClickFile, editFile, fileClick, treeClick, pathClick, getList, edit_media_folder, get_folder_tree } = useModel(isAll ? 'useLaunchAdq.useBdMedia' : 'useLaunchAdq.useBdMediaPup')
  53. const { fileVisible, belongUser, selectFile, selectItem, delPupId, xy, rightClickPup, path, publicPath, parentId, imgVisrible, mediaType, sortVisible } = state
  54. const { copy } = useCopy()
  55. const fileImg = require('../../../public/file.png')
  56. const [moveId, setMoveId] = useState<any>('')//移动的素材ID
  57. const [treeEl, item, folderId, setActionId, setHoverId] = TreeBox({ data: get_folder_tree.data, belongUser })
  58. const [listData, setListData] = useState<any>({})
  59. const [imgVisible, setImgVisible] = useState<boolean>(false)
  60. // 获取数据
  61. useEffect(() => {
  62. setListData(list?.data)
  63. }, [list, mediaType])
  64. /**复制编号 */
  65. const copyId = useCallback((e: React.MouseEvent<HTMLSpanElement, MouseEvent>, value: string) => {
  66. e.stopPropagation()//阻止冒泡传递到文件夹被点击事件
  67. copy(value)
  68. }, [])
  69. /**全局右键菜单 */
  70. const Menu = useCallback((props: { isItem?: boolean }) => {
  71. if (props.isItem && isAll) {
  72. return <ul style={{ top: xy?.y, left: xy?.x }} className={style.menu} >
  73. {
  74. isAll && <li onClick={allFile}>全选/反选</li>
  75. }
  76. {mediaType === 'PAGE' ? <>
  77. <li onClick={() => { setPage && setPage(1, rightClickPup.id) }}>查看</li>
  78. <li onClick={() => { setPage && setPage(2, rightClickPup.id) }}>复制</li>
  79. <li onClick={() => { setPage && setPage(3, rightClickPup.id) }}>批量复制</li>
  80. </> : <>
  81. <li onClick={() => { editFile() }}>编辑</li>
  82. </>}
  83. <li onClick={() => { set({ actionItem: rightClickPup, sortVisible: true }) }}>编辑排序</li>
  84. <li onClick={() => { set({ fileVisible: true }) }}>新建文件夹</li>
  85. <li onClick={() => { set({ imgVisrible: true }) }}>新建素材</li>
  86. <li onClick={(e) => { delPupOn(rightClickPup.id); onFile(e, rightClickPup, isAll, true) }} style={{ color: 'red' }}> 删除</li>
  87. {
  88. isAll && <li onClick={dels} style={{ color: 'red' }}>删除选中文件</li>
  89. }
  90. </ul>
  91. }
  92. return <ul style={{ top: xy?.y, left: xy?.x }} className={style.menu}>
  93. {
  94. isAll && <li onClick={allFile}>全选/反选</li>
  95. }
  96. {//防止K图文无限嵌套创建判断
  97. (isAll !== false) && <li onClick={() => { set({ fileVisible: true }) }}>新建文件夹</li> //: <li>此处无法新建操作</li>
  98. }
  99. {
  100. mediaType === 'PAGE' ? <li onClick={() => { setPage && setPage(0) }}>新建素材</li> : <li onClick={() => { set({ imgVisrible: true }) }}>新建素材</li>
  101. }
  102. {
  103. isAll && <li onClick={dels} style={{ color: 'red' }}>删除选中文件</li>
  104. }
  105. </ul>
  106. }, [xy, rightClickPup, allFile, mediaType])
  107. /**鼠标右键 */
  108. const rightMenu = useCallback((e: any, isItem?: any) => {
  109. e.stopPropagation()
  110. e.preventDefault()
  111. let x = e.clientX;
  112. let y = e.clientY;
  113. set({ xy: { x, y } })
  114. if (isItem) {
  115. set({ rightClickPup: isItem })
  116. } else {
  117. set({ rightClickPup: { id: 'all' } })
  118. }
  119. return false;
  120. }, [])
  121. /**左键点击页面隐藏菜单 */
  122. useEffect(() => {
  123. function fnc(e: any) {
  124. set({ rightClickPup: { id: '' } })
  125. }
  126. document.addEventListener('click', fnc)
  127. return () => {
  128. document.removeEventListener('click', fnc)
  129. }
  130. }, [])
  131. // /**切换个人|公共|父目录ID变化清空选中的文件*/
  132. useEffect(() => {
  133. if (!isBack) {
  134. set({ selectFile: [], selectItem: [] })
  135. }
  136. if (belongUser == '1' && path) {//处理切换个人|公共时保留路径层级的请求
  137. set({ parentId: path[path?.length - 1]?.id || null })
  138. }
  139. if (belongUser == '0' && publicPath) {
  140. set({ parentId: publicPath[publicPath?.length - 1]?.id || null })
  141. }
  142. }, [belongUser, parentId, path?.length, publicPath?.length])
  143. /**卸载组件处理 */
  144. useEffect(() => {
  145. return () => {
  146. set({ parentId: null })
  147. }
  148. }, [])
  149. /**获取目录树*/
  150. useEffect(() => {
  151. get_folder_tree.run({ belongUser, mediaType })
  152. }, [isAll, mediaType, belongUser])
  153. // 点击目录树进入对应目录
  154. useEffect(() => {
  155. if (item) {
  156. let { icon, children, ...arg } = item
  157. treeClick(arg)
  158. }
  159. }, [item])
  160. // 拖动事件配置
  161. const moveConfig = useCallback((item) => {
  162. return {
  163. draggable: true,
  164. onDragStart: (ev: any) => {
  165. let img = document.createElement('img')
  166. img.src = fileImg;
  167. ev.dataTransfer.setDragImage(img, 0, 0)
  168. setMoveId(item.id)
  169. },
  170. onDragEnd: (e: any) => {
  171. if (folderId !== '' && moveId !== '') {
  172. edit_media_folder.run({ sysMediaId: moveId, folderId, mediaType }).then(res => {
  173. message.success('操作成功')
  174. list.refresh()
  175. })
  176. }
  177. setActionId('')
  178. setHoverId('')
  179. setMoveId('')
  180. },
  181. }
  182. }, [folderId, moveId, mediaType])
  183. return <div style={{ display: 'flex', flexFlow: 'row' }}>
  184. {get_folder_tree?.data?.length > 0 && <div style={{ flexShrink: 0 }}>
  185. {treeEl}
  186. </div>}
  187. <div style={{ flexShrink: 1 }}>
  188. <div className={style.files} onContextMenu={rightMenu} style={height ? { height } : {}}>
  189. {/* 关联公众号筛选 */}
  190. <div className={style.wxSelect}>
  191. </div>
  192. {/* 层级路径 */}
  193. <div className={style.path} >
  194. {//存在渲染个人本地路径不存在执行公共本地
  195. belongUser == '1' && path?.map((item: { title: string }, index: number) => {
  196. return <span key={index} onClick={() => { pathClick({ ...item, index }) }} >
  197. <span className={path?.length !== index + 1 ? style.path_color : ''}>{item?.title}</span>
  198. {path?.length !== index + 1 && <span className={style.rt} >{'>'}</span>}
  199. </span>
  200. })
  201. }
  202. {
  203. belongUser == '0' && publicPath?.map((item: { title: string }, index: number) => {
  204. return <span key={index} onClick={() => { pathClick({ ...item, index }) }} >
  205. <span className={publicPath?.length !== index + 1 ? style.path_color : ''}>{item?.title}</span>
  206. {publicPath?.length !== index + 1 && <span className={style.rt} >{'>'}</span>}
  207. </span>
  208. })
  209. }
  210. </div>
  211. {/* 内容 */}
  212. <div className={style.file_content} >
  213. {
  214. listData?.records?.map((item: Item) => {
  215. if (item.folder) {
  216. {/* 文件夹模板 */ }
  217. return <Popconfirm
  218. title="确定要删除吗?"
  219. onConfirm={() => { dels(item) }}
  220. okText="是"
  221. cancelText="否"
  222. onCancel={delPupOff}
  223. visible={delPupId === item.id}
  224. key={item.id}
  225. >
  226. <Spin tip='正在请求素材详情,请耐心等待...' spinning={get?.loading}>
  227. <div
  228. className={`${style.flex_box} ${selectFile?.some((id: number) => id === item.id) ? style.action : ''}`}
  229. onContextMenu={(e) => { rightMenu(e, item) }}
  230. onClick={(e) => { changeClickFile(e, item, isAll, noFile) }}
  231. onDragStart={(e: any) => {
  232. e.preventDefault()
  233. }}
  234. >
  235. {/* {isAll && <span className={`${style.select}`} onClick={(e) => { changeClickFile(e, item, isAll, noFile) }}>
  236. <span role="img" aria-label="check" style={{ color: '#fff' }}><svg viewBox="64 64 896 896" focusable="false" data-icon="check" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5 207 474a32 32 0 00-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8z"></path></svg></span>
  237. </span>} */}
  238. <img src={fileImg}
  239. className={style.flex_box_img}
  240. onClick={(e: any) => { e.stopPropagation(); fileClick(item) }}
  241. onDragOver={(ev) => {
  242. ev.preventDefault()
  243. }}
  244. onDrop={() => {
  245. if (item.id !== folderId) {
  246. setActionId(item.id)
  247. }
  248. }}
  249. />
  250. <span className={style.flex_box_name} >{item?.title}</span>
  251. <span className={style.flex_box_id} onClick={(e) => { copyId(e, item?.number) }} >{item?.number}</span>
  252. </div>
  253. </Spin>
  254. </Popconfirm>
  255. } else {
  256. {/* 图片模板 ,视频模板,音频模板*/ }
  257. let topPageElements: any
  258. let topName: string = ""
  259. if (mediaType === 'PAGE' && item?.pageSpecsList) {
  260. topPageElements = item?.pageSpecsList[0]?.pageElementsSpecList[0]
  261. }
  262. let El = null
  263. if (mediaType === 'IMG') {
  264. El = <Image src={item.url} onClick={(e) => { e.stopPropagation() }} />
  265. } else if (mediaType === 'VIDEO') {
  266. El = <video src={item.url} style={{ width: 130, height: 100 }} controls />
  267. } else if (mediaType === 'PAGE') {
  268. switch (topPageElements?.elementType) {
  269. case 'TOP_IMAGE':
  270. topName = "顶部图片"
  271. El = <Image src={topPageElements?.topImageSpec?.imageUrl} onClick={(e) => { e.stopPropagation() }} />
  272. break
  273. case 'TOP_SLIDER':
  274. topName = "顶部轮播图"
  275. El = <>
  276. <Carousel autoplay style={{ width: 150, textAlign: 'center' }}>
  277. {topPageElements?.topSliderSpec?.imageUrlList?.map((item: string, index: number) => <div key={index}>
  278. <Image preview={{ visible: false }} src={item} onClick={(e) => { e.stopPropagation(); setImgVisible(true) }} />
  279. </div>)}
  280. </Carousel>
  281. <div style={{ display: 'none' }}>
  282. <Image.PreviewGroup preview={{ visible: imgVisible, onVisibleChange: vis => setImgVisible(vis) }}>
  283. {topPageElements?.topSliderSpec?.imageUrlList?.map((item: string, index: number) => <Image src={item} key={index} />)}
  284. </Image.PreviewGroup>
  285. </div>
  286. </>
  287. break
  288. case 'TOP_VIDEO':
  289. topName = "顶部视频"
  290. El = <video src={topPageElements?.topVideoSpec?.videoUrl} style={{ width: 130, height: 100 }} controls />
  291. break
  292. }
  293. }
  294. return <Popconfirm
  295. title="确定要删除吗?"
  296. onConfirm={() => { dels(item.id) }}
  297. okText="是"
  298. cancelText="否"
  299. onCancel={delPupOff}
  300. visible={delPupId === item.id}
  301. key={item.id}
  302. >
  303. <Spin tip='正在请求素材详情,请耐心等待...' spinning={get?.loading}>
  304. <div
  305. className={`${style.image_box} ${!isBack ? (selectFile?.some((id: number) => id === item.id) ? style.action : '') : (selectItem?.some((item1: { url: string }) => item1.url === item.url) ? style.action : '')}`}
  306. onContextMenu={(e) => { rightMenu(e, item) }}
  307. onClick={(e) => { changeClickFile(e, item, isAll) }}
  308. {...moveConfig(item)}
  309. >
  310. <span className={`${style.select}`} onClick={(e) => { changeClickFile(e, item, isAll) }}>
  311. <span role="img" aria-label="check" style={{ color: '#fff' }}><svg viewBox="64 64 896 896" focusable="false" data-icon="check" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5 207 474a32 32 0 00-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8z"></path></svg></span>
  312. </span>
  313. {El}
  314. <span className={style.flex_box_name} onClick={(e) => { copyId(e, item?.videoTitle || item?.title) }}>{item?.videoTitle || item?.title}</span>
  315. {mediaType === 'PAGE' ? <span>{topName}</span> : <span>{item?.width}*{item.height}</span>}
  316. </div>
  317. </Spin>
  318. </Popconfirm>
  319. }
  320. })
  321. }
  322. </div>
  323. {/* 鼠标右键菜单 */}
  324. {rightClickPup.id ? rightClickPup.id === 'all' ? <Menu /> : <Menu isItem /> : null}
  325. {/* 新建文件弹窗 */}
  326. {fileVisible && <FileModal isAll={isAll} />}
  327. {/* 编辑排序 */}
  328. {sortVisible && <SortModal isAll={isAll} />}
  329. {/* 新建非图文 */}
  330. {imgVisrible && <ImgModal isAll={isAll} />}
  331. </div>
  332. {
  333. listData?.records?.length > 0 && <div className={style.pagination}>
  334. {/* 分页 */}
  335. <Pagination
  336. showSizeChanger
  337. onChange={(page: number, pageSize?: number) => {
  338. getList({ pageSize, pageNum: page })
  339. }}
  340. onShowSizeChange={(current: number, size: number) => {
  341. getList({ pageSize: size, pageNum: current })
  342. }}
  343. current={listData?.current}
  344. defaultPageSize={20}
  345. total={listData?.total}
  346. />
  347. </div>
  348. }
  349. </div>
  350. </div>
  351. }
  352. export default React.memo(FlieBox)