page.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import React from 'react';
  2. import { Tabs, Form, Row, Col, Input, InputNumber, Button, Upload, Icon } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import { flattenObject } from '@src/services/Tools';
  7. import { asyncSMessage } from '@src/services/AsyncTools';
  8. import { ServiceParamMap } from '../../../../Constant';
  9. import { System } from '../../../stores/system';
  10. export default class extends Page {
  11. constructor(props) {
  12. super(props);
  13. this.state.tab = 'qx_cat';
  14. this.vipList = ServiceParamMap.vip;
  15. }
  16. initData() {
  17. this.refresh(this.state.tab);
  18. }
  19. refresh(tab) {
  20. if (tab === 'qx_cat') {
  21. return this.refreshQxCat();
  22. }
  23. if (tab === 'textbook') {
  24. return this.refreshTextbook();
  25. }
  26. if (tab === 'vip') {
  27. return this.refreshVip();
  28. }
  29. return Promise.reject();
  30. }
  31. refreshQxCat() {
  32. return System.getServiceQxCat().then(result => {
  33. this.setState({ qx_cat: result || {} });
  34. const { form } = this.props;
  35. form.setFieldsValue(flattenObject(result, 'qx_cat'));
  36. });
  37. }
  38. refreshTextbook() {
  39. return System.getServiceTextbook().then(result => {
  40. this.setState({ textbook: result || {} });
  41. const { form } = this.props;
  42. form.setFieldsValue(flattenObject(result, 'textbook'));
  43. });
  44. }
  45. refreshVip() {
  46. return System.getServiceVip().then(result => {
  47. this.setState({ vip: result || {} });
  48. const { form } = this.props;
  49. form.setFieldsValue(flattenObject(result, 'vip'));
  50. });
  51. }
  52. changeMapValue(field, second, index, key, value) {
  53. const data = this.state[field] || {};
  54. data[second] = data[second] || [];
  55. data[second][index] = data[second][index] || {};
  56. data[second][index][key] = value;
  57. this.setState({ [field]: data });
  58. }
  59. changeValue(field, key, value) {
  60. const data = this.state[field] || {};
  61. data[key] = value;
  62. this.setStaet({ [field]: data });
  63. }
  64. submit(tab) {
  65. let handler;
  66. if (tab === 'qx_cat') {
  67. handler = this.submitQxCat();
  68. }
  69. if (tab === 'textbook') {
  70. handler = this.submitTextbook();
  71. }
  72. if (tab === 'vip') {
  73. handler = this.submitVip();
  74. }
  75. handler.then(() => {
  76. asyncSMessage('保存成功');
  77. });
  78. }
  79. submitQxCat() {
  80. const { qx_cat } = this.state;
  81. return System.setServiceQxCat(qx_cat);
  82. }
  83. submitTextbook() {
  84. const { textbook } = this.state;
  85. return System.setServiceTextbook(textbook);
  86. }
  87. submitVip() {
  88. const { vip } = this.state;
  89. return System.setServiceVip(vip);
  90. }
  91. renderQxCat() {
  92. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  93. const image = getFieldValue('qx_cat.image') || null;
  94. return <Form>
  95. <Row>
  96. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='商品价格'>
  97. {getFieldDecorator('qx_cat.package[0].price', {
  98. rules: [
  99. { required: true, message: '输入千行Cat价格' },
  100. ],
  101. })(
  102. <InputNumber placeholder='请输入千行Cat价格' onChange={(value) => {
  103. this.changeMapValue('qx_cat', 'package', 0, 'price', value);
  104. }} style={{ width: '200px' }} />,
  105. )}
  106. </Form.Item>
  107. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='服务名称'>
  108. {getFieldDecorator('qx_cat.package[0].title', {
  109. rules: [
  110. { required: true, message: '输入千行Cat名称' },
  111. ],
  112. })(
  113. <Input placeholder='请输入千行Cat名称' onChange={(e) => {
  114. this.changeMapValue('qx_cat', 'package', 0, 'title', e.target.value);
  115. }} style={{ width: '200px' }} />,
  116. )}
  117. </Form.Item>
  118. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='服务简介'>
  119. {getFieldDecorator('qx_cat.package[0].description', {
  120. rules: [
  121. { required: true, message: '输入千行Cat服务简介' },
  122. ],
  123. })(
  124. <Input placeholder='请输入千行Cat服务简介' onChange={(e) => {
  125. this.changeMapValue('qx_cat', 'package', 0, 'description', e.target.value);
  126. }} style={{ width: '200px' }} />,
  127. )}
  128. </Form.Item>
  129. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='退款政策'>
  130. {getFieldDecorator('qx_cat.package[0].refund_policy', {
  131. rules: [
  132. { required: true, message: '输入千行Cat退款政策' },
  133. ],
  134. })(
  135. <Input placeholder='请输入千行Cat退款政策' onChange={(e) => {
  136. this.changeMapValue('qx_cat', 'package', 0, 'refund_policy', e.target.value);
  137. }} style={{ width: '200px' }} />,
  138. )}
  139. </Form.Item>
  140. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='版权说明'>
  141. {getFieldDecorator('qx_cat.package[0].copyright_notes', {
  142. rules: [
  143. { required: true, message: '输入千行Cat版权说明' },
  144. ],
  145. })(
  146. <Input placeholder='请输入千行Cat版权说明' onChange={(e) => {
  147. this.changeMapValue('qx_cat', 'package', 0, 'copyright_notes', e.target.value);
  148. }} style={{ width: '200px' }} />,
  149. )}
  150. </Form.Item>
  151. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='商品图片'>
  152. {getFieldDecorator('qx_cat.image', {
  153. rules: [
  154. { required: true, message: '上传图片' },
  155. ],
  156. })(
  157. <Upload
  158. listType="picture-card"
  159. showUploadList={false}
  160. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  161. setFieldsValue({ 'qx_cat.image': result.url });
  162. this.changeValue('qx_cat', 'image', result.url);
  163. return Promise.reject();
  164. })}
  165. >
  166. {image ? <img src={image} alt="avatar" /> : <div>
  167. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  168. <div className="ant-upload-text">Upload</div>
  169. </div>}
  170. </Upload>,
  171. )}
  172. </Form.Item>
  173. </Row>
  174. </Form>;
  175. }
  176. renderTextbook() {
  177. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  178. const image = getFieldValue('textbook.image') || null;
  179. return <Form>
  180. <Row>
  181. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='商品图片'>
  182. {getFieldDecorator('textbook.image', {
  183. rules: [
  184. { required: true, message: '上传图片' },
  185. ],
  186. })(
  187. <Upload
  188. listType="picture-card"
  189. showUploadList={false}
  190. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  191. setFieldsValue({ 'textbook.image': result.url });
  192. this.changeValue('textbook', 'image', result.url);
  193. return Promise.reject();
  194. })}
  195. >
  196. {image ? <img src={image} alt="avatar" /> : <div>
  197. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  198. <div className="ant-upload-text">Upload</div>
  199. </div>}
  200. </Upload>,
  201. )}
  202. </Form.Item>
  203. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='商品价格'>
  204. {getFieldDecorator('textbook.package[0].price', {
  205. rules: [
  206. { required: true, message: '输入数学机经价格' },
  207. ],
  208. })(
  209. <InputNumber placeholder='请输入数学机经价格' onChange={(value) => {
  210. this.changeMapValue('textbook', 'package', 0, 'price', value);
  211. }} style={{ width: '200px' }} />,
  212. )}
  213. </Form.Item>
  214. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='服务名称'>
  215. {getFieldDecorator('textbook.package[0].title', {
  216. rules: [
  217. { required: true, message: '输入数学机经名称' },
  218. ],
  219. })(
  220. <Input placeholder='请输入数学机经名称' onChange={(e) => {
  221. this.changeMapValue('textbook', 'package', 0, 'title', e.target.value);
  222. }} style={{ width: '200px' }} />,
  223. )}
  224. </Form.Item>
  225. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='服务简介'>
  226. {getFieldDecorator('textbook.package[0].description', {
  227. rules: [
  228. { required: true, message: '输入数学机经服务简介' },
  229. ],
  230. })(
  231. <Input placeholder='请输入数学机经服务简介' onChange={(e) => {
  232. this.changeMapValue('textbook', 'package', 0, 'description', e.target.value);
  233. }} style={{ width: '200px' }} />,
  234. )}
  235. </Form.Item>
  236. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='有效期说明'>
  237. {getFieldDecorator('textbook.package[0].expire_info', {
  238. rules: [
  239. { required: true, message: '输入数学机经有效期说明' },
  240. ],
  241. })(
  242. <Input placeholder='请输入数学机经有效期说明' onChange={(e) => {
  243. this.changeMapValue('textbook', 'package', 0, 'expire_info', e.target.value);
  244. }} style={{ width: '200px' }} />,
  245. )}
  246. </Form.Item>
  247. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='退款政策'>
  248. {getFieldDecorator('textbook.package[0].refund_policy', {
  249. rules: [
  250. { required: true, message: '输入数学机经退款政策' },
  251. ],
  252. })(
  253. <Input placeholder='请输入数学机经退款政策' onChange={(e) => {
  254. this.changeMapValue('textbook', 'package', 0, 'refund_policy', e.target.value);
  255. }} style={{ width: '200px' }} />,
  256. )}
  257. </Form.Item>
  258. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='版权说明'>
  259. {getFieldDecorator('textbook.package[0].copyright_notes', {
  260. rules: [
  261. { required: true, message: '输入数学机经版权说明' },
  262. ],
  263. })(
  264. <Input placeholder='请输入数学机经版权说明' onChange={(e) => {
  265. this.changeMapValue('textbook', 'package', 0, 'copyright_notes', e.target.value);
  266. }} style={{ width: '200px' }} />,
  267. )}
  268. </Form.Item>
  269. </Row>
  270. </Form>;
  271. }
  272. renderVip() {
  273. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  274. const image = getFieldValue('vip.image') || null;
  275. return <Form>
  276. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='商品图片'>
  277. {getFieldDecorator('vip.image', {
  278. rules: [
  279. { required: true, message: '上传图片' },
  280. ],
  281. })(
  282. <Upload
  283. listType="picture-card"
  284. showUploadList={false}
  285. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  286. setFieldsValue({ 'vip.image': result.url });
  287. this.changeValue('vip', 'image', result.url);
  288. return Promise.reject();
  289. })}
  290. >
  291. {image ? <img src={image} alt="avatar" /> : <div>
  292. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  293. <div className="ant-upload-text">Upload</div>
  294. </div>}
  295. </Upload>,
  296. )}
  297. </Form.Item>
  298. <Row>
  299. {this.vipList.map((row, index) => {
  300. return <Col span={12}>
  301. <h1>{row.label}</h1>
  302. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='商品价格'>
  303. {getFieldDecorator(`vip.package[${index}].price`, {
  304. rules: [
  305. { required: true, message: '输入价格' },
  306. ],
  307. })(
  308. <InputNumber placeholder={'输入价格'} onChange={(value) => {
  309. this.changeMapValue('vip', 'package', index, 'price', value);
  310. }} style={{ width: '200px' }} />,
  311. )}
  312. </Form.Item>
  313. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='服务名称'>
  314. {getFieldDecorator(`vip.package[${index}].title`, {
  315. rules: [
  316. { required: true, message: '输入名称' },
  317. ],
  318. })(
  319. <Input placeholder={'输入名称'} onChange={(e) => {
  320. this.changeMapValue('vip', 'package', index, 'title', e.target.value);
  321. }} style={{ width: '200px' }} />,
  322. )}
  323. </Form.Item>
  324. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='服务简介'>
  325. {getFieldDecorator(`vip.package[${index}].description`, {
  326. rules: [
  327. { required: true, message: '输入服务简介' },
  328. ],
  329. })(
  330. <Input placeholder='请输入服务简介' onChange={(e) => {
  331. this.changeMapValue('vip', 'package', index, 'description', e.target.value);
  332. }} style={{ width: '200px' }} />,
  333. )}
  334. </Form.Item>
  335. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='有效期说明'>
  336. {getFieldDecorator(`vip.package[${index}].expire_info`, {
  337. rules: [
  338. { required: true, message: '输入有效期说明' },
  339. ],
  340. })(
  341. <Input placeholder='请输入有效期说明' onChange={(e) => {
  342. this.changeMapValue('vip', 'package', index, 'expire_info', e.target.value);
  343. }} style={{ width: '200px' }} />,
  344. )}
  345. </Form.Item>
  346. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='退款政策'>
  347. {getFieldDecorator(`vip.package[${index}].refund_policy`, {
  348. rules: [
  349. { required: true, message: '输入退款政策' },
  350. ],
  351. })(
  352. <Input placeholder='请输入退款政策' onChange={(e) => {
  353. this.changeMapValue('vip', 'package', index, 'refund_policy', e.target.value);
  354. }} style={{ width: '200px' }} />,
  355. )}
  356. </Form.Item>
  357. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='版权说明'>
  358. {getFieldDecorator(`vip.package[${index}].copyright_notes`, {
  359. rules: [
  360. { required: true, message: '输入版权说明' },
  361. ],
  362. })(
  363. <Input placeholder='请输入版权说明' onChange={(e) => {
  364. this.changeMapValue('vip', 'package', index, 'copyright_notes', e.target.value);
  365. }} style={{ width: '200px' }} />,
  366. )}
  367. </Form.Item>
  368. </Col>;
  369. })}
  370. </Row>
  371. </Form>;
  372. }
  373. renderView() {
  374. const { tab } = this.state;
  375. return <Block><Tabs activeKey={tab} onChange={(value) => {
  376. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  377. this.refresh(value);
  378. }}>
  379. <Tabs.TabPane tab="千行Cat" key="qx_cat">
  380. {this.renderQxCat()}
  381. </Tabs.TabPane>
  382. <Tabs.TabPane tab="数学机经" key="textbook">
  383. {this.renderTextbook()}
  384. </Tabs.TabPane>
  385. <Tabs.TabPane tab="Vip" key="vip">
  386. {this.renderVip()}
  387. </Tabs.TabPane>
  388. </Tabs>
  389. <Row type="flex" justify="center">
  390. <Col>
  391. <Button type="primary" onClick={() => {
  392. this.submit(tab);
  393. }}>保存</Button>
  394. </Col>
  395. </Row>
  396. </Block>;
  397. }
  398. }