page.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import React from 'react';
  2. import { Form, Input, InputNumber, Card, Icon, Button, Row, Col, Upload, Affix } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. // import FileUpload from '@src/components/FileUpload';
  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. initData() {
  12. System.getIndex().then(result => {
  13. const { form } = this.props;
  14. form.setFieldsValue(flattenObject(result));
  15. this.setState({ load: true, data: result });
  16. });
  17. }
  18. addLength(field, info) {
  19. let { data } = this.state;
  20. data = data || {};
  21. data[field] = data[field] || [];
  22. data[field].push(info);
  23. this.setState({ data });
  24. }
  25. deleteLength(field, start, length) {
  26. let { data } = this.state;
  27. data = data || {};
  28. data[field] = data[field] || [];
  29. data[field].splice(start, length);
  30. this.setState({ data });
  31. }
  32. submit() {
  33. const { form } = this.props;
  34. form.validateFields((err) => {
  35. if (!err) {
  36. const data = form.getFieldsValue();
  37. data.class = Object.keys(data.class || {}).map((key) => data.class[key]);
  38. data.activity = Object.keys(data.activity || {}).map((key) => data.activity[key]);
  39. data.evaluation = Object.keys(data.evaluation || {}).map((key) => data.evaluation[key]);
  40. System.setIndex(data)
  41. .then(() => {
  42. this.setState(data);
  43. asyncSMessage('保存成功');
  44. }).catch((e) => {
  45. form.setFields(formatFormError(data, e.result));
  46. });
  47. }
  48. });
  49. }
  50. renderPrepare() {
  51. const { getFieldDecorator } = this.props.form;
  52. return <Block>
  53. <h1>备考攻略</h1>
  54. <Form>
  55. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='自学-从零开始'>
  56. {getFieldDecorator('prepare.first', {
  57. rules: [
  58. { required: false, message: '请输入跳转地址' },
  59. ],
  60. })(
  61. <Input placeholder='请输入跳转地址' />,
  62. )}
  63. </Form.Item>
  64. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='自学-继续练习'>
  65. {getFieldDecorator('prepare.continue', {
  66. rules: [
  67. { required: false, message: '请输入跳转地址' },
  68. ],
  69. })(
  70. <Input placeholder='请输入跳转地址' />,
  71. )}
  72. </Form.Item>
  73. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程-初学'>
  74. {getFieldDecorator('prepare.classJunior', {
  75. rules: [
  76. { required: false, message: '请输入跳转地址' },
  77. ],
  78. })(
  79. <Input placeholder='请输入跳转地址' />,
  80. )}
  81. </Form.Item>
  82. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程-中级'>
  83. {getFieldDecorator('prepare.classMiddle', {
  84. rules: [
  85. { required: false, message: '请输入跳转地址' },
  86. ],
  87. })(
  88. <Input placeholder='请输入跳转地址' />,
  89. )}
  90. </Form.Item>
  91. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程-高级'>
  92. {getFieldDecorator('prepare.classSenior', {
  93. rules: [
  94. { required: false, message: '请输入跳转地址' },
  95. ],
  96. })(
  97. <Input placeholder='请输入跳转地址' />,
  98. )}
  99. </Form.Item>
  100. </Form>
  101. </Block>;
  102. }
  103. renderUser() {
  104. const { getFieldDecorator } = this.props.form;
  105. return <Block>
  106. <h1>用户数据</h1>
  107. <Form>
  108. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='线下用户量'>
  109. {getFieldDecorator('user.numberOffline', {
  110. rules: [
  111. { required: false, message: '' },
  112. ],
  113. })(
  114. <InputNumber placeholder='请输入线下用户量' style={{ width: '200px' }} />,
  115. )}
  116. </Form.Item>
  117. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='700+学员数'>
  118. {getFieldDecorator('user.number700', {
  119. rules: [
  120. { required: false, message: '' },
  121. ],
  122. })(
  123. <InputNumber placeholder='请输入700+学员数' style={{ width: '200px' }} />,
  124. )}
  125. </Form.Item>
  126. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='学员平均分'>
  127. {getFieldDecorator('user.numberScore', {
  128. rules: [
  129. { required: false, message: '' },
  130. ],
  131. })(
  132. <InputNumber placeholder='请输入学员平均分' style={{ width: '200px' }} />,
  133. )}
  134. </Form.Item>
  135. </Form>
  136. </Block>;
  137. }
  138. renderClass() {
  139. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  140. const { data } = this.state;
  141. const course = data.course || [];
  142. return <Block>
  143. <h1>千行课堂</h1>
  144. <Form>
  145. <Row>
  146. {course.map((row, index) => {
  147. const image = getFieldValue(`course.${index}.image`) || null;
  148. return <Col span={7} offset={index % 3 ? 1 : 0}><Card>
  149. <Button className="delete-button" size="small" onClick={() => {
  150. this.deleteLength('course', index, 1);
  151. }}>
  152. <Icon type="delete" />
  153. </Button>
  154. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='课程名称'>
  155. {getFieldDecorator(`course.${index}.title`, {
  156. rules: [
  157. { required: true, message: '输入课程名称' },
  158. ],
  159. initialValue: row.title,
  160. })(
  161. <Input placeholder='请输入课程名称' />,
  162. )}
  163. </Form.Item>
  164. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='跳转链接'>
  165. {getFieldDecorator(`course.${index}.link`, {
  166. rules: [
  167. { required: true, message: '输入跳转链接' },
  168. ],
  169. initialValue: row.link,
  170. })(
  171. <Input placeholder='请输入跳转链接' />,
  172. )}
  173. </Form.Item>
  174. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='背景图片'>
  175. {getFieldDecorator(`course.${index}.image`, {
  176. rules: [
  177. { required: true, message: '上传图片' },
  178. ],
  179. })(
  180. <Upload
  181. listType="picture-card"
  182. showUploadList={false}
  183. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  184. setFieldsValue({ [`course.${index}.image`]: result.url });
  185. return Promise.reject();
  186. })}
  187. >
  188. {image ? <img src={image} alt="avatar" /> : <div>
  189. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  190. <div className="ant-upload-text">Upload</div>
  191. </div>}
  192. </Upload>,
  193. )}
  194. </Form.Item>
  195. </Card></Col>;
  196. })}
  197. <Col span={7} offset={course.length % 3 ? 1 : 0}>
  198. <Card className="plus" onClick={() => {
  199. this.addLength('course', { title: '', link: '', image: '' });
  200. }}>
  201. <Icon type={'plus'} />
  202. </Card>
  203. </Col>
  204. </Row>
  205. </Form>
  206. </Block >;
  207. }
  208. renderActivity() {
  209. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  210. const { data } = this.state;
  211. const activity = data.activity || [];
  212. return <Block>
  213. <h1>活动信息</h1>
  214. <Form>
  215. <Row>
  216. {activity.map((row, index) => {
  217. const image = getFieldValue(`activity.${index}.image`) || null;
  218. return <Col span={7} offset={index % 3 ? 1 : 0}><Card>
  219. <Button className="delete-button" size="small" onClick={() => {
  220. this.deleteLength('activity', index, 1);
  221. }}>
  222. <Icon type="delete" />
  223. </Button>
  224. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='跳转链接'>
  225. {getFieldDecorator(`activity.${index}.link`, {
  226. rules: [
  227. { required: true, message: '输入跳转链接' },
  228. ],
  229. initialValue: row.link,
  230. })(
  231. <Input placeholder='请输入跳转链接' />,
  232. )}
  233. </Form.Item>
  234. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='活动图片'>
  235. {getFieldDecorator(`activity.${index}.image`, {
  236. rules: [
  237. { required: true, message: '上传图片' },
  238. ],
  239. })(
  240. <Upload
  241. listType="picture-card"
  242. showUploadList={false}
  243. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  244. setFieldsValue({ [`claactivityss.${index}.image`]: result.url });
  245. return Promise.reject();
  246. })}
  247. >
  248. {image ? <img src={image} alt="avatar" /> : <div>
  249. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  250. <div className="ant-upload-text">Upload</div>
  251. </div>}
  252. </Upload>,
  253. )}
  254. </Form.Item>
  255. </Card></Col>;
  256. })}
  257. <Col span={7} offset={activity.length % 3 ? 1 : 0}>
  258. <Card className="plus" onClick={() => {
  259. this.addLength('activity', { link: '', image: '' });
  260. }}>
  261. <Icon type={'plus'} />
  262. </Card>
  263. </Col>
  264. </Row>
  265. </Form>
  266. </Block>;
  267. }
  268. renderEvaluation() {
  269. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  270. const { data } = this.state;
  271. const evaluation = data.evaluation || [];
  272. return <Block>
  273. <h1>学员评价</h1>
  274. <Form>
  275. <Row>
  276. {evaluation.map((row, index) => {
  277. const avatar = getFieldValue(`evaluation.${index}.avatar`) || null;
  278. return <Col span={7} offset={index % 3 ? 1 : 0}><Card>
  279. <Button className="delete-button" size="small" onClick={() => {
  280. this.deleteLength('evaluation', index, 1);
  281. }}>
  282. <Icon type="delete" />
  283. </Button>
  284. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='学员昵称'>
  285. {getFieldDecorator(`evaluation.${index}.nickname`, {
  286. rules: [
  287. { required: true, message: '输入学员昵称' },
  288. ],
  289. initialValue: row.nickname,
  290. })(
  291. <Input placeholder='请输入学员昵称' />,
  292. )}
  293. </Form.Item>
  294. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='学员头像'>
  295. {getFieldDecorator(`evaluation.${index}.avatar`, {
  296. rules: [
  297. { required: true, message: '上传图片' },
  298. ],
  299. })(
  300. <Upload
  301. listType="picture-card"
  302. showUploadList={false}
  303. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  304. setFieldsValue({ [`evaluation.${index}.avatar`]: result.url });
  305. return Promise.reject();
  306. })}
  307. >
  308. {avatar ? <img src={avatar} alt="avatar" /> : <div>
  309. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  310. <div className="ant-upload-text">Upload</div>
  311. </div>}
  312. </Upload>,
  313. )}
  314. </Form.Item>
  315. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='评价内容'>
  316. {getFieldDecorator(`evaluation.${index}.content`, {
  317. rules: [
  318. { required: true, message: '输入评价内容' },
  319. ],
  320. initialValue: row.content,
  321. })(
  322. <Input placeholder='请输入评价内容' />,
  323. )}
  324. </Form.Item>
  325. </Card></Col>;
  326. })}
  327. <Col span={7} offset={evaluation.length % 3 ? 1 : 0}>
  328. <Card className="plus" onClick={() => {
  329. this.addLength('evaluation', { title: '', link: '', image: '' });
  330. }}>
  331. <Icon type={'plus'} />
  332. </Card>
  333. </Col>
  334. </Row>
  335. </Form>
  336. </Block>;
  337. }
  338. renderContact() {
  339. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  340. const wechatImage = getFieldValue('contact.wechatImage');
  341. const weiboImage = getFieldValue('contacct.weiboImage');
  342. return <Block>
  343. <Form>
  344. <h1>联系方式</h1>
  345. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='电话'>
  346. {getFieldDecorator('contact.phone', {
  347. rules: [
  348. { required: false, message: '请输入电话' },
  349. ],
  350. })(
  351. <Input placeholder='请输入电话' />,
  352. )}
  353. </Form.Item>
  354. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='邮箱'>
  355. {getFieldDecorator('contact.email', {
  356. rules: [
  357. { required: false, message: '请输入邮箱' },
  358. ],
  359. })(
  360. <Input placeholder='请输入邮箱' />,
  361. )}
  362. </Form.Item>
  363. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='微信号'>
  364. {getFieldDecorator('contact.wechat', {
  365. rules: [
  366. { required: false, message: '请输入微信号' },
  367. ],
  368. })(
  369. <Input placeholder='请输入微信号' />,
  370. )}
  371. </Form.Item>
  372. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='公众号二维码'>
  373. {getFieldDecorator('contact.wechatImage')(
  374. <Upload
  375. listType="picture-card"
  376. showUploadList={false}
  377. beforeUpload={(file) => {
  378. System.uploadImage(file).then((result) => {
  379. setFieldsValue({ 'contact.wechatImage': result.url });
  380. return Promise.reject();
  381. });
  382. }
  383. }
  384. >
  385. {wechatImage ? <img src={wechatImage} alt="avatar" /> : <div>
  386. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  387. <div className="ant-upload-text">Upload</div>
  388. </div>}
  389. </Upload>,
  390. )}
  391. </Form.Item>
  392. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='微博二维码'>
  393. {getFieldDecorator('contact.weiboImage')(
  394. <Upload
  395. listType="picture-card"
  396. showUploadList={false}
  397. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  398. setFieldsValue({ 'contact.weiboImage': result.url });
  399. return Promise.reject();
  400. })}
  401. >
  402. {weiboImage ? <img src={weiboImage} alt="avatar" /> : <div>
  403. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  404. <div className="ant-upload-text">Upload</div>
  405. </div>}
  406. </Upload>,
  407. )}
  408. </Form.Item>
  409. </Form>
  410. </Block>;
  411. }
  412. renderView() {
  413. return <div >
  414. {this.renderPrepare()}
  415. {this.renderUser()}
  416. {this.renderClass()}
  417. {this.renderActivity()}
  418. {this.renderEvaluation()}
  419. {this.renderContact()}
  420. <Affix style={{ position: 'absolute', bottom: '50px', right: '50px' }}>
  421. <Button type="primary" onClick={() => {
  422. this.submit();
  423. }}>保存</Button>
  424. </Affix>
  425. </div>;
  426. }
  427. }