page.js 17 KB

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