page.js 18 KB

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