content.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import NewSteps from '@/pages/weComTask/components/newSteps';
  2. import { App, Button, Card, Form, Select } from 'antd';
  3. import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
  4. import '../../global.less';
  5. import { welcomeContentData } from '../../const';
  6. import { PlusOutlined, MinusOutlined } from '@ant-design/icons'
  7. import MaterialNoTextMould from './materialNoTextMould';
  8. import MaterialMould from '@/pages/weComTask/components/materialMould';
  9. /**
  10. * 群发内容
  11. * @param param0
  12. * @returns
  13. */
  14. const Content = forwardRef(({ massSendingStrategy, value, onChange }: TASK_CREATE.ContentProps, ref: React.ForwardedRef<{ handleOk: (type: string) => void }>) => {
  15. /****************************************/
  16. const { message } = App.useApp()
  17. const ref1 = useRef<HTMLDivElement>(null)
  18. const [form] = Form.useForm();
  19. const [stepsList, setStepsList] = useState<any>([])
  20. /****************************************/
  21. useImperativeHandle(ref, () => ({
  22. handleOk(type) {
  23. handleOk(type)
  24. }
  25. }));
  26. // 回填
  27. useEffect(() => {
  28. if (!massSendingStrategy?.strategySettings) {
  29. message.error('请先设置群发策略')
  30. return
  31. }
  32. if (value && Object.keys(value).length) {
  33. filedUpdateChange(value)
  34. form.setFieldsValue(value)
  35. } else {
  36. const data = {
  37. massSendingContentDTO: massSendingStrategy.strategySettings.map(item => {
  38. return {
  39. sendContentDto: item.sendData.map(() => ({ contentDTO: [undefined], sendMode: undefined }))
  40. }
  41. })
  42. }
  43. filedUpdateChange(data)
  44. form.setFieldsValue(data)
  45. }
  46. }, [value, massSendingStrategy])
  47. const handleOk = (type: string) => {
  48. form.validateFields().then((values) => {
  49. console.log(values)
  50. onChange(values, type)
  51. }).catch(() => {
  52. form.submit()
  53. });
  54. };
  55. const filedUpdateChange = ({ massSendingContentDTO }: { massSendingContentDTO: { sendContentDto: any }[] }) => {
  56. const stepsData = massSendingContentDTO.map((item, index) => {
  57. const children = item.sendContentDto.map((ci, c_index) => {
  58. const content = ci?.contentDTO?.map((i, i_index) => {
  59. return { title: `内容${i_index + 1}`, checked: i?.length > 0, id: 'clientId' + '_' + index + '_' + c_index + 'contentDTO' }
  60. })
  61. const contentChildren = [
  62. { title: '发送模式', checked: ci?.sendMode || ci?.sendMode === 0, id: 'clientId' + '_' + index + '_' + c_index + 'sendMode' },
  63. ...content
  64. ]
  65. return {
  66. title: `发送对象 ${c_index + 1}`,
  67. id: 'clientId' + '_' + index + '_' + c_index,
  68. children: contentChildren,
  69. checked: (ci?.sendMode || ci?.sendMode === 0) && contentChildren?.every(item => item.checked)
  70. }
  71. })
  72. return {
  73. title: '策略' + (index + 1),
  74. id: 'clientId' + '_' + index,
  75. children: [
  76. ...children,
  77. { title: '完成', checked: children?.every(item => item.checked) }
  78. ],
  79. checked: children?.every(item => item.checked)
  80. }
  81. })
  82. setStepsList([
  83. ...stepsData,
  84. { title: '完成', checked: stepsData?.every(item => item.checked) }
  85. ])
  86. }
  87. return <>
  88. <div className={`body_steps`}>
  89. <NewSteps
  90. items={stepsList}
  91. onChange={(e) => {
  92. if (e?.id)
  93. ref1.current?.querySelector('#' + e?.id)?.scrollIntoView({ behavior: 'smooth' })
  94. }}
  95. />
  96. </div>
  97. <div className={`body_content`} ref={ref1}>
  98. <Form
  99. form={form}
  100. name="gjnewContent"
  101. labelAlign='left'
  102. labelCol={{ span: 5 }}
  103. colon={false}
  104. scrollToFirstError={{
  105. behavior: 'smooth',
  106. block: 'center'
  107. }}
  108. onFinishFailed={({ errorFields }) => {
  109. message.error(errorFields?.[0]?.errors?.[0])
  110. }}
  111. onFinish={handleOk}
  112. initialValues={{ massSendingContentDTO: [undefined] }}
  113. onFieldsChange={() => {
  114. console.log(form.getFieldsValue())
  115. filedUpdateChange(form.getFieldsValue())
  116. }}
  117. preserve={true}
  118. >
  119. <Form.List name='massSendingContentDTO'>
  120. {(fields) => (
  121. <>
  122. {fields.map(({ key, name, ...restField }, index) => {
  123. return <Card key={index} title={<strong>策略 {index + 1}</strong>} style={{ background: '#fff', marginBottom: 10 }} id={'clientId' + '_' + index}>
  124. <Form.List {...restField} name={[name, 'sendContentDto']}>
  125. {(fields) => (
  126. <>
  127. {fields.map(({ key, name, ...restField }, i) => {
  128. return <Card
  129. key={i}
  130. title={<strong>发送对象{i + 1} 内容配置</strong>}
  131. style={{ background: '#fff', marginBottom: 10 }}
  132. id={'clientId' + '_' + index + '_' + i}
  133. >
  134. <div id={'clientId' + '_' + index + '_' + i + 'sendMode'}>
  135. <Form.Item
  136. {...restField}
  137. label={<strong>内容组发送模式</strong>}
  138. name={[name, 'sendMode']}
  139. rules={[{ required: true, message: '请选择内容组发送模式!' }]}
  140. >
  141. <Select
  142. showSearch
  143. style={{ width: 358 }}
  144. allowClear
  145. placeholder="请选择内容组发送模式"
  146. filterOption={(input, option) =>
  147. ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
  148. }
  149. options={welcomeContentData}
  150. />
  151. </Form.Item>
  152. </div>
  153. <div id={'clientId' + '_' + index + '_' + i + 'contentDTO'}>
  154. <Form.List {...restField} name={[name, 'contentDTO']} >
  155. {(fields, { add, remove }) => {
  156. return <>
  157. {fields.map(({ key, name, ...restField }, index) => {
  158. return <div key={index}>
  159. <h3 style={{ display: 'flex', justifyContent: 'space-between', margin: 0 }}>内容{index + 1}
  160. {fields?.length > 1 && <Button
  161. type="dashed"
  162. danger
  163. onClick={() => remove(index)}
  164. icon={<MinusOutlined />}
  165. size='small'
  166. >
  167. 移除内容
  168. </Button>}
  169. </h3>
  170. <Form.Item
  171. {...restField}
  172. name={name}
  173. rules={[{
  174. validator: (_, value) => {
  175. if (!value || value?.length === 0) {
  176. return Promise.reject('请填写内容或上传附件');
  177. }
  178. return Promise.resolve();
  179. }
  180. }]}
  181. >
  182. <MaterialMould dataNum={1} />
  183. </Form.Item>
  184. </div>
  185. })}
  186. <Form.Item noStyle>
  187. <Button type="dashed" onClick={() => add()} style={{ width: '100%' }} icon={<PlusOutlined />}>
  188. 新增内容
  189. </Button>
  190. </Form.Item>
  191. </>
  192. }}
  193. </Form.List>
  194. </div>
  195. </Card>
  196. })}
  197. </>
  198. )}
  199. </Form.List>
  200. </Card>
  201. })}
  202. </>
  203. )}
  204. </Form.List>
  205. </Form>
  206. </div>
  207. </>
  208. });
  209. export default React.memo(Content);