page.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import React from 'react';
  2. import moment from 'moment';
  3. import { Form, Input, Button, Row, Col, InputNumber, DatePicker } from 'antd';
  4. import './index.less';
  5. import Editor from '@src/components/Editor';
  6. import Page from '@src/containers/Page';
  7. import Block from '@src/components/Block';
  8. import Select from '@src/components/Select';
  9. // import FileUpload from '@src/components/FileUpload';
  10. import { formatFormError, generateSearch } from '@src/services/Tools';
  11. import { asyncSMessage } from '@src/services/AsyncTools';
  12. import { PrepareStatus, ExperienceScoreRange, ExperiencePercent } from '../../../../Constant';
  13. import { Course } from '../../../stores/course';
  14. import { User } from '../../../stores/user';
  15. const [minScore, maxScore, step] = ExperienceScoreRange;
  16. let tmp = minScore;
  17. const ExperienceScore = [];
  18. while (tmp <= maxScore) {
  19. ExperienceScore.push({ label: `${tmp}`, value: tmp });
  20. tmp += step;
  21. }
  22. export default class extends Page {
  23. initState() {
  24. return { showNickname: true };
  25. }
  26. initData() {
  27. const { id } = this.params;
  28. const { form } = this.props;
  29. let handler;
  30. if (id) {
  31. handler = Course.getExperience({ id });
  32. } else {
  33. handler = Promise.resolve({});
  34. }
  35. handler
  36. .then(result => {
  37. if (result.userId) {
  38. this.setState({ showNickname: false });
  39. }
  40. result.experienceTime = moment(result.experienceTime);
  41. generateSearch('userId', {
  42. allowClear: true,
  43. }, this, (search) => {
  44. return User.list(search);
  45. }, (row) => {
  46. return {
  47. title: `${row.nickname}(${row.mobile})`,
  48. value: row.id,
  49. };
  50. }, result.userId, null);
  51. form.setFieldsValue(result);
  52. });
  53. }
  54. submit() {
  55. const { form } = this.props;
  56. form.validateFields((err) => {
  57. if (!err) {
  58. const data = form.getFieldsValue();
  59. let handler;
  60. data.description = (data.content || '').replace(/<[^>]+>/g, '').substr(0, 200);
  61. if (data.id) {
  62. handler = Course.editExperience(data);
  63. } else {
  64. handler = Course.addExperience(data);
  65. }
  66. handler.then(() => {
  67. asyncSMessage('保存成功');
  68. goBack();
  69. }).catch((e) => {
  70. if (e.result) form.setFields(formatFormError(data, e.result));
  71. });
  72. }
  73. });
  74. }
  75. renderBase() {
  76. const { getFieldDecorator } = this.props.form;
  77. const { showNickname } = this.state;
  78. return <Block>
  79. <Form>
  80. {getFieldDecorator('id')(<input hidden />)}
  81. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='题目'>
  82. {getFieldDecorator('title', {
  83. rules: [
  84. { required: true, message: '请输入名称' },
  85. ],
  86. })(<Input placeholder='请输入名称' />)}
  87. </Form.Item>
  88. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='作者'>
  89. {getFieldDecorator('userId', {
  90. rules: [
  91. { required: !showNickname, message: '请选择作者' },
  92. ],
  93. })(
  94. <Select {...this.state.userId} onChange={(value) => {
  95. this.setState({ showNickname: !value });
  96. }} placeholder='请选择作者' />,
  97. )}
  98. </Form.Item>
  99. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='昵称'>
  100. {getFieldDecorator('nickname', {
  101. rules: [
  102. { required: showNickname, message: '请输入昵称' },
  103. ],
  104. })(
  105. <Input disabled={!showNickname} placeholder='输入昵称' />,
  106. )}
  107. </Form.Item>
  108. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='作者信息'>
  109. <Row>
  110. <Col span={12}>
  111. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 14 }} label='身份'>
  112. {getFieldDecorator('prepareStatus', {
  113. rules: [{
  114. required: true, message: '请选择',
  115. }],
  116. })(
  117. <Select select={PrepareStatus} placeholder='身份' />,
  118. )}
  119. </Form.Item>
  120. </Col>
  121. <Col span={12}>
  122. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 14 }} label='备考周期'>
  123. {getFieldDecorator('experienceDay', {
  124. rules: [{
  125. required: true, message: '请输入备考天数',
  126. }],
  127. })(
  128. <InputNumber placeholder='备考周期: 天' />,
  129. )}
  130. </Form.Item>
  131. </Col>
  132. <Col span={12}>
  133. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 14 }} label='分手成绩'>
  134. {getFieldDecorator('experienceScore', {
  135. rules: [{
  136. required: true, message: '请选择',
  137. }],
  138. })(
  139. <Select select={ExperienceScore} placeholder='分手成绩' />,
  140. )}
  141. </Form.Item>
  142. </Col>
  143. <Col span={12}>
  144. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 14 }} label='提分范围'>
  145. {getFieldDecorator('experiencePercent', {
  146. rules: [{
  147. required: true, message: '请选择',
  148. }],
  149. })(
  150. <Select select={ExperiencePercent} placeholder='提分范围' />,
  151. )}
  152. </Form.Item>
  153. </Col>
  154. <Col span={12}>
  155. <Form.Item labelCol={{ span: 8 }} wrapperCol={{ span: 14 }} label='考试时间'>
  156. {getFieldDecorator('experienceTime', {
  157. rules: [{
  158. required: true, message: '请选择',
  159. }],
  160. })(
  161. <DatePicker placeholder='考试时间' />,
  162. )}
  163. </Form.Item>
  164. </Col>
  165. </Row>
  166. </Form.Item>
  167. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='问卷链接'>
  168. {getFieldDecorator('link', {
  169. })(
  170. <Input placeholder='输入内容' />,
  171. )}
  172. </Form.Item>
  173. <Form.Item label='正文'>
  174. {getFieldDecorator('content', {
  175. })(
  176. <Editor placeholder='输入内容' />,
  177. )}
  178. </Form.Item>
  179. </Form>
  180. </Block>;
  181. }
  182. renderView() {
  183. return <div flex>
  184. {this.renderBase()}
  185. <Row type="flex" justify="center">
  186. <Col>
  187. <Button type="primary" onClick={() => {
  188. this.submit();
  189. }}>保存</Button>
  190. </Col>
  191. </Row>
  192. </div>;
  193. }
  194. }