page.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React from 'react';
  2. import { Form, Button, Row, Col, Upload } from 'antd';
  3. import './index.less';
  4. // import Editor from '@src/components/Editor';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. // import Radio from '@src/components/Radio';
  8. // import TreeSelect from '@src/components/TreeSelect';
  9. // import EditTableCell from '@src/components/EditTableCell';
  10. import ActionLayout from '@src/layouts/ActionLayout';
  11. import TableLayout from '@src/layouts/TableLayout';
  12. import { formatFormError, formatDate } from '@src/services/Tools';
  13. import { asyncSMessage, asyncForm, asyncDelConfirm } from '@src/services/AsyncTools';
  14. // import { SwitchSelect, DataType } from '../../../../Constant';
  15. // import { User } from '../../../stores/user';
  16. // import { Exercise } from '../../../stores/exercise';
  17. import { Course } from '../../../stores/course';
  18. import { System } from '../../../stores/system';
  19. export default class extends Page {
  20. initState() {
  21. return { history: false };
  22. }
  23. init() {
  24. this.exerciseMap = {};
  25. this.actionList = [{
  26. key: 'addHistory',
  27. type: 'primary',
  28. name: '新增版本',
  29. }];
  30. this.itemList = [{
  31. key: 'id',
  32. type: 'hidden',
  33. }, {
  34. key: 'dataId',
  35. type: 'hidden',
  36. }, {
  37. key: 'time',
  38. type: 'date',
  39. name: '更新时间',
  40. }, {
  41. key: 'position',
  42. type: 'input',
  43. name: '更新位置',
  44. }, {
  45. key: 'originContent',
  46. type: 'input',
  47. name: '原内容',
  48. }, {
  49. key: 'content',
  50. type: 'input',
  51. name: '更改为',
  52. }, {
  53. key: 'version',
  54. type: 'input',
  55. name: '更新至',
  56. }];
  57. this.columns = [{
  58. title: '更新时间',
  59. dataIndex: 'time',
  60. render: (text) => {
  61. return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
  62. },
  63. }, {
  64. title: '版本名称',
  65. dataIndex: 'version',
  66. }, {
  67. title: '位置',
  68. dataIndex: 'position',
  69. }, {
  70. title: '原内容',
  71. dataIndex: 'originContent',
  72. }, {
  73. title: '更正为',
  74. dataIndex: 'content',
  75. }, {
  76. title: '更新至',
  77. dataIndex: 'version',
  78. }, {
  79. title: '操作',
  80. dataIndex: 'handler',
  81. render: (text, record) => {
  82. return <div className="table-button">
  83. {(
  84. <a onClick={() => {
  85. this.changeHistory(record);
  86. }}>编辑</a>
  87. )}
  88. {(
  89. <a onClick={() => {
  90. this.deleteHistory(record);
  91. }}>删除</a>
  92. )}
  93. </div>;
  94. },
  95. }];
  96. }
  97. initData() {
  98. const { id } = this.params;
  99. let handler;
  100. if (id) {
  101. handler = Course.getData({ id });
  102. } else {
  103. handler = Promise.resolve({ structId: 0 });
  104. }
  105. handler
  106. .then(result => {
  107. const { setFieldsValue } = this.props.form;
  108. setFieldsValue(result);
  109. this.setState({ data: result });
  110. this.refreshHistory();
  111. });
  112. }
  113. submit() {
  114. const { form } = this.props;
  115. form.validateFields((err, values) => {
  116. console.log(values);
  117. if (!err) {
  118. const data = form.getFieldsValue();
  119. Course.editData(data).then(() => {
  120. asyncSMessage('保存成功');
  121. }).catch((e) => {
  122. if (e.result) form.setFields(formatFormError(data, e.result));
  123. });
  124. }
  125. });
  126. }
  127. refreshHistory() {
  128. const { id } = this.params;
  129. Course.listDataHistory({ dataId: id }).then(result => {
  130. this.setState({ list: result.list, total: result.total, history: true });
  131. });
  132. }
  133. addHistoryAction() {
  134. const { id } = this.params;
  135. asyncForm('创建', this.itemList, { dataId: id }, data => {
  136. return Course.addDataHistory(data).then(() => {
  137. asyncSMessage('添加成功!');
  138. this.refreshHistory();
  139. });
  140. });
  141. }
  142. changeHistory(record) {
  143. asyncForm('修改', this.itemList, record, data => {
  144. return Course.editDataHistory(data).then(() => {
  145. asyncSMessage('修改成功!');
  146. this.refreshHistory();
  147. });
  148. });
  149. }
  150. deleteHistory(record) {
  151. asyncDelConfirm('删除确认', '是否删除选中记录?', () => {
  152. return Course.delDataHistory(record).then(() => {
  153. asyncSMessage('删除成功!');
  154. this.refreshHistory();
  155. });
  156. });
  157. }
  158. renderFile() {
  159. const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
  160. const resource = getFieldValue('resource');
  161. const trailResource = getFieldValue('trailResource');
  162. return <Block>
  163. <h1>资料文件</h1>
  164. {getFieldDecorator('id')(<input hidden />)}
  165. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='正式版本'>
  166. {resource && <a href={resource} target="_blank">访问</a>}
  167. {getFieldDecorator('resource', {
  168. rules: [
  169. { required: true, message: '上传文件' },
  170. ],
  171. })(
  172. <Upload
  173. showUploadList={false}
  174. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  175. setFieldsValue({ resource: result.url });
  176. return Promise.reject();
  177. })}
  178. >
  179. <Button>上传文件</Button>
  180. </Upload>,
  181. )}
  182. </Form.Item>
  183. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='试用版本'>
  184. {trailResource && <a href={trailResource} target="_blank">访问</a>}
  185. {getFieldDecorator('trailResource', {
  186. rules: [
  187. { required: true, message: '上传文件' },
  188. ],
  189. })(
  190. <Upload
  191. showUploadList={false}
  192. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  193. setFieldsValue({ trailResource: result.url });
  194. return Promise.reject();
  195. })}
  196. >
  197. <Button>上传文件</Button>
  198. </Upload>,
  199. )}
  200. </Form.Item>
  201. <Row type="flex" justify="center">
  202. <Col>
  203. <Button type="primary" onClick={() => {
  204. this.submit();
  205. }}>保存</Button>
  206. </Col>
  207. </Row>
  208. </Block>;
  209. }
  210. renderHistory() {
  211. return <Block>
  212. <h1>资料版本</h1>
  213. <ActionLayout
  214. itemList={this.actionList}
  215. selectedKeys={this.state.selectedKeys}
  216. onAction={key => this.onAction(key)}
  217. />
  218. <TableLayout
  219. columns={this.tableSort(this.columns)}
  220. list={this.state.list}
  221. pagination={false}
  222. loading={this.props.core.loading}
  223. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  224. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  225. selectedKeys={this.state.selectedKeys}
  226. />
  227. </Block>;
  228. }
  229. renderView() {
  230. const { history } = this.state;
  231. return <div flex>
  232. {this.renderFile()}
  233. {history && this.renderHistory()}
  234. </div>;
  235. }
  236. }