page.js 18 KB

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