page.js 17 KB

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