|
@@ -0,0 +1,430 @@
|
|
|
+import getMD5 from '@/components/MD5';
|
|
|
+import { useAjax } from '@/Hook/useAjax';
|
|
|
+import { bdSysMediaList, bdSysMediaAdd, delMedia, bdSysMediaEdit, getFileUrl, syncMedia, getMedia, configSortApi, syncForSend, wxSysMediaAdd, editWxlist, getFolderTree, editMediaFolder } from '@/services/launchAdq/material';
|
|
|
+import { message } from 'antd';
|
|
|
+import { Dispatch, useCallback, useReducer, useState } from 'react'
|
|
|
+import { request } from 'umi';
|
|
|
+
|
|
|
+type State = {
|
|
|
+ fileVisible?: boolean,
|
|
|
+ imgVisrible?: boolean,
|
|
|
+ newsVisrible?: boolean,
|
|
|
+ knewsVisrible?: boolean,
|
|
|
+ fileName?: string,
|
|
|
+ sort?: number,
|
|
|
+ videoTitle?: string,
|
|
|
+ videoDescription?: string,
|
|
|
+ belongUser?: any,
|
|
|
+ fileType?: 'image' | 'news' | 'voice' | 'video' | 'knews',
|
|
|
+ parentId?: any,
|
|
|
+ url?: string,
|
|
|
+ pathId?: string,
|
|
|
+ selectFile?: number[],
|
|
|
+ rightClickPup?: any,
|
|
|
+ xy?: { x: number, y: number },
|
|
|
+ delPupId?: any,
|
|
|
+ actionItem?: any,
|
|
|
+ path?: any[],
|
|
|
+ publicPath?: any[],
|
|
|
+ file?: File,
|
|
|
+ selectItem?: any,
|
|
|
+ knewsdefaultData?: any,
|
|
|
+ sortVisible?:boolean,
|
|
|
+}
|
|
|
+export type Action = {
|
|
|
+ type: 'set' | 'init',
|
|
|
+ params?: any
|
|
|
+}
|
|
|
+const typeEnum = {
|
|
|
+ 'image': '图片',
|
|
|
+ 'news': '图文素材',
|
|
|
+ 'voice': '音频',
|
|
|
+ 'video': '视频',
|
|
|
+ 'knews': 'K-图文'
|
|
|
+}
|
|
|
+function reducer(state: State, action: Action) {
|
|
|
+ let { type, params } = action
|
|
|
+ let newState = JSON.parse(JSON.stringify(state))
|
|
|
+ newState.file = state.file
|
|
|
+ switch (type) {
|
|
|
+ case 'set':
|
|
|
+ Object.keys(params as State).forEach((key: string) => {
|
|
|
+ newState[key] = (params as State)[key]
|
|
|
+ })
|
|
|
+ return newState
|
|
|
+ case 'init':
|
|
|
+ return { ...initData, ...params }
|
|
|
+ default:
|
|
|
+ return state;
|
|
|
+ }
|
|
|
+}
|
|
|
+const initData: State = {
|
|
|
+ fileVisible: false,
|
|
|
+ knewsVisrible: false,
|
|
|
+ videoTitle: '',
|
|
|
+ videoDescription: '',
|
|
|
+ fileName: '',
|
|
|
+ belongUser: '1',
|
|
|
+ fileType: 'image',
|
|
|
+ parentId: null,
|
|
|
+ selectFile: [],
|
|
|
+ delPupId: '',
|
|
|
+ rightClickPup: { id: '' },
|
|
|
+ publicPath: [{ title: '公共本地', number: '0' }],
|
|
|
+ path: [{ title: '个人本地', number: '0' }],
|
|
|
+ imgVisrible: false,
|
|
|
+ newsVisrible: false,
|
|
|
+ sortVisible:false,
|
|
|
+ sort: 0
|
|
|
+}
|
|
|
+
|
|
|
+function useBdMediaPup() {
|
|
|
+ const [state, dispatch]: [State, Dispatch<Action>] = useReducer(reducer, initData)
|
|
|
+ const { fileName, sort, belongUser, fileType, parentId, selectFile, delPupId, rightClickPup, actionItem, path, publicPath, file, videoTitle, videoDescription } = state
|
|
|
+ const list = useAjax((params) => bdSysMediaList(params))
|
|
|
+ const add = useAjax((params) => bdSysMediaAdd(params), { msgNmae: '新增' })
|
|
|
+ const addWx = useAjax((params) => wxSysMediaAdd(params), { msgNmae: '新增' })
|
|
|
+ const del = useAjax((params) => delMedia(params), { msgNmae: '删除' })
|
|
|
+ const edit = useAjax((params) => bdSysMediaEdit(params), { msgNmae: '编辑' })
|
|
|
+ const sync = useAjax((params) => syncMedia(params), { msgNmae: '同步' })
|
|
|
+ const syncId = useAjax((params) => syncForSend(params), { msgNmae: '同步' })
|
|
|
+ const configSort = useAjax((params) => configSortApi(params), { msgNmae: '排序' })
|
|
|
+ const get = useAjax((params) => getMedia(params))
|
|
|
+ const get_folder_tree = useAjax((params: any) => getFolderTree(params))
|
|
|
+ const edit_media_folder = useAjax((params: any) => editMediaFolder(params))
|
|
|
+ const [isOk, setIsOk] = useState<boolean>(true)
|
|
|
+ const eidtWxlists = useAjax((params) => editWxlist(params))
|
|
|
+
|
|
|
+ const fileUrl = useAjax((params: { type: string }) => getFileUrl(params), {
|
|
|
+ manual: true,
|
|
|
+ })
|
|
|
+
|
|
|
+ const init = useCallback((params: any) => {
|
|
|
+ dispatch({ type: 'init', params })
|
|
|
+ }, [])
|
|
|
+
|
|
|
+ const set = useCallback((params: State) => {
|
|
|
+ if (params) {
|
|
|
+ dispatch({ type: 'set', params })
|
|
|
+ }
|
|
|
+ }, [])
|
|
|
+
|
|
|
+ const knewsOk = useCallback((props: any) => {
|
|
|
+ let { title, knewsThumbUrl, knewsLink, description, knewsThumbId } = props?.newsList[0]
|
|
|
+ let obj = {
|
|
|
+ parentId,
|
|
|
+ belongUser,
|
|
|
+ folder: false,
|
|
|
+ fileType,
|
|
|
+ title: title,
|
|
|
+ knewsThumbUrl: knewsThumbUrl,
|
|
|
+ knewsLink: knewsLink,
|
|
|
+ description: description,
|
|
|
+ sort: props.sort
|
|
|
+ }
|
|
|
+ if (knewsThumbId) {
|
|
|
+ obj['knewsThumbId'] = knewsThumbId
|
|
|
+ }
|
|
|
+ if (rightClickPup?.id && rightClickPup?.id !== 'all') {
|
|
|
+ obj['sysMediaId'] = rightClickPup?.id
|
|
|
+ edit.run(obj).then((res) => {
|
|
|
+ if (res) {
|
|
|
+ list.refresh()
|
|
|
+ set({ knewsVisrible: false, knewsdefaultData: null })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ eidtWxlists.run({ sysMediaId: rightClickPup?.id, mpIds: props?.mpIds || [] }).then((res) => {
|
|
|
+ console.log(res)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ add.run({ ...obj, mpIds: props?.mpIds }).then((res) => {
|
|
|
+ if (res) {
|
|
|
+ list.refresh()
|
|
|
+ set({ knewsVisrible: false, knewsdefaultData: null })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }, [add, edit, list, parentId, belongUser, fileType, rightClickPup])
|
|
|
+
|
|
|
+ const fileOk = useCallback((e: any, fnc?: () => Promise<any>, selectWx?: number[]) => {
|
|
|
+ e?.stopPropagation()
|
|
|
+ if (fnc) {
|
|
|
+ fnc().then(res => {
|
|
|
+ if (!res) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (res?.file && file) {
|
|
|
+
|
|
|
+ let newFile = new File([file], res?.fileName ? res?.fileName + '.' + file?.name?.split('.')[1] : file?.name, { type: file?.type })
|
|
|
+ let formData = new FormData();
|
|
|
+
|
|
|
+ if (fileType === 'voice') {
|
|
|
+ if (newFile.size > 2097152) {
|
|
|
+ message.error('请上传小于2M的音频')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fileType === 'video' || fileType === 'image') {
|
|
|
+ if (newFile.size > 10485760) {
|
|
|
+ message.error('请上传小于10M的素材')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fileUrl.run({ type: newFile.type }).then(res1 => {
|
|
|
+ Object.keys(res1).forEach((key: string) => {
|
|
|
+ if (key !== 'url') {
|
|
|
+ formData.append(key, res1[key])
|
|
|
+ }
|
|
|
+ })
|
|
|
+ formData.append('file', newFile)
|
|
|
+
|
|
|
+ request(res1?.ossUrl, { method: 'post', body: formData }).then(async (res2: { code: number, data: { url: string } }) => {
|
|
|
+ if (res2.code === 200) {
|
|
|
+ message.success('上传成功')
|
|
|
+ if (res) {
|
|
|
+
|
|
|
+ if (res2?.data?.url) {
|
|
|
+ let fileMd5 = await getMD5(file)
|
|
|
+ let obj = { title: res?.fileName, fileType, folder: false, parentId, belongUser, url: res2?.data?.url, mpIds: selectWx, sort: res?.sort, fileMd5, fileSize: newFile?.size }
|
|
|
+ if (fileType === 'video') {
|
|
|
+ obj['videoTitle'] = res?.videoTitle || res?.title
|
|
|
+ obj['videoDescription'] = res?.videoDescription
|
|
|
+ }
|
|
|
+ add.run(obj).then((res) => {
|
|
|
+ list.refresh()
|
|
|
+ offEditFile()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ message.error('上传失败!')
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ if (fileName) {
|
|
|
+ let obj = { title: fileName, fileType, folder: true, parentId, belongUser, mpIds: selectWx, sort }
|
|
|
+ add.run(obj).then((res) => {
|
|
|
+ get_folder_tree.refresh()
|
|
|
+ list.refresh()
|
|
|
+ offEditFile()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, [fileName, fileType, parentId, belongUser, list, file])
|
|
|
+
|
|
|
+ const nameOk = useCallback((selectWx: any) => {
|
|
|
+ if (fileName && actionItem) {
|
|
|
+ let obj = { title: fileName, belongUser, sysMediaId: actionItem?.id, fileType: actionItem?.fileType, folder: actionItem?.folder, url: actionItem?.url, sort }
|
|
|
+ if (fileType === 'video') {
|
|
|
+ obj['videoTitle'] = videoTitle
|
|
|
+ obj['videoDescription'] = videoDescription
|
|
|
+ }
|
|
|
+ edit.run(obj).then((res) => {
|
|
|
+ list.refresh()
|
|
|
+ offEditFile()
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ eidtWxlists.run({ sysMediaId: actionItem?.id, mpIds: selectWx || [] }).then((res) => {
|
|
|
+ console.log(res)
|
|
|
+ })
|
|
|
+ }, [fileName, actionItem, edit, belongUser, fileType, videoTitle, videoDescription])
|
|
|
+
|
|
|
+ const dels = useCallback((id?: any) => {
|
|
|
+ let arr = typeof id === 'number' ? [id] : selectFile
|
|
|
+ let len = arr?.length || 0
|
|
|
+ if (len) {
|
|
|
+ arr?.map((id, index) => {
|
|
|
+ del.run(id).then(() => {
|
|
|
+ set({ selectFile: selectFile?.filter(i => i !== id) })
|
|
|
+ if (index === len - 1) {
|
|
|
+ list.refresh()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }, [list, selectFile])
|
|
|
+
|
|
|
+ const getList = useCallback((props?: any) => {
|
|
|
+ console.log('fileType',fileType)
|
|
|
+ let obj = { pageSize: 20, pageNum: 1, fileType, belongUser, parentId, ...props }
|
|
|
+ list.run(obj).then((res) => {
|
|
|
+ setIsOk(true)
|
|
|
+ })
|
|
|
+ }, [list, fileType, belongUser, parentId])
|
|
|
+
|
|
|
+ const onFile = useCallback((e: any, item: { id: any, folder?: boolean }, isAll?: boolean, single?: boolean) => {
|
|
|
+ let { id } = item
|
|
|
+ e?.stopPropagation()
|
|
|
+ if (isAll && !single) {
|
|
|
+ set({ selectFile: [...new Set([...selectFile as number[], id])] })
|
|
|
+ } else {
|
|
|
+ if (item?.folder && !single) {
|
|
|
+ message.error('不能选择文件夹')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ set({ selectFile: [item.id], selectItem: item })
|
|
|
+ }
|
|
|
+ }, [selectFile])
|
|
|
+
|
|
|
+ const fileClick = useCallback((item) => {
|
|
|
+ if (isOk) {
|
|
|
+ setIsOk(false)
|
|
|
+ if (belongUser == '1' && path) {
|
|
|
+ set({ path: [...path, item] })
|
|
|
+ }
|
|
|
+ if (belongUser == '0' && publicPath) {
|
|
|
+ set({ publicPath: [...publicPath, item] })
|
|
|
+ }
|
|
|
+ set({ parentId: item.id })
|
|
|
+ getList({ parentId: item.id })
|
|
|
+ }
|
|
|
+
|
|
|
+ }, [path, publicPath, fileType, belongUser, isOk])
|
|
|
+
|
|
|
+ const treeClick=useCallback((item) => {
|
|
|
+ if (isOk) {
|
|
|
+ setIsOk(false)
|
|
|
+ console.log(item, belongUser == '1')
|
|
|
+ if (belongUser == '1' && path) {
|
|
|
+ set({ path: [path[0], item] })
|
|
|
+ }
|
|
|
+ if (belongUser == '0' && publicPath) {
|
|
|
+ set({ publicPath: [publicPath[0], item] })
|
|
|
+ }
|
|
|
+ set({ parentId: item.id })
|
|
|
+ getList({ parentId: item.id })
|
|
|
+ }
|
|
|
+ }, [path, publicPath, fileType, belongUser, isOk])
|
|
|
+
|
|
|
+ const pathClick = useCallback((item) => {
|
|
|
+ let newPath: any[] = []
|
|
|
+ if (belongUser == '1') {
|
|
|
+ path?.forEach((paths, index) => {
|
|
|
+ if (index <= item.index) {
|
|
|
+ newPath.push(paths)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ set({ path: newPath })
|
|
|
+ } else {
|
|
|
+ publicPath?.forEach((paths, index) => {
|
|
|
+ if (index <= item.index) {
|
|
|
+ newPath.push(paths)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ set({ publicPath: newPath })
|
|
|
+ }
|
|
|
+ set({ parentId: item.parentId })
|
|
|
+ getList({ pageSize: 20, pageNum: 1, fileType, belongUser, parentId: item.id })
|
|
|
+ }, [path, fileType, belongUser, publicPath])
|
|
|
+
|
|
|
+ const offFile = useCallback((e: any, item: { id: any }) => {
|
|
|
+ let { id } = item
|
|
|
+ set({ selectFile: selectFile?.filter(i => i !== id) })
|
|
|
+ }, [selectFile])
|
|
|
+
|
|
|
+ const changeClickFile = useCallback((e: any, item: { id: any, folder?: boolean }, isAll?: boolean, noFile?: boolean) => {
|
|
|
+ console.log('2222222---->', noFile);
|
|
|
+ let { id } = item
|
|
|
+ e?.stopPropagation()
|
|
|
+ let state = selectFile?.some((i) => i === id)
|
|
|
+ if (isAll) {
|
|
|
+ if (state) {
|
|
|
+ set({ selectFile: selectFile?.filter(i => i !== id) })
|
|
|
+ } else {
|
|
|
+ set({ selectFile: [...selectFile as number[], id] })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!noFile) {
|
|
|
+ if (item?.folder) {
|
|
|
+ message.error('不能选择文件夹')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (state) {
|
|
|
+ set({ selectFile: selectFile?.filter(i => i !== id) })
|
|
|
+ set({ selectItem: null })
|
|
|
+ } else {
|
|
|
+ set({ selectFile: [id] })
|
|
|
+ set({ selectItem: item })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, [selectFile, fileType])
|
|
|
+
|
|
|
+ const delPupOn = useCallback((delPupId) => {
|
|
|
+ set({ delPupId })
|
|
|
+ }, [])
|
|
|
+
|
|
|
+ const delPupOff = useCallback(() => {
|
|
|
+ set({ delPupId: '' })
|
|
|
+ offFile(null, { id: delPupId })
|
|
|
+ }, [delPupId,])
|
|
|
+
|
|
|
+ const editFile = useCallback((e?: any,) => {
|
|
|
+ e?.stopPropagation()
|
|
|
+ onFile(null, rightClickPup, true, true)
|
|
|
+
|
|
|
+ let obj = { fileVisible: true, actionItem: rightClickPup, fileName: rightClickPup.title, sort: rightClickPup.sort }
|
|
|
+ if (rightClickPup?.fileType === 'video') {
|
|
|
+ obj['videoTitle'] = rightClickPup?.videoTitle || rightClickPup?.title
|
|
|
+ obj['videoDescription'] = rightClickPup?.videoDescription
|
|
|
+ }
|
|
|
+ set(obj)
|
|
|
+
|
|
|
+ }, [rightClickPup])
|
|
|
+
|
|
|
+ const offEditFile = useCallback(() => {
|
|
|
+ set({ fileVisible: false, imgVisrible: false,sortVisible:false, actionItem: '', fileName: '', selectFile: selectFile?.filter(id => id !== actionItem?.id), sort: 0 })
|
|
|
+ }, [selectFile, actionItem])
|
|
|
+
|
|
|
+ const allFile = useCallback(() => {
|
|
|
+ let allArr: any[] = []
|
|
|
+ list?.data?.records?.forEach((item: { id: any }) => {
|
|
|
+ allArr.push(item.id)
|
|
|
+ })
|
|
|
+ set({ selectFile: allArr.filter((i) => selectFile?.every(id => id !== i)) })
|
|
|
+ }, [selectFile, list])
|
|
|
+
|
|
|
+ const showNews = useCallback(() => {
|
|
|
+
|
|
|
+ }, [])
|
|
|
+ return {
|
|
|
+ state,
|
|
|
+ init,
|
|
|
+ set,
|
|
|
+ fileOk,
|
|
|
+ getList,
|
|
|
+ dels,
|
|
|
+ onFile,
|
|
|
+ changeClickFile,
|
|
|
+ allFile,
|
|
|
+ delPupOn,
|
|
|
+ delPupOff,
|
|
|
+ editFile,
|
|
|
+ offEditFile,
|
|
|
+ nameOk,
|
|
|
+ fileClick,
|
|
|
+ treeClick,
|
|
|
+ pathClick,
|
|
|
+ knewsOk,
|
|
|
+ configSort,
|
|
|
+ addWx,
|
|
|
+ list,
|
|
|
+ add,
|
|
|
+ sync,
|
|
|
+ get,
|
|
|
+ edit,
|
|
|
+ syncId,
|
|
|
+ typeEnum,
|
|
|
+ eidtWxlists,
|
|
|
+ get_folder_tree,
|
|
|
+ edit_media_folder
|
|
|
+ }
|
|
|
+}
|
|
|
+export default useBdMediaPup
|