smModalBox.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React, { useCallback, useEffect, useState } from "react"
  2. import { Card, Col, Empty, Modal, Pagination, Row, Tabs, Tag, Image, Input, message, Button, Space } from "antd"
  3. import { useModel } from 'umi'
  4. import FlieBox from '@/components/FileBox'
  5. const { TabPane } = Tabs
  6. import style from './index.less'
  7. type Props = {
  8. mediaType: number,
  9. visible: boolean,
  10. onOk: (url: string, type?: number) => void,
  11. onCancel: () => void,
  12. title: string,
  13. actionKey?: string,
  14. isShowWx?: boolean,
  15. isAllData?: boolean,
  16. noFile?: boolean//是否禁止选择文件夹
  17. }
  18. let config2 = {
  19. 1: 'image',
  20. 2: 'voice',
  21. 3: 'video',
  22. 5: 'news',
  23. 6: 'knews'
  24. }
  25. const SmModalBox = (props: Props) => {
  26. const { mediaType, visible, onOk, onCancel, title, actionKey, isShowWx = true, isAllData = false, noFile = false } = props
  27. const { getWeData, update } = useModel('useOperating.useMaterialContent', model => ({
  28. getWeData: model.getWeData,
  29. update: model.update,
  30. }))
  31. const { actionWX } = useModel('useOperating.useWxGroupList', model => ({ actionWX: model.state.actionWX }))
  32. const { state, set, getList, init, typeEnum } = useModel('useOperating.useBdMediaPup')
  33. const { fileType, belongUser, parentId, selectItem } = state
  34. const [actionData, setActionData] = useState<any>(null)
  35. const [action, setAction] = useState<string>('')
  36. const [page, setPage] = useState<number>(1)//页码
  37. const [qqLink, setQqLink] = useState<string>('')
  38. //
  39. let ok = useCallback(() => {
  40. if (!actionKey) {
  41. if (isAllData) {
  42. if (belongUser == 0 || belongUser == 1) {
  43. if (selectItem && Object.keys(selectItem).length > 0) {
  44. onOk(selectItem, mediaType)
  45. } else {
  46. message.error('选择本地图片')
  47. }
  48. } else if (belongUser == 3) {
  49. if (actionData) {
  50. onOk(actionData, mediaType)
  51. } else {
  52. message.error('选择微信图片')
  53. }
  54. }
  55. } else {
  56. if (action) {
  57. onOk(action, mediaType)
  58. }
  59. }
  60. } else if (actionKey === '1') {
  61. if (qqLink) {
  62. onOk(qqLink)
  63. } else {
  64. message.error('地址不正确')
  65. }
  66. }
  67. }, [actionKey, actionData, selectItem, belongUser])
  68. useEffect(() => {
  69. if (belongUser == 0 || belongUser == 1) {
  70. getList()
  71. } else if (belongUser == 3) {
  72. setPage(1)
  73. getWeData.run({
  74. mpId: actionWX?.id,
  75. pageSize: 20,
  76. pageNum: 1,
  77. mediaType: config2[mediaType]
  78. })
  79. }
  80. }, [fileType, belongUser, parentId, actionWX?.id])
  81. useEffect(() => {
  82. init({ fileType: config2[mediaType] })
  83. }, [mediaType])
  84. // 分页
  85. let wxpageSizeChange = useCallback((page: number, pageSize?: number) => {
  86. setPage(page)
  87. if (actionWX?.id) {
  88. getWeData.run({
  89. mpId: actionWX?.id,
  90. pageSize: pageSize,
  91. pageNum: page,
  92. mediaType: config2[mediaType]
  93. })
  94. }
  95. }, [mediaType, actionWX])
  96. //选中图片
  97. let handleClick = useCallback((url, data?: any) => {
  98. setAction(url)
  99. setActionData(data)
  100. }, [])
  101. //qq视频
  102. const qqLinkChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
  103. let v = e.target.value
  104. let arr = v.split('/')
  105. let vid = arr[arr.length - 1].replace('.html', '')
  106. if (v.search(/http[s]?:\/\/v.qq.com[\S]*.html/ig) !== -1) {
  107. setQqLink(`https://v.qq.com/iframe/player.html?vid=${vid}&auto=0`)
  108. }
  109. }, [])
  110. // 更新公众号下素材
  111. let sync = useCallback(() => {
  112. update.run({ mpId: actionWX?.id, mediaType: config2[mediaType] }).then((res) => {
  113. if (res) {
  114. setPage(0)
  115. if (actionWX?.id) {
  116. getWeData.run({
  117. mpId: actionWX?.id,
  118. pageSize: 20,
  119. pageNum: 1,
  120. mediaType: config2[mediaType]
  121. })
  122. }
  123. }
  124. })
  125. }, [update, actionWX?.id, mediaType])
  126. let Tags = (props: { url: string, data?: { title?: string, name?: string } }) => {
  127. switch (mediaType) {
  128. case 1:
  129. return <Card bodyStyle={{ padding: 0, height: 150, display: 'flex', flexFlow: 'column' }} bordered={false} hoverable>
  130. <Image src={props.url} width={150} height={150} placeholder preview={false} onClick={() => handleClick(props.url, props?.data)} />
  131. </Card >
  132. case 3:
  133. return <div>
  134. <Card bodyStyle={{ padding: 10, height: 165, display: 'flex', flexFlow: 'column', border: '1px solid #efefef' }} bordered={false} hoverable onClick={() => handleClick(props.url, props?.data)}>
  135. <video src={props.url} height='130px' controls />
  136. <span>名称: {props?.data?.title || props?.data?.name}</span>
  137. </Card>
  138. </div>
  139. default:
  140. return <div>
  141. <Card bodyStyle={{ padding: 10, height: 80, display: 'flex', flexFlow: 'column', border: '1px solid #efefef' }} bordered={false} hoverable onClick={() => handleClick(props.url, props?.data)}>
  142. <audio src={props.url} controls />
  143. <span>名称: {props?.data?.title || props?.data?.name}</span>
  144. </Card>
  145. </div>
  146. }
  147. }
  148. return <Modal
  149. title={title}
  150. width={1200}
  151. open={visible}
  152. onOk={ok}
  153. onCancel={onCancel}
  154. maskClosable={false}
  155. >
  156. {
  157. !actionKey && <>
  158. <Tabs
  159. type="card"
  160. onChange={(activeKey: any) => { set({ belongUser: activeKey }) }}
  161. activeKey={belongUser}
  162. tabBarExtraContent={
  163. <Space>
  164. {
  165. belongUser === '3' ? <Button type='primary' onClick={sync} loading={update?.loading}>更新公众号下素材</Button> : ''
  166. }
  167. </Space>
  168. }
  169. >
  170. <TabPane tab={'个人本地'} key={1} />
  171. <TabPane tab={'公共本地'} key={0} />
  172. {
  173. isShowWx && <TabPane tab={"微信素材"} key={3}>
  174. {
  175. getWeData?.data?.records?.length > 0 ? <Row justify='start' gutter={[15, 15]}>
  176. {
  177. getWeData?.data?.records?.map((item: any) => {
  178. return <Col key={item?.mediaId} className={`${style.img} ${actionData?.id === item?.id ? style.action : undefined}`}>
  179. <Tags url={item?.url} data={item} />
  180. </Col>
  181. })
  182. }
  183. </Row> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
  184. }
  185. <Pagination onChange={wxpageSizeChange} size={'small'} defaultPageSize={20} current={page} total={getWeData?.data?.total} style={{ float: 'right' }} showTotal={(total) => <Tag color="cyan">总共{total}数据</Tag>} />
  186. </TabPane>
  187. }
  188. </Tabs>
  189. </>
  190. }
  191. {
  192. (belongUser == 1 || belongUser == 0) && !actionKey && <FlieBox isAll={false} noFile={noFile} />
  193. }
  194. {
  195. actionKey === '1' &&
  196. <Tabs type="card">
  197. <TabPane tab="腾讯视频" key="2" style={{ padding: 10 }}>
  198. <Row>
  199. <Col className={style.qq_link}>
  200. <label>视频地址:</label>
  201. <Input placeholder='请输入腾讯视频地址' onChange={qqLinkChange} allowClear />
  202. </Col>
  203. <Col className={style.qq_iframe}>
  204. <label>预览:</label>
  205. <iframe
  206. data-vidtype="2"
  207. height="310"
  208. width="1000"
  209. data-ratio="1.7666666666666666"
  210. data-w="848"
  211. src={qqLink}
  212. />
  213. </Col>
  214. </Row>
  215. </TabPane>
  216. </Tabs>
  217. }
  218. {
  219. actionKey === '2' && <Tabs type="card">
  220. <TabPane tab="音频" key="2" style={{ padding: 10 }}>
  221. <Row>
  222. <Col className={style.qq_link}>
  223. <label>视频地址:</label>
  224. <Input placeholder='请输入腾讯视频地址' />
  225. </Col>
  226. <Col className={style.qq_iframe}>
  227. <label>预览:</label>
  228. <iframe
  229. data-vidtype="2"
  230. height="310"
  231. width="1000"
  232. data-ratio="1.7666666666666666"
  233. data-w="848"
  234. src="https://v.qq.com/iframe/player.html?vid=e3161ny316e&auto=0"
  235. />
  236. </Col>
  237. </Row>
  238. </TabPane>
  239. </Tabs>
  240. }
  241. </Modal>
  242. }
  243. export default React.memo(SmModalBox)