index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import React, { PureComponent } from 'react';
  2. import { routerRedux } from 'dva/router';
  3. import queryString from 'query-string';
  4. import { connect } from 'dva';
  5. import { Spin, Popover, Badge, Table, Radio, Card, Form, Input, Icon, Button, Select } from 'antd';
  6. import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
  7. import SupportSelectSortModal from './support';
  8. import ResourceSelectModal from './resource';
  9. import { Codes, ossHost } from '../../../utils/config';
  10. @Form.create()
  11. @connect(state => ({
  12. merchant: state.merchant,
  13. resource: state.resource,
  14. support: state.support,
  15. supportDetail: state.supportDetail,
  16. }))
  17. export default class SupportDetail extends PureComponent {
  18. state = { curClickedBtn: null };
  19. componentDidMount() {
  20. const { dispatch } = this.props;
  21. dispatch({
  22. type: 'merchant/query',
  23. payload: {
  24. pageSize: 1000,
  25. pageNo: 1,
  26. }
  27. })
  28. }
  29. // 展示选择模态框 - 加载第一页数据
  30. handleModalShow = (btnName) => {
  31. this.setState({
  32. curClickedBtn: btnName,
  33. }, () => {
  34. const { dispatch } = this.props;
  35. if (btnName == 'supportBtn') {
  36. dispatch({ type: 'supportDetail/showSupportModal' });
  37. dispatch({
  38. type: 'support/query',
  39. payload: {
  40. pageNo: 1,
  41. pageSize: 10,
  42. }
  43. });
  44. } else if (btnName == 'imgBtn' || btnName == 'cvImgBtn') {
  45. dispatch({ type: 'supportDetail/showResourceModal' });
  46. dispatch({
  47. type: 'resource/query',
  48. payload: {
  49. pageNo: 1,
  50. pageSize: 10,
  51. type: Codes.CODE_IMAGE,
  52. }
  53. });
  54. }
  55. })
  56. }
  57. // 取消/关闭 - 隐藏选择模态框
  58. handleModalCancel = () => {
  59. const { curClickedBtn } = this.state;
  60. const { dispatch } = this.props;
  61. if (curClickedBtn == 'supportBtn') {
  62. dispatch({ type: 'supportDetail/hideSupportModal' });
  63. }
  64. if (curClickedBtn == 'imgBtn' || curClickedBtn == 'cvImgBtn') {
  65. dispatch({ type: 'supportDetail/hideResourceModal' });
  66. }
  67. }
  68. // 提交 - 保存选择和排序完的数据到model中
  69. handleModalOk = (data) => {
  70. const { curClickedBtn } = this.state;
  71. const { dispatch } = this.props;
  72. if (curClickedBtn == 'supportBtn') {
  73. dispatch({
  74. type: 'supportDetail/saveSupportList',
  75. payload: { supportList: data }
  76. });
  77. } else if (curClickedBtn == 'imgBtn') {
  78. dispatch({
  79. type: 'supportDetail/saveImgList',
  80. payload: { imgList: data.map(item => item.path) },
  81. });
  82. } else if (curClickedBtn == 'cvImgBtn') {
  83. dispatch({
  84. type: 'supportDetail/saveCoverImg',
  85. payload: { coverUrl: data.path },
  86. });
  87. }
  88. }
  89. // 搜索
  90. handleModalSearch = (data) => {
  91. const { curClickedBtn } = this.state;
  92. const { dispatch } = this.props;
  93. const newData = { ...data };
  94. if (newData.keyword) {
  95. newData[newData.field] = newData.keyword;
  96. }
  97. delete newData.field;
  98. delete newData.keyword;
  99. if (curClickedBtn == 'supportBtn') {
  100. dispatch({
  101. type: `support/query`,
  102. payload: { ...newData, pageNo: 1, pageSize: 10, status: Codes.CODE_NORMAL },
  103. });
  104. } else if (curClickedBtn == 'imgBtn' || curClickedBtn == 'cvImgBtn') {
  105. dispatch({
  106. type: `resource/query`,
  107. payload: { ...newData, pageNo: 1, pageSize: 10, status: Codes.CODE_NORMAL, type: Codes.CODE_IMAGE },
  108. });
  109. }
  110. }
  111. // 翻页 - 资源列表
  112. handleModalTableChange = (pagination, filterArgs, filters) => {
  113. const { curClickedBtn } = this.state;
  114. const { dispatch } = this.props;
  115. const newFilters = { ...filters };
  116. if (newFilters.keyword) {
  117. newFilters[newFilters.field] = newFilters.keyword;
  118. delete newFilters.field;
  119. delete newFilters.keyword;
  120. } else {
  121. delete newFilters.field;
  122. delete newFilters.keyword;
  123. }
  124. const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
  125. const tableFilters = Object.keys(filterArgs).reduce((obj, key) => {
  126. const newObj = { ...obj };
  127. newObj[key] = getValue(filterArgs[key]);
  128. return newObj;
  129. }, {});
  130. const data = { ...newFilters, ...tableFilters, pageNo: pagination.current, pageSize: pagination.pageSize };
  131. Object.keys(data).map(key => data[key] ? null : delete data[key]);
  132. if (curClickedBtn == 'supportBtn') {
  133. dispatch({ type: `support/query`, payload: { ...data, status: Codes.CODE_NORMAL } });
  134. } else if (curClickedBtn == 'imgBtn' || curClickedBtn == 'cvImgBtn') {
  135. dispatch({ type: `resource/query`, payload: { ...data, status: Codes.CODE_NORMAL, type: Codes.CODE_IMAGE } });
  136. }
  137. }
  138. handlePageSubmit = (e) => {
  139. e.preventDefault()
  140. const {
  141. dispatch,
  142. form: {
  143. validateFields,
  144. getFieldsValue,
  145. resetFields
  146. },
  147. supportDetail: {
  148. operType,
  149. currentItem,
  150. filters,
  151. }
  152. } = this.props;
  153. validateFields((errors) => {
  154. if (errors) { return; }
  155. // 过滤掉gmtCreated, gmtModified, status三个字段
  156. const { supportList, imgList, gmtCreated, gmtModified, ...rest } = currentItem;
  157. let newImgList;
  158. let newSupportList;
  159. if (Array.isArray(imgList) && imgList.length) {
  160. newImgList = imgList;
  161. } else {
  162. newImgList = [];
  163. }
  164. // 如果supportList是个list且内容不为空
  165. if (Array.isArray(supportList) && supportList.length) {
  166. newSupportList = supportList.map(item => item.id);
  167. } else {
  168. newSupportList = [];
  169. }
  170. // 取出名称name值作为标题title字段值
  171. const { title, subTitle } = getFieldsValue();
  172. // 最终提交的数据
  173. const data = {
  174. ...rest,
  175. ...getFieldsValue(),
  176. name: `${title}${subTitle}`,
  177. imgList: newImgList,
  178. supportList: newSupportList,
  179. };
  180. // 其他参数
  181. if (operType == 'create') {
  182. data.type = Codes.CODE_SUPPORT;
  183. data.status = Codes.CODE_NORMAL;
  184. }
  185. dispatch({
  186. type: `supportDetail/${operType}`,
  187. payload: data,
  188. callback: () => {
  189. dispatch(
  190. routerRedux.push({
  191. pathname: '/product/support',
  192. search: queryString.stringify(filters),
  193. })
  194. );
  195. resetFields();
  196. }
  197. })
  198. });
  199. }
  200. handlePageCancel = () => {
  201. const { dispatch, supportDetail: { filters } } = this.props;
  202. dispatch({ type: 'supportDetail/initState' });
  203. dispatch(
  204. routerRedux.push({
  205. pathname: '/product/support',
  206. search: queryString.stringify(filters),
  207. })
  208. );
  209. }
  210. render() {
  211. const {
  212. merchant,
  213. resource,
  214. support,
  215. supportDetail,
  216. form: {
  217. getFieldDecorator
  218. },
  219. } = this.props;
  220. const {
  221. itemLoading,
  222. currentItem,
  223. filters,
  224. supportModalVisible,
  225. resourceModalVisible
  226. } = supportDetail;
  227. const {
  228. cpId,
  229. title,
  230. subTitle,
  231. code,
  232. name,
  233. digest,
  234. detail,
  235. coverUrl,
  236. imgList,
  237. supportList,
  238. } = currentItem;
  239. const { curClickedBtn } = this.state;
  240. // 待选表格去掉分页的跳转及变换页码
  241. if (resource && resource.pagination) {
  242. delete resource.pagination.showQuickJumper;
  243. delete resource.pagination.showSizeChanger;
  244. }
  245. const supportTableColumns = [{
  246. title: '配套编号',
  247. dataIndex: 'code',
  248. key: 'code',
  249. },{
  250. title: '配套名称',
  251. dataIndex: 'name',
  252. key: 'name',
  253. }];
  254. const imgTableColumns = [{
  255. title: '位置',
  256. dataIndex: 'sort',
  257. key: 'sort',
  258. render: (text, record, index) => index + 1,
  259. },{
  260. title: '缩略图',
  261. dataIndex: 'path',
  262. key: 'path',
  263. render: (text, record) => (
  264. <Popover
  265. content={<img alt="" src={`${ossHost}/${text}`} width={350} />}
  266. title={record.name}
  267. >
  268. <img alt="" src={`${ossHost}/${text}`} width={70} />
  269. </Popover>
  270. ),
  271. }];
  272. const formItemLayout = {
  273. labelCol: {
  274. span: 7,
  275. },
  276. wrapperCol: {
  277. span: 12,
  278. },
  279. };
  280. const submitFormLayout = {
  281. wrapperCol: {
  282. xs: { span: 24, offset: 0 },
  283. sm: { span: 10, offset: 7 },
  284. },
  285. };
  286. return (
  287. <PageHeaderLayout>
  288. <Spin spinning={itemLoading}>
  289. <Card title="配套信息">
  290. <Form layout="horizontal" onSubmit={this.handlePageSubmit}>
  291. <Form.Item label="配套编号:" hasFeedback {...formItemLayout}>
  292. {getFieldDecorator('code', {
  293. rules: [{ required: true, type: 'string', message: "编号为必填项!" }],
  294. initialValue: code,
  295. })(<Input />)}
  296. </Form.Item>
  297. <Form.Item label="配套标题:" hasFeedback {...formItemLayout}>
  298. {getFieldDecorator('title', {
  299. rules: [{ required: true, type: 'string', message: "标题为必填项!" }],
  300. initialValue: title,
  301. })(<Input />)}
  302. </Form.Item>
  303. <Form.Item label="配套副标题:" hasFeedback {...formItemLayout}>
  304. {getFieldDecorator('subTitle', {
  305. rules: [{ required: true, type: 'string', message: "副标题为必填项!" }],
  306. initialValue: subTitle,
  307. })(<Input />)}
  308. </Form.Item>
  309. <Form.Item label="配套名称:" hasFeedback {...formItemLayout}>
  310. {getFieldDecorator('name', {
  311. initialValue: name,
  312. })(<Input disabled={true} placeholder="根据标题及副标题自动生成"/>)}
  313. </Form.Item>
  314. <Form.Item label="配套概要:" hasFeedback {...formItemLayout}>
  315. {getFieldDecorator('digest', {
  316. initialValue: digest,
  317. })(<Input.TextArea />)}
  318. </Form.Item>
  319. <Form.Item label="配套详情:" hasFeedback {...formItemLayout}>
  320. {getFieldDecorator('detail', {
  321. initialValue: detail,
  322. })(<Input.TextArea />)}
  323. </Form.Item>
  324. <Form.Item label="所属供应商:" {...formItemLayout}>
  325. {getFieldDecorator('cpId', {
  326. initialValue: cpId,
  327. })(
  328. <Select placeholder="请选择">{merchant.list.map(item => <Select.Option value={item.id} key={item.id}>{item.name}</Select.Option>)}</Select>
  329. )}
  330. </Form.Item>
  331. <Form.Item label="封面图片" {...formItemLayout}>
  332. <Button onClick={() => this.handleModalShow('cvImgBtn')} type="primary" icon="select" size="small">选择</Button>
  333. {!coverUrl? null :
  334. <Card
  335. hoverable
  336. bordered
  337. cover={<img alt="" src={coverUrl.startsWith('http') ? coverUrl : `${ossHost}/${coverUrl}`} />}
  338. style={{ width: 240, marginTop: 20 }}
  339. >
  340. </Card>}
  341. </Form.Item>
  342. <Form.Item label="图片列表" {...formItemLayout}>
  343. <Button onClick={() => this.handleModalShow('imgBtn')} type="primary" size="small" icon="edit">编辑</Button>
  344. </Form.Item>
  345. <Form.Item wrapperCol={{ offset: 7, span: 12 }}>
  346. <Table
  347. locale={{
  348. emptyText: <span style={{ color: "#C6D0D6" }}>&nbsp;&nbsp;<Icon type="frown-o"/>
  349. 该配套下不包含任何图片,请选择!</span>
  350. }}
  351. dataSource={(imgList || []).map((item,index) => ({ id: index, path: item }))}
  352. columns={imgTableColumns}
  353. rowKey={record => record.id}
  354. bordered
  355. pagination={false}
  356. />
  357. </Form.Item>
  358. <Form.Item label="相关配套" {...formItemLayout}>
  359. <Button onClick={() => this.handleModalShow('supportBtn')} type="primary" size="small" icon="edit">编辑</Button>
  360. </Form.Item>
  361. <Form.Item wrapperCol={{ offset: 7, span: 12 }}>
  362. <Table
  363. locale={{
  364. emptyText: <span style={{ color: "#C6D0D6" }}>&nbsp;&nbsp;<Icon type="frown-o"/>
  365. 暂无与该配套相关的配套,请选择!</span>
  366. }}
  367. dataSource={supportList || []}
  368. columns={supportTableColumns}
  369. rowKey={record => record.id}
  370. bordered
  371. pagination={false}
  372. />
  373. </Form.Item>
  374. <Form.Item {...submitFormLayout} style={{ marginTop: 32 }}>
  375. <Button onClick={this.handlePageCancel}>取消</Button>
  376. <Button type="primary" style={{ marginLeft: 35 }} htmlType="submit">提交</Button>
  377. </Form.Item>
  378. </Form>
  379. {/*周边的模态选择框*/}
  380. <SupportSelectSortModal
  381. rowKeyName="id"
  382. modalVisible={supportModalVisible}
  383. width={600}
  384. onCancel={this.handleModalCancel}
  385. onOk={this.handleModalOk}
  386. onSearch={this.handleModalSearch}
  387. selTableData={supportList || []}
  388. fsTableDataSource={support.list || []}
  389. fsTableLoading={support.listLoading}
  390. fsTablePagination={support.pagination}
  391. fsTableOnChange={this.handleModalTableChange}
  392. />
  393. {/*图片资源的模态选择框*/}
  394. <ResourceSelectModal
  395. mode={curClickedBtn == 'imgBtn' ? 'multiple' : 'single'}
  396. rowKeyName="id"
  397. modalVisible={resourceModalVisible}
  398. width={600}
  399. onOk={this.handleModalOk}
  400. onCancel={this.handleModalCancel}
  401. onSearch={this.handleModalSearch}
  402. selTableData={[]}
  403. fsTableDataSource={resource.list || []}
  404. fsTableLoading={resource.listLoading}
  405. fsTablePagination={resource.pagination}
  406. fsTableOnChange={this.handleModalTableChange}
  407. />
  408. </Card>
  409. </Spin>
  410. </PageHeaderLayout>
  411. );
  412. }
  413. }