page.js 15 KB

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