page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Form, Input, Button, Row, Col, InputNumber, Upload, Card, Icon, Tabs, Spin } from 'antd';
  4. import './index.less';
  5. import Editor from '@src/components/Editor';
  6. import Page from '@src/containers/Page';
  7. import Block from '@src/components/Block';
  8. // import FileUpload from '@src/components/FileUpload';
  9. import { formatFormError } from '@src/services/Tools';
  10. import { asyncSMessage } from '@src/services/AsyncTools';
  11. import { CourseVsType } from '../../../../Constant';
  12. import { Course } from '../../../stores/course';
  13. import { System } from '../../../stores/system';
  14. export default class extends Page {
  15. init() {
  16. this.state.tab = CourseVsType[0].value;
  17. }
  18. initData() {
  19. this.changeCourse(this.state.tab);
  20. }
  21. changeCourse(key) {
  22. this.setState({ tab: key, loading: true });
  23. Course.list({ courseModule: 'vs' })
  24. .then(result => {
  25. const { form } = this.props;
  26. const list = result.list.filter(row => row.vsType === key);
  27. let row = {};
  28. if (list.length > 0) {
  29. ([row] = list);
  30. Course.get({ id: row.id }).then(r => {
  31. form.setFieldsValue(r);
  32. this.setState({ data: r, loading: false });
  33. this.refreshTeacher(row.id);
  34. });
  35. } else {
  36. form.resetFields();
  37. this.setState({ data: { expireDays: 7 }, loading: false, teacher: false });
  38. }
  39. });
  40. }
  41. refreshTeacher(id) {
  42. Course.listTeacher({ courseId: id }).then(result => {
  43. this.setState({ teachers: result.list, total: result.total, teacher: true });
  44. });
  45. }
  46. changeTeacher(field, index, value) {
  47. const { teachers } = this.state;
  48. teachers[index] = teachers[index] || {};
  49. teachers[index][field] = value;
  50. this.setState({ teachers });
  51. }
  52. addLength(key, data) {
  53. this.state[key] = this.state[key] || [];
  54. this.state[key].push(data);
  55. this.setState({ [key]: this.state[key] });
  56. }
  57. deleteLength(key, start) {
  58. this.state[key] = this.state[key] || [];
  59. this.state[key].splice(start, 1);
  60. this.setState({ [key]: this.state[key] });
  61. }
  62. submit() {
  63. const { form } = this.props;
  64. form.validateFields((err) => {
  65. if (!err) {
  66. const data = form.getFieldsValue();
  67. data.vsType = this.state.tab;
  68. data.parentStructId = 0;
  69. data.structId = 0;
  70. data.extend = '';
  71. data.courseModule = 'vs';
  72. let handler;
  73. if (data.id) {
  74. handler = Course.edit(data);
  75. } else {
  76. handler = Course.add(data);
  77. }
  78. handler.then(() => {
  79. asyncSMessage('保存成功');
  80. if (!data.id) {
  81. this.setState({ teacher: true, teachers: [] });
  82. }
  83. }).catch((e) => {
  84. if (e.result) form.setFields(formatFormError(data, e.result));
  85. });
  86. }
  87. });
  88. }
  89. renderVs() {
  90. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  91. const cover = getFieldValue('cover');
  92. return <Block>
  93. <Form>
  94. {getFieldDecorator('id')(<input hidden />)}
  95. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程名称'>
  96. {getFieldDecorator('title', {
  97. rules: [
  98. { required: true, message: '请输入课程名称' },
  99. ],
  100. })(
  101. <Input placeholder='请输入课程名称' />,
  102. )}
  103. </Form.Item>
  104. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='定价'>
  105. {getFieldDecorator('price', {
  106. rules: [
  107. { required: true, message: '请输入价格' },
  108. ],
  109. })(
  110. <InputNumber placeholder='请输入' />,
  111. )}
  112. </Form.Item>
  113. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='最小购买数量'>
  114. {getFieldDecorator('minNumber', {
  115. rules: [
  116. { required: true, message: '请输入最小购买数量' },
  117. ],
  118. })(
  119. <InputNumber placeholder='请输入' />,
  120. )}
  121. </Form.Item>
  122. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='最大购买数量'>
  123. {getFieldDecorator('maxNumber', {
  124. rules: [
  125. { required: true, message: '请输入最大购买数量' },
  126. ],
  127. })(
  128. <InputNumber placeholder='请输入' />,
  129. )}
  130. </Form.Item>
  131. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='有效期'>
  132. {getFieldDecorator('expirePreDays', {
  133. rules: [
  134. { required: true, message: '请输入有效期' },
  135. ],
  136. })(
  137. <InputNumber placeholder='天/10课时' />,
  138. )}
  139. </Form.Item>
  140. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='开通有效期'>
  141. {getFieldDecorator('expireDays', {
  142. rules: [
  143. { required: true, message: '请输入有效期' },
  144. ],
  145. })(
  146. <InputNumber placeholder='天' />,
  147. )}
  148. </Form.Item>
  149. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='简短描述'>
  150. {getFieldDecorator('comment', {
  151. rules: [
  152. { required: true, message: '请输入描述' },
  153. ],
  154. })(
  155. <Input placeholder='请输入简短描述' />,
  156. )}
  157. </Form.Item>
  158. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程封面'>
  159. {getFieldDecorator('cover', {
  160. rules: [
  161. { required: true, message: '上传图片' },
  162. ],
  163. })(
  164. <Upload
  165. listType="picture-card"
  166. showUploadList={false}
  167. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  168. setFieldsValue({ cover: result.url });
  169. return Promise.reject();
  170. })}
  171. >
  172. {cover ? <img src={cover} alt="avatar" /> : <div>
  173. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  174. <div className="ant-upload-text">Upload</div>
  175. </div>}
  176. </Upload>,
  177. )}
  178. </Form.Item>
  179. </Form>
  180. </Block>;
  181. }
  182. renderInfoVs() {
  183. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  184. const image = getFieldValue('wechatAvatar') || null;
  185. return <Block flex>
  186. <h1>课程介绍</h1>
  187. <Form>
  188. <Form.Item label='服务介绍'>
  189. {getFieldDecorator('serviceContent', {
  190. })(
  191. <Editor placeholder='输入内容' />,
  192. )}
  193. </Form.Item>
  194. <Form.Item label='适合人群'>
  195. {getFieldDecorator('crowdContent', {
  196. })(
  197. <Editor placeholder='输入内容' />,
  198. )}
  199. </Form.Item>
  200. <Form.Item label='课时数'>
  201. {getFieldDecorator('courseNoContent', {
  202. })(
  203. <Editor placeholder='输入内容' />,
  204. )}
  205. </Form.Item>
  206. <Form.Item label='授课流程'>
  207. {getFieldDecorator('processContent', {
  208. })(
  209. <Editor placeholder='输入内容' />,
  210. )}
  211. </Form.Item>
  212. <Form.Item label='推广语'>
  213. {getFieldDecorator('messageContent', {
  214. })(
  215. <Editor placeholder='输入内容' />,
  216. )}
  217. </Form.Item>
  218. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label='微信头像'>
  219. {getFieldDecorator('wechatAvatar')(
  220. <Upload
  221. listType="picture-card"
  222. showUploadList={false}
  223. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  224. setFieldsValue({ wechatAvatar: result.url });
  225. return Promise.reject();
  226. })}
  227. >
  228. {image ? <img src={image} alt="avatar" /> : <div>
  229. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  230. <div className="ant-upload-text">Upload</div>
  231. </div>}
  232. </Upload>,
  233. )}
  234. </Form.Item>
  235. </Form>
  236. </Block>;
  237. }
  238. renderTeacher() {
  239. const { teachers, teacher, data } = this.state;
  240. if (!teacher) return null;
  241. return <Block>
  242. <h1>授课老师</h1>
  243. <Form>
  244. <Row>
  245. {teachers.map((row, index) => {
  246. return <Col span={7} offset={index % 3 ? 1 : 0}><Card>
  247. <Button className="delete-button" size="small" onClick={() => {
  248. if (row.id) {
  249. Course.delTeacher({ id: row.id }).then(() => {
  250. this.deleteLength('teachers', index);
  251. });
  252. } else {
  253. this.deleteLength('teachers', index);
  254. }
  255. }}>
  256. <Icon type="delete" />
  257. </Button>
  258. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label='姓名'>
  259. <Input placeholder='请输入姓名' value={row.realname} onChange={(e) => {
  260. this.changeTeacher('realname', index, e.target.value);
  261. }} />
  262. </Form.Item>
  263. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label='微信'>
  264. <Input placeholder='请输入微信' value={row.wechat} onChange={(e) => {
  265. this.changeTeacher('wechat', index, e.target.value);
  266. }} />
  267. </Form.Item>
  268. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label='头像'>
  269. <Upload
  270. listType="picture-card"
  271. showUploadList={false}
  272. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  273. this.changeTeacher('avatar', index, result.url);
  274. return Promise.reject();
  275. })}
  276. >
  277. {row.avatar ? <img src={row.avatar} alt="avatar" /> : <div>
  278. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  279. <div className="ant-upload-text">Upload</div>
  280. </div>}
  281. </Upload>
  282. </Form.Item>
  283. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label='二维码'>
  284. <Upload
  285. listType="picture-card"
  286. showUploadList={false}
  287. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  288. this.changeTeacher('qr', index, result.url);
  289. return Promise.reject();
  290. })}
  291. >
  292. {row.qr ? <img src={row.qr} alt="avatar" /> : <div>
  293. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  294. <div className="ant-upload-text">Upload</div>
  295. </div>}
  296. </Upload>
  297. </Form.Item>
  298. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label='简介'>
  299. <Input.TextArea placeholder='请输入简介' value={row.description} onChange={(e) => {
  300. this.changeTeacher('description', index, e.target.value);
  301. }} />
  302. </Form.Item>
  303. <Button onClick={() => {
  304. let handler = null;
  305. if (row.id) {
  306. handler = Course.editTeacher(row);
  307. } else {
  308. handler = Course.addTeacher(Object.assign({ courseId: data.id }, row));
  309. }
  310. handler.then(() => {
  311. this.refreshTeacher(data.id);
  312. });
  313. }}>{row.id ? '修改' : '添加'}</Button>
  314. </Card></Col>;
  315. })}
  316. <Col span={7} offset={teachers.length % 3 ? 1 : 0}>
  317. <Card className="plus" onClick={() => {
  318. this.addLength('teachers', { realname: '', wechat: '', qr: '' });
  319. }}>
  320. <Icon type={'plus'} />
  321. </Card>
  322. </Col>
  323. </Row>
  324. </Form>
  325. </Block>;
  326. }
  327. renderContent() {
  328. return [this.renderVs(), this.renderInfoVs(), this.renderTeacher()];
  329. }
  330. renderView() {
  331. const { tab, data } = this.state;
  332. return <div flex>
  333. <Tabs activeKey={tab} tabBarExtraContent={data.id > 0 && <Link to={`/course/student/${data.id}`}>查看学员</Link>} onChange={(value) => {
  334. this.changeCourse(value);
  335. }}>
  336. {CourseVsType.map((row) => {
  337. return <Tabs.TabPane tab={row.label} key={row.value} />;
  338. })}
  339. </Tabs>
  340. <Spin spinning={this.state.loading}>{this.renderContent()}</Spin>
  341. <Row type="flex" justify="center">
  342. <Col>
  343. <Button type="primary" onClick={() => {
  344. this.submit();
  345. }}>保存</Button>
  346. </Col>
  347. </Row>
  348. </div>;
  349. }
  350. }