index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import React, { PureComponent } from 'react';
  2. import { routerRedux } from 'dva/router';
  3. import PropTypes from 'prop-types';
  4. import queryString from 'query-string';
  5. import { connect } from 'dva';
  6. import { Upload, Spin, Switch, Popover, Badge, Table, Radio, Card, Form, Input, Icon, Button, Select } from 'antd';
  7. import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
  8. import CourseSelectModal from './course';
  9. import SupportSelectModal from './support';
  10. import PackageSelectModal from './package';
  11. import { Codes, productType } from '../../../utils/config';
  12. import styles from './index.less';
  13. const RadioGroup = Radio.Group;
  14. const FormItem = Form.Item;
  15. const Option = Select.Option;
  16. const { Meta } = Card;
  17. const { TextArea } = Input;
  18. @Form.create()
  19. @connect(state => ({
  20. course: state.course,
  21. support: state.support,
  22. combo: state.combo,
  23. merchant: state.merchant,
  24. goodsDetail: state.goodsDetail,
  25. }))
  26. export default class GoodsAdd extends PureComponent {
  27. state = {
  28. radioType: Codes.CODE_COURSE,
  29. curClickedBtn: null,
  30. courseModalVisible: false,
  31. supportModalVisible: false,
  32. packageModalVisible: false,
  33. selectItem: null,
  34. };
  35. componentDidMount() {
  36. const { dispatch } = this.props;
  37. dispatch({
  38. type: 'merchant/query',
  39. payload: {
  40. pageNo: 1,
  41. pageSize: 1000,
  42. status: Codes.CODE_NORMAL,
  43. domain: Codes.CODE_PJ,
  44. }
  45. })
  46. }
  47. handleRadioOnChange = (e) => {
  48. this.setState({ radioType: e.target.value });
  49. }
  50. handleModalSearch = (data) => {
  51. const { curClickedBtn } = this.state;
  52. const { dispatch } = this.props;
  53. const newData = { ...data };
  54. if (newData.keyword) {
  55. newData[newData.field] = newData.keyword;
  56. delete newData.field;
  57. delete newData.keyword;
  58. } else {
  59. delete newData.field;
  60. delete newData.keyword;
  61. }
  62. if (curClickedBtn === Codes.CODE_COURSE) {
  63. dispatch({
  64. type: 'course/query',
  65. payload: { ...newData, pageNo: 1, pageSize: 10, status: Codes.CODE_NORMAL },
  66. });
  67. }
  68. }
  69. handleModalOk = (data) => {
  70. const { curClickedBtn } = this.state;
  71. const { dispatch } = this.props;
  72. if (curClickedBtn === Codes.CODE_COURSE) {
  73. this.setState({
  74. selectItem: data,
  75. courseModalVisible: false,
  76. });
  77. } else if(curClickedBtn === Codes.CODE_SUPPORT) {
  78. this.setState({
  79. selectItem: data,
  80. supportModalVisible: false,
  81. });
  82. } else if (curClickedBtn === Codes.CODE_PACKAGE) {
  83. this.setState({
  84. selectItem: data,
  85. packageModalVisible: false,
  86. });
  87. }
  88. }
  89. handleModalCancel = () => {
  90. const { curClickedBtn } = this.state;
  91. const { dispatch } = this.props;
  92. if (curClickedBtn === Codes.CODE_COURSE) {
  93. this.setState({ courseModalVisible: false });
  94. } else if (curClickedBtn === Codes.CODE_SUPPORT) {
  95. this.setState({ supportModalVisible: false });
  96. } else if (curClickedBtn === Codes.CODE_PACKAGE) {
  97. this.setState({ packageModalVisible: false });
  98. }
  99. }
  100. handleModalShow = (name) => {
  101. const { dispatch } = this.props;
  102. if (name === Codes.CODE_COURSE) {
  103. this.setState({
  104. curClickedBtn: name,
  105. courseModalVisible: true,
  106. }, () => {
  107. dispatch({
  108. type: 'course/query',
  109. payload: {
  110. pageNo: 1,
  111. pageSize: 10,
  112. status: Codes.CODE_NORMAL,
  113. }
  114. });
  115. })
  116. } else if (name === Codes.CODE_SUPPORT) {
  117. this.setState({
  118. curClickedBtn: name,
  119. supportModalVisible: true,
  120. }, () => {
  121. dispatch({
  122. type: 'support/query',
  123. payload: {
  124. pageNo: 1,
  125. pageSize: 10,
  126. status: Codes.CODE_NORMAL,
  127. }
  128. });
  129. })
  130. } else if (name === Codes.CODE_PACKAGE) {
  131. this.setState({
  132. curClickedBtn: name,
  133. packageModalVisible: true,
  134. }, () => {
  135. dispatch({
  136. type: 'combo/query',
  137. payload: {
  138. pageNo: 1,
  139. pageSize: 10,
  140. status: Codes.CODE_NORMAL,
  141. }
  142. });
  143. })
  144. }
  145. }
  146. handleModalTableChange = () => {
  147. const { curClickedBtn } = this.state;
  148. const { dispatch } = this.props;
  149. const newFilters = { ...filters };
  150. if (newFilters.keyword) {
  151. newFilters[newFilters.field] = newFilters.keyword;
  152. delete newFilters.field;
  153. delete newFilters.keyword;
  154. } else {
  155. delete newFilters.field;
  156. delete newFilters.keyword;
  157. }
  158. const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
  159. const tableFilters = Object.keys(filterArgs).reduce((obj, key) => {
  160. const newObj = { ...obj };
  161. newObj[key] = getValue(filterArgs[key]);
  162. return newObj;
  163. }, {});
  164. const data = { ...newFilters, ...tableFilters, pageNo: pagination.current, pageSize: pagination.pageSize };
  165. Object.keys(data).map(key => data[key] ? null : delete data[key]);
  166. if (curClickedBtn === Codes.CODE_COURSE) {
  167. dispatch({ type: 'course/query', payload: { ...data, status: Codes.CODE_NORMAL } });
  168. } else if (curClickedBtn === Codes.CODE_SUPPORT) {
  169. dispatch({ type: 'support/query', payload: { ...data, status: Codes.CODE_NORMAL } });
  170. } else if (curClickedBtn === Codes.CODE_PACKAGE) {
  171. dispatch({ type: 'combo/query', payload: { ...data, status: Codes.CODE_PACKAGE } });
  172. }
  173. }
  174. handlePageSubmit = (e) => {
  175. e.preventDefault();
  176. const {
  177. dispatch,
  178. form: {
  179. validateFields,
  180. getFieldsValue,
  181. resetFields,
  182. },
  183. goodsDetail: {
  184. filters,
  185. }
  186. } = this.props;
  187. const {
  188. selectItem,
  189. radioType,
  190. } = this.state;
  191. validateFields(errors => {
  192. if (errors) return;
  193. const data = {
  194. ...getFieldsValue(['merchantId', 'status']),
  195. pid: selectItem.pid,
  196. };
  197. data.status ? data.status = Codes.CODE_NORMAL : data.status = Codes.CODE_DELETE;
  198. dispatch({
  199. type: `goodsDetail/createMerchantProduct`,
  200. payload: data,
  201. callback: () => {
  202. dispatch(
  203. routerRedux.push({
  204. pathname: '/goods',
  205. search: queryString.stringify(filters),
  206. })
  207. );
  208. resetFields();
  209. }
  210. });
  211. });
  212. }
  213. handlePageCancel = () => {
  214. const { dispatch, goodsDetail: { filters } } = this.props;
  215. dispatch(
  216. routerRedux.push({
  217. pathname: '/goods',
  218. search: queryString.stringify(filters),
  219. })
  220. );
  221. }
  222. renderLabelName = () => {
  223. const { radioType } = this.state;
  224. switch (radioType) {
  225. case Codes.CODE_COURSE:
  226. return '选择课程';
  227. break;
  228. case Codes.CODE_SUPPORT:
  229. return '选择配套';
  230. break;
  231. case Codes.CODE_PACKAGE:
  232. return '选择课程包';
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. render() {
  239. const formItemLayout = {
  240. labelCol: {
  241. span: 7,
  242. },
  243. wrapperCol: {
  244. span: 12,
  245. },
  246. };
  247. const submitFormLayout = {
  248. wrapperCol: {
  249. xs: { span: 24, offset: 0 },
  250. sm: { span: 10, offset: 7 },
  251. },
  252. };
  253. const {
  254. form: {
  255. getFieldDecorator
  256. },
  257. item = {},
  258. course,
  259. merchant,
  260. support,
  261. combo
  262. } = this.props;
  263. const {
  264. radioType,
  265. selectItem,
  266. courseModalVisible,
  267. supportModalVisible,
  268. packageModalVisible,
  269. } = this.state;
  270. return (
  271. <PageHeaderLayout>
  272. <Card>
  273. <Form layout="horizontal" onSubmit={this.handlePageSubmit}>
  274. <FormItem label="选择渠道:" {...formItemLayout}>
  275. {getFieldDecorator('merchantId', {
  276. rules: [{ required: true, type: 'string', message: '此项为必选项!' }],
  277. })(<Select placeholder="请选择" style={{ width: '43%' }}>{merchant.list.map(item => <Option value={item.id} key={item.id}>{item.name}</Option>)}</Select>)}
  278. </FormItem>
  279. <FormItem label="产品类型:" {...formItemLayout}>
  280. {getFieldDecorator('type', {
  281. rules: [{ required: true, type: 'string', message: '请选择产品类型!' }],
  282. initialValue: radioType,
  283. })(
  284. <RadioGroup onChange={this.handleRadioOnChange}>
  285. {Object.keys(productType).map(key =>
  286. <Radio value={key} key={key}>{productType[key]}</Radio>)
  287. }
  288. </RadioGroup>
  289. )}
  290. </FormItem>
  291. <FormItem label={this.renderLabelName()} {...formItemLayout}>
  292. <Button size="small" type="primary" icon="select" onClick={() => this.handleModalShow(radioType)}>选择</Button>
  293. </FormItem>
  294. <FormItem {...submitFormLayout}>
  295. {selectItem ?
  296. <Card
  297. title={selectItem.name}
  298. hoverable
  299. cover={<img alt="暂无封面图片" src={selectItem.coverUrl} />}
  300. style={{ width: '50%' }}
  301. >
  302. </Card> :
  303. <Button type="dashed" className={styles.newButton}>未选择</Button>
  304. }
  305. </FormItem>
  306. <FormItem label="上架状态:" {...formItemLayout}>
  307. {getFieldDecorator('status', {
  308. valuePropsName: 'checked',
  309. })(<Switch defaultChecked={false} checkedChildren="出售" unCheckedChildren="下架" />)}
  310. </FormItem>
  311. <FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
  312. <Button onClick={this.handlePageCancel}>取消</Button>
  313. <Button type="primary" style={{ marginLeft: 35 }} htmlType="submit">提交</Button>
  314. </FormItem>
  315. </Form>
  316. {/*查询课程的模态选择框*/}
  317. <CourseSelectModal
  318. rowKeyName="id"
  319. modalVisible={courseModalVisible}
  320. style={{ top: 30 }}
  321. onOk={this.handleModalOk}
  322. onCancel={this.handleModalCancel}
  323. onSearch={this.handleModalSearch}
  324. fsTableDataSource={course.list || []}
  325. fsTableLoading={course.listLoading}
  326. fsTablePagination={course.pagination}
  327. fsTableOnChange={this.handleModalTableChange}
  328. />
  329. {/*查询配套的模态选择框*/}
  330. <SupportSelectModal
  331. rowKeyName="id"
  332. modalVisible={supportModalVisible}
  333. style={{ top: 30 }}
  334. onOk={this.handleModalOk}
  335. onCancel={this.handleModalCancel}
  336. onSearch={this.handleModalSearch}
  337. fsTableDataSource={support.list || []}
  338. fsTableLoading={support.listLoading}
  339. fsTablePagination={support.pagination}
  340. fsTableOnChange={this.handleModalTableChange}
  341. />
  342. {/*查询课程包的模态选择框*/}
  343. <PackageSelectModal
  344. rowKeyName="id"
  345. modalVisible={packageModalVisible}
  346. style={{ top: 30 }}
  347. onOk={this.handleModalOk}
  348. onCancel={this.handleModalCancel}
  349. onSearch={this.handleModalSearch}
  350. fsTableDataSource={combo.list || []}
  351. fsTableLoading={combo.listLoading}
  352. fsTablePagination={combo.pagination}
  353. fsTableOnChange={this.handleModalTableChange}
  354. />
  355. </Card>
  356. </PageHeaderLayout>
  357. );
  358. }
  359. }