page.js 19 KB

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