page.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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', true));
  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 = Object.keys(index.course || {}).map((key) => index.course[key]);
  57. index.activity = Object.keys(index.activity || {}).map((key) => index.activity[key]);
  58. index.evaluation = Object.keys(index.evaluation || {}).map((key) => index.evaluation[key]);
  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. })(
  214. <Upload
  215. listType="picture-card"
  216. showUploadList={false}
  217. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  218. setFieldsValue({ [`index.course.${index}.image`]: result.url });
  219. return Promise.reject();
  220. })}
  221. >
  222. {image ? <img src={image} alt="avatar" /> : <div>
  223. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  224. <div className="ant-upload-text">Upload</div>
  225. </div>}
  226. </Upload>,
  227. )}
  228. </Form.Item>
  229. </Card></Col>;
  230. })}
  231. <Col span={7} offset={course.length % 3 ? 1 : 0}>
  232. <Card className="plus" onClick={() => {
  233. this.addLength('course', { title: '', link: '', image: '' });
  234. }}>
  235. <Icon type={'plus'} />
  236. </Card>
  237. </Col>
  238. </Row>
  239. </Form>
  240. </Block >;
  241. }
  242. renderActivity() {
  243. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  244. const { indexInfo = {} } = this.state;
  245. const activity = indexInfo.activity || [];
  246. return <Block>
  247. <h1>活动信息</h1>
  248. <Form>
  249. <Row>
  250. {activity.map((row, index) => {
  251. const image = getFieldValue(`index.activity.${index}.image`) || null;
  252. return <Col span={7} offset={index % 3 ? 1 : 0}><Card>
  253. <Button className="delete-button" size="small" onClick={() => {
  254. this.deleteLength('activity', index, 1);
  255. }}>
  256. <Icon type="delete" />
  257. </Button>
  258. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='跳转链接'>
  259. {getFieldDecorator(`index.activity.${index}.link`, {
  260. rules: [
  261. { required: true, message: '输入跳转链接' },
  262. ],
  263. initialValue: row.link,
  264. })(
  265. <Input placeholder='请输入跳转链接' />,
  266. )}
  267. </Form.Item>
  268. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='活动图片'>
  269. {getFieldDecorator(`index.activity.${index}.image`, {
  270. rules: [
  271. { required: true, message: '上传图片' },
  272. ],
  273. })(
  274. <Upload
  275. listType="picture-card"
  276. showUploadList={false}
  277. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  278. setFieldsValue({ [`index.activity.${index}.image`]: result.url });
  279. return Promise.reject();
  280. })}
  281. >
  282. {image ? <img src={image} alt="avatar" /> : <div>
  283. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  284. <div className="ant-upload-text">Upload</div>
  285. </div>}
  286. </Upload>,
  287. )}
  288. </Form.Item>
  289. </Card></Col>;
  290. })}
  291. <Col span={7} offset={activity.length % 3 ? 1 : 0}>
  292. <Card className="plus" onClick={() => {
  293. this.addLength('activity', { link: '', image: '' });
  294. }}>
  295. <Icon type={'plus'} />
  296. </Card>
  297. </Col>
  298. </Row>
  299. </Form>
  300. </Block>;
  301. }
  302. renderEvaluation() {
  303. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  304. const { indexInfo = {} } = this.state;
  305. const evaluation = indexInfo.evaluation || [];
  306. return <Block>
  307. <h1>学员评价</h1>
  308. <Form>
  309. <Row>
  310. {evaluation.map((row, index) => {
  311. const avatar = getFieldValue(`index.evaluation.${index}.avatar`) || null;
  312. return <Col span={7} offset={index % 3 ? 1 : 0}><Card>
  313. <Button className="delete-button" size="small" onClick={() => {
  314. this.deleteLength('evaluation', index, 1);
  315. }}>
  316. <Icon type="delete" />
  317. </Button>
  318. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='学员昵称'>
  319. {getFieldDecorator(`index.evaluation.${index}.nickname`, {
  320. rules: [
  321. { required: true, message: '输入学员昵称' },
  322. ],
  323. initialValue: row.nickname,
  324. })(
  325. <Input placeholder='请输入学员昵称' />,
  326. )}
  327. </Form.Item>
  328. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='发布时间'>
  329. {getFieldDecorator(`index.evaluation.${index}.createTime`, {
  330. rules: [
  331. { required: true, message: '输入发布时间' },
  332. ],
  333. initialValue: row.createTime,
  334. })(
  335. <Input placeholder='请输入发布时间' />,
  336. )}
  337. </Form.Item>
  338. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='学员头像'>
  339. {getFieldDecorator(`index.evaluation.${index}.avatar`, {
  340. rules: [
  341. { required: true, message: '上传图片' },
  342. ],
  343. })(
  344. <Upload
  345. listType="picture-card"
  346. showUploadList={false}
  347. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  348. setFieldsValue({ [`index.evaluation.${index}.avatar`]: result.url });
  349. return Promise.reject();
  350. })}
  351. >
  352. {avatar ? <img src={avatar} alt="avatar" /> : <div>
  353. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  354. <div className="ant-upload-text">Upload</div>
  355. </div>}
  356. </Upload>,
  357. )}
  358. </Form.Item>
  359. <Form.Item labelCol={{ span: 7 }} wrapperCol={{ span: 15 }} label='评价内容'>
  360. {getFieldDecorator(`index.evaluation.${index}.content`, {
  361. rules: [
  362. { required: true, message: '输入评价内容' },
  363. ],
  364. initialValue: row.content,
  365. })(
  366. <Input placeholder='请输入评价内容' />,
  367. )}
  368. </Form.Item>
  369. </Card></Col>;
  370. })}
  371. <Col span={7} offset={evaluation.length % 3 ? 1 : 0}>
  372. <Card className="plus" onClick={() => {
  373. this.addLength('evaluation', { title: '', link: '', image: '' });
  374. }}>
  375. <Icon type={'plus'} />
  376. </Card>
  377. </Col>
  378. </Row>
  379. </Form>
  380. </Block>;
  381. }
  382. renderContact() {
  383. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  384. const wechatImage = getFieldValue('base.contact.wechatImage');
  385. const weiboImage = getFieldValue('base.contact.weiboImage');
  386. return <Block>
  387. <Form>
  388. <h1>联系方式</h1>
  389. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='电话'>
  390. {getFieldDecorator('base.contact.phone', {
  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.email', {
  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.wechat', {
  409. rules: [
  410. { required: false, message: '请输入微信号' },
  411. ],
  412. })(
  413. <Input placeholder='请输入微信号' />,
  414. )}
  415. </Form.Item>
  416. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='公众号二维码'>
  417. {getFieldDecorator('base.contact.wechatImage')(
  418. <Upload
  419. listType="picture-card"
  420. showUploadList={false}
  421. beforeUpload={(file) => {
  422. System.uploadImage(file).then((result) => {
  423. setFieldsValue({ 'base.contact.wechatImage': result.url });
  424. return Promise.reject();
  425. });
  426. }
  427. }
  428. >
  429. {wechatImage ? <img src={wechatImage} alt="avatar" /> : <div>
  430. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  431. <div className="ant-upload-text">Upload</div>
  432. </div>}
  433. </Upload>,
  434. )}
  435. </Form.Item>
  436. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='微博二维码'>
  437. {getFieldDecorator('base.contact.weiboImage')(
  438. <Upload
  439. listType="picture-card"
  440. showUploadList={false}
  441. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  442. setFieldsValue({ 'base.contact.weiboImage': result.url });
  443. return Promise.reject();
  444. })}
  445. >
  446. {weiboImage ? <img src={weiboImage} alt="avatar" /> : <div>
  447. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  448. <div className="ant-upload-text">Upload</div>
  449. </div>}
  450. </Upload>,
  451. )}
  452. </Form.Item>
  453. </Form>
  454. </Block>;
  455. }
  456. renderView() {
  457. return <div >
  458. {this.renderPrepare()}
  459. {this.renderUser()}
  460. {this.renderCourse()}
  461. {this.renderActivity()}
  462. {this.renderEvaluation()}
  463. {this.renderContact()}
  464. <Affix style={{ position: 'absolute', bottom: '50px', right: '50px' }}>
  465. <Button type="primary" onClick={() => {
  466. this.submit();
  467. }}>保存</Button>
  468. </Affix>
  469. </div>;
  470. }
  471. }