page.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import React from 'react';
  2. import { Tabs, Form, Row, Col, Input, InputNumber, Button } from 'antd';
  3. import './index.less';
  4. import Editor from '@src/components/Editor';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. import { flattenObject, formatFormError } from '@src/services/Tools';
  8. import { asyncSMessage } from '@src/services/AsyncTools';
  9. import { System } from '../../../stores/system';
  10. export default class extends Page {
  11. constructor(props) {
  12. super(props);
  13. this.state.tab = 'sentence';
  14. }
  15. initData() {
  16. this.refresh(this.state.tab);
  17. }
  18. refresh(tab) {
  19. if (tab === 'sentence') {
  20. return this.refreshSentence();
  21. }
  22. if (tab === 'prepare') {
  23. return this.refreshPrepare();
  24. }
  25. return Promise.reject();
  26. }
  27. refreshSentence() {
  28. return System.getSentenceInfo().then(result => {
  29. this.setState({ sentence: result || {} });
  30. const { form } = this.props;
  31. form.setFieldsValue(flattenObject(result, 'sentence'));
  32. });
  33. }
  34. refreshPrepare() {
  35. return System.getPrepareInfo().then(result => {
  36. this.setState({ prepare: result || {} });
  37. const { form } = this.props;
  38. form.setFieldsValue(flattenObject(result, 'prepare'));
  39. });
  40. }
  41. changeMapValue(field, second, index, key, value) {
  42. const data = this.state[field] || {};
  43. data[second] = data[second] || [];
  44. data[second][index] = data[second][index] || {};
  45. data[second][index][key] = value;
  46. this.setState({ [field]: data });
  47. }
  48. changeValue(field, key, value) {
  49. const { data } = this.state;
  50. data[field] = data[field] || {};
  51. data[field][key] = value;
  52. this.setState({ data });
  53. }
  54. submit(tab) {
  55. let handler = Promise.reject();
  56. if (tab === 'sentence') {
  57. handler = this.submitSentence();
  58. }
  59. if (tab === 'prepare') {
  60. handler = this.submitPrepare();
  61. }
  62. handler.then(() => {
  63. asyncSMessage('保存成功');
  64. });
  65. }
  66. submitSentence() {
  67. const { form } = this.props;
  68. return new Promise((resolve, reject) => {
  69. form.validateFields(['sentence'], (err, values) => {
  70. if (err) {
  71. reject(err);
  72. }
  73. const data = values.sentence;
  74. return System.setSentenceInfo(data)
  75. .then(() => {
  76. resolve();
  77. }).catch((e) => {
  78. form.setFields(formatFormError(data, e.result));
  79. reject(e);
  80. });
  81. });
  82. });
  83. }
  84. submitPrepare() {
  85. const { form } = this.props;
  86. return new Promise((resolve, reject) => {
  87. form.validateFields(['prepare'], (err, values) => {
  88. if (err) {
  89. reject(err);
  90. }
  91. const data = values.prepare;
  92. return System.setPrepareInfo(data)
  93. .then(() => {
  94. resolve();
  95. }).catch((e) => {
  96. form.setFields(formatFormError(data, e.result));
  97. reject(e);
  98. });
  99. });
  100. });
  101. }
  102. renderSentence() {
  103. const { getFieldDecorator } = this.props.form;
  104. return <Form>
  105. <Row>
  106. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='购买链接'>
  107. {getFieldDecorator('sentence.link', {
  108. rules: [
  109. { required: true, message: '输入购买链接' },
  110. ],
  111. })(
  112. <Input placeholder='请输入购买链接' onChange={(e) => {
  113. this.changeValue('sentence', 'link', e.target.value);
  114. }} style={{ width: '200px' }} />,
  115. )}
  116. </Form.Item>
  117. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='购买价格'>
  118. {getFieldDecorator('sentence.price', {
  119. rules: [
  120. { required: true, message: '输入购买价格' },
  121. ],
  122. })(
  123. <InputNumber placeholder='请输入购买价格' onChange={(value) => {
  124. this.changeValue('sentence', 'price', value);
  125. }} style={{ width: '200px' }} />,
  126. )}
  127. </Form.Item>
  128. <Form.Item label='Code说明'>
  129. {getFieldDecorator('sentence.detail', {
  130. })(
  131. <Editor placeholder='请输入内容' onUpload={(file) => System.uploadImage(file)} />,
  132. )}
  133. </Form.Item>
  134. </Row>
  135. </Form>;
  136. }
  137. renderPrepare() {
  138. const { getFieldDecorator } = this.props.form;
  139. return <Form>
  140. <Row>
  141. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='访问链接'>
  142. {getFieldDecorator('prepare.link', {
  143. rules: [
  144. { required: true, message: '输入访问链接' },
  145. ],
  146. })(
  147. <Input placeholder='请输入访问链接' onChange={(e) => {
  148. this.changeValue('prepare', 'link', e.target.value);
  149. }} style={{ width: '200px' }} />,
  150. )}
  151. </Form.Item>
  152. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='访问标题'>
  153. {getFieldDecorator('prepare.title', {
  154. rules: [
  155. { required: true, message: '输入访问标题' },
  156. ],
  157. })(
  158. <Input placeholder='请输入访问标题' onChange={(value) => {
  159. this.changeValue('prepare', 'title', value);
  160. }} style={{ width: '200px' }} />,
  161. )}
  162. </Form.Item>
  163. </Row>
  164. </Form>;
  165. }
  166. renderView() {
  167. const { tab } = this.state;
  168. return <Block><Tabs activeKey={tab} onChange={(value) => {
  169. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  170. this.refresh(value);
  171. }}>
  172. <Tabs.TabPane tab="长难句购买" key="sentence">
  173. {this.renderSentence()}
  174. </Tabs.TabPane>
  175. <Tabs.TabPane tab="备考时间" key="prepare">
  176. {this.renderPrepare()}
  177. </Tabs.TabPane>
  178. </Tabs>
  179. <Row type="flex" justify="center">
  180. <Col>
  181. <Button type="primary" onClick={() => {
  182. this.submit(tab);
  183. }}>保存</Button>
  184. </Col>
  185. </Row>
  186. </Block>;
  187. }
  188. }