page.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. return Promise.reject();
  168. })}
  169. >
  170. {image ? <img src={image} alt="avatar" /> : <div>
  171. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  172. <div className="ant-upload-text">Upload</div>
  173. </div>}
  174. </Upload>,
  175. )}
  176. </Form.Item>
  177. </Row>
  178. </Form>;
  179. }
  180. renderTextbook() {
  181. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  182. const image = getFieldValue('textbook.image') || null;
  183. return <Form>
  184. <Row>
  185. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='商品图片'>
  186. {getFieldDecorator('textbook.image', {
  187. rules: [
  188. { required: true, message: '上传图片' },
  189. ],
  190. })(
  191. <Upload
  192. listType="picture-card"
  193. showUploadList={false}
  194. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  195. setFieldsValue({ 'textbook.image': result });
  196. return Promise.reject();
  197. })}
  198. >
  199. {image ? <img src={image} alt="avatar" /> : <div>
  200. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  201. <div className="ant-upload-text">Upload</div>
  202. </div>}
  203. </Upload>,
  204. )}
  205. </Form.Item>
  206. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='商品价格'>
  207. {getFieldDecorator('textbook[0].price', {
  208. rules: [
  209. { required: true, message: '输入数学机经价格' },
  210. ],
  211. })(
  212. <InputNumber placeholder='请输入数学机经价格' onChange={(value) => {
  213. this.changeMapValue('textbook', 0, 'price', value);
  214. }} style={{ width: '200px' }} />,
  215. )}
  216. </Form.Item>
  217. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='服务名称'>
  218. {getFieldDecorator('textbook[0].title', {
  219. rules: [
  220. { required: true, message: '输入数学机经名称' },
  221. ],
  222. })(
  223. <Input placeholder='请输入数学机经名称' onChange={(value) => {
  224. this.changeMapValue('textbook', 0, 'title', value);
  225. }} style={{ width: '200px' }} />,
  226. )}
  227. </Form.Item>
  228. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='服务简介'>
  229. {getFieldDecorator('textbook[0].description', {
  230. rules: [
  231. { required: true, message: '输入数学机经服务简介' },
  232. ],
  233. })(
  234. <Input placeholder='请输入数学机经服务简介' onChange={(value) => {
  235. this.changeMapValue('textbook', 0, 'description', value);
  236. }} style={{ width: '200px' }} />,
  237. )}
  238. </Form.Item>
  239. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='有效期说明'>
  240. {getFieldDecorator('textbook[0].expire_info', {
  241. rules: [
  242. { required: true, message: '输入数学机经有效期说明' },
  243. ],
  244. })(
  245. <Input placeholder='请输入数学机经有效期说明' onChange={(value) => {
  246. this.changeMapValue('textbook', 0, 'expire_info', value);
  247. }} style={{ width: '200px' }} />,
  248. )}
  249. </Form.Item>
  250. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='退款政策'>
  251. {getFieldDecorator('textbook[0].refund_policy', {
  252. rules: [
  253. { required: true, message: '输入数学机经退款政策' },
  254. ],
  255. })(
  256. <Input placeholder='请输入数学机经退款政策' onChange={(value) => {
  257. this.changeMapValue('textbook', 0, 'refund_policy', value);
  258. }} style={{ width: '200px' }} />,
  259. )}
  260. </Form.Item>
  261. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='版权说明'>
  262. {getFieldDecorator('textbook[0].copyright_notes', {
  263. rules: [
  264. { required: true, message: '输入数学机经版权说明' },
  265. ],
  266. })(
  267. <Input placeholder='请输入数学机经版权说明' onChange={(value) => {
  268. this.changeMapValue('textbook', 0, 'copyright_notes', value);
  269. }} style={{ width: '200px' }} />,
  270. )}
  271. </Form.Item>
  272. </Row>
  273. </Form>;
  274. }
  275. renderVip() {
  276. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  277. const image = getFieldValue('vip.image') || null;
  278. return <Form>
  279. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='商品图片'>
  280. {getFieldDecorator('vip.image', {
  281. rules: [
  282. { required: true, message: '上传图片' },
  283. ],
  284. })(
  285. <Upload
  286. listType="picture-card"
  287. showUploadList={false}
  288. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  289. setFieldsValue({ 'vip.image': result });
  290. return Promise.reject();
  291. })}
  292. >
  293. {image ? <img src={image} alt="avatar" /> : <div>
  294. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  295. <div className="ant-upload-text">Upload</div>
  296. </div>}
  297. </Upload>,
  298. )}
  299. </Form.Item>
  300. <Row>
  301. {this.vipList.map((row, index) => {
  302. return <Col span={12}>
  303. <h1>{row}</h1>
  304. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='商品价格'>
  305. {getFieldDecorator(`vip[${index}].price`, {
  306. rules: [
  307. { required: true, message: '输入价格' },
  308. ],
  309. })(
  310. <InputNumber placeholder={'输入价格'} onChange={(value) => {
  311. this.changeMapValue('vip', index, 'price', value);
  312. }} style={{ width: '200px' }} />,
  313. )}
  314. </Form.Item>
  315. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='服务名称'>
  316. {getFieldDecorator(`vip[${index}].title`, {
  317. rules: [
  318. { required: true, message: '输入名称' },
  319. ],
  320. })(
  321. <Input placeholder={'输入名称'} onChange={(value) => {
  322. this.changeMapValue('vip', index, 'title', value);
  323. }} style={{ width: '200px' }} />,
  324. )}
  325. </Form.Item>
  326. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='服务简介'>
  327. {getFieldDecorator(`vip[${index}].description`, {
  328. rules: [
  329. { required: true, message: '输入服务简介' },
  330. ],
  331. })(
  332. <Input placeholder='请输入服务简介' onChange={(value) => {
  333. this.changeMapValue('vip', index, 'description', value);
  334. }} style={{ width: '200px' }} />,
  335. )}
  336. </Form.Item>
  337. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='有效期说明'>
  338. {getFieldDecorator(`vip[${index}].expire_info`, {
  339. rules: [
  340. { required: true, message: '输入有效期说明' },
  341. ],
  342. })(
  343. <Input placeholder='请输入有效期说明' onChange={(value) => {
  344. this.changeMapValue('vip', index, 'expire_info', value);
  345. }} style={{ width: '200px' }} />,
  346. )}
  347. </Form.Item>
  348. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='退款政策'>
  349. {getFieldDecorator(`vip[${index}].refund_policy`, {
  350. rules: [
  351. { required: true, message: '输入退款政策' },
  352. ],
  353. })(
  354. <Input placeholder='请输入退款政策' onChange={(value) => {
  355. this.changeMapValue('vip', index, 'refund_policy', value);
  356. }} style={{ width: '200px' }} />,
  357. )}
  358. </Form.Item>
  359. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} label='版权说明'>
  360. {getFieldDecorator(`vip[${index}].copyright_notes`, {
  361. rules: [
  362. { required: true, message: '输入版权说明' },
  363. ],
  364. })(
  365. <Input placeholder='请输入版权说明' onChange={(value) => {
  366. this.changeMapValue('vip', index, 'copyright_notes', value);
  367. }} style={{ width: '200px' }} />,
  368. )}
  369. </Form.Item>
  370. </Col>;
  371. })}
  372. </Row>
  373. </Form>;
  374. }
  375. renderView() {
  376. const { tab } = this.state;
  377. return <Block><Tabs activeKey={tab} onChange={(value) => {
  378. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  379. this.refresh(value);
  380. }}>
  381. <Tabs.TabPane tab="千行Cat" key="qx_cat">
  382. {this.renderQxCat()}
  383. </Tabs.TabPane>
  384. <Tabs.TabPane tab="数学机经" key="textbook">
  385. {this.renderTextbook()}
  386. </Tabs.TabPane>
  387. <Tabs.TabPane tab="Vip" key="vip">
  388. {this.renderVip()}
  389. </Tabs.TabPane>
  390. </Tabs>
  391. <Row type="flex" justify="center">
  392. <Col>
  393. <Button type="primary" onClick={() => {
  394. this.submit(tab);
  395. }}>保存</Button>
  396. </Col>
  397. </Row>
  398. </Block>;
  399. }
  400. }