news.jsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import React from 'react';
  2. import { Icon, Button, Spin, message, Table, Input, Modal, DatePicker, Upload, Select, Radio, Tabs } from 'antd';
  3. import { beforeUpload, getBase64 } from '../../../tools';
  4. import theme from 'react-quill/dist/quill.snow.css'
  5. import ReactQuill from 'react-quill'
  6. import moment from 'moment';
  7. import ajax from 'jquery/src/ajax/xhr.js';
  8. import $ from 'jquery/src/ajax';
  9. function uploadImg(s) {
  10. let myform = new FormData(), file = document.getElementById("fileToUpload").files[0];
  11. if (!file) {
  12. return;
  13. }
  14. myform.append('file', file);
  15. myform.append('sign', 'news_content_picture');
  16. $.ajax({
  17. method: "post",
  18. dataType: "json",
  19. url: location.origin + "/api/admin/news/upload",
  20. data: myform,
  21. contentType: false,
  22. processData: false,
  23. success: function (data) {
  24. if (data.error && data.error.length) {
  25. message.warning(data.error[0].message);
  26. } else {
  27. const cursorPosition = s.quill.getSelection().index
  28. s.quill.insertEmbed(cursorPosition, 'image', globalConfig.avatarHost + '/upload' + data.data)
  29. s.quill.setSelection(cursorPosition + 1)
  30. };
  31. }
  32. })
  33. }
  34. function fileSelect() {
  35. let uploader = document.getElementById("fileToUpload");
  36. if (!uploader.onchange) {
  37. uploader.onchange = () => {
  38. uploadImg(this)
  39. }
  40. }
  41. uploader.click();
  42. }
  43. const Avatar = React.createClass({
  44. getInitialState() {
  45. return {
  46. imageUrl: ''
  47. }
  48. },
  49. handleChange(info) {
  50. if (info.file.status === 'done') {
  51. // Get this url from response in real world.
  52. getBase64(info.file.originFileObj, imageUrl => this.setState({ imageUrl }));
  53. this.props.urlData(info.file.response.data);
  54. }
  55. },
  56. componentWillReceiveProps(nextProps) {
  57. if (this.props.visible && !nextProps.visible) {
  58. this.state.imageUrl = null;
  59. };
  60. },
  61. render() {
  62. const imageUrl = this.state.imageUrl;
  63. return (
  64. <Upload
  65. className="avatar-uploader"
  66. name={this.props.name}
  67. showUploadList={false}
  68. action={globalConfig.context + "/api/admin/news/upload"}
  69. data={{ 'sign': this.props.sign }}
  70. beforeUpload={beforeUpload}
  71. onChange={this.handleChange} >
  72. {
  73. (imageUrl || this.props.imageUrl) ?
  74. <img src={imageUrl || this.props.imageUrl} role="presentation" id={this.props.name} /> :
  75. <Icon type="plus" className="avatar-uploader-trigger" />
  76. }
  77. </Upload>
  78. );
  79. }
  80. });
  81. const News = React.createClass({
  82. loadData(pageNo, theType) {
  83. this.setState({
  84. loading: true
  85. });
  86. $.ajax({
  87. method: "get",
  88. dataType: "json",
  89. crossDomain: false,
  90. url: globalConfig.context + '/api/admin/news/list',
  91. data: {
  92. pageNo: pageNo || 1,
  93. pageSize: this.state.pagination.pageSize,
  94. type: theType || this.props.newsType || 0, //新闻类型
  95. title: this.state.searchTitle,
  96. author: this.state.searchAuthor,
  97. startCreateTime: this.state.startDate, //新闻开始时间 yyyy-MM-dd
  98. endCreateTime: this.state.endDate, //新闻结束时间 yyyy-MM-dd
  99. source: this.state.searchSource,
  100. hot: this.state.searchHot, //是否放在首页
  101. },
  102. success: function (data) {
  103. let theArr = [];
  104. if (!data.data || !data.data.list) {
  105. if (data.error && data.error.length) {
  106. message.warning(data.error[0].message);
  107. };
  108. } else {
  109. for (let i = 0; i < data.data.list.length; i++) {
  110. let thisdata = data.data.list[i];
  111. theArr.push({
  112. key: i,
  113. id: thisdata.id,
  114. createTime: thisdata.createTime,
  115. editTime: thisdata.editTime,
  116. title: thisdata.title,
  117. titleImg: thisdata.titleImg,
  118. author: thisdata.author,
  119. type: thisdata.type,
  120. hot: thisdata.hot,
  121. source: thisdata.source,
  122. sourceUrl: thisdata.sourceUrl,
  123. summary: thisdata.summary,
  124. content: thisdata.content,
  125. createTimeFormattedDate: thisdata.createTimeFormattedDate,
  126. });
  127. };
  128. this.state.pagination.current = data.data.pageNo;
  129. this.state.pagination.total = data.data.totalCount;
  130. };
  131. this.setState({
  132. tableData: theArr,
  133. pagination: this.state.pagination
  134. });
  135. }.bind(this),
  136. }).always(function () {
  137. this.setState({
  138. loading: false
  139. });
  140. }.bind(this));
  141. },
  142. loadDetailData() {
  143. this.setState({
  144. loading: true
  145. });
  146. $.ajax({
  147. method: "get",
  148. dataType: "json",
  149. crossDomain: false,
  150. url: globalConfig.context + '/api/admin/news/detail',
  151. data: {
  152. id: this.state.RowData.id //新闻主键ID(数字类型,非字符串)
  153. },
  154. success: function (data) {
  155. let theObj = {};
  156. if (!data.data) {
  157. if (data.error && data.error.length) {
  158. message.warning(data.error[0].message);
  159. };
  160. } else {
  161. theObj = data.data;
  162. };
  163. this.setState({
  164. RowData: theObj,
  165. });
  166. }.bind(this),
  167. }).always(function () {
  168. this.setState({
  169. loading: false
  170. });
  171. }.bind(this));
  172. },
  173. getInitialState() {
  174. return {
  175. loading: false,
  176. visible: false,
  177. selectedRowKeys: [],
  178. selectedRows: [],
  179. keysObj: {},
  180. RowData: {},
  181. pagination: {
  182. defaultCurrent: 1,
  183. defaultPageSize: 10,
  184. showQuickJumper: true,
  185. pageSize: 10,
  186. onChange: function (page) {
  187. this.loadData(page);
  188. }.bind(this),
  189. showTotal: function (total) {
  190. return '共' + total + '条数据';
  191. }
  192. },
  193. columns: [
  194. {
  195. title: '标题',
  196. dataIndex: 'title',
  197. key: 'title',
  198. width: '20%'
  199. }, {
  200. title: '发布时间',
  201. dataIndex: 'createTimeFormattedDate',
  202. key: 'createTimeFormattedDate',
  203. }, {
  204. title: '作者',
  205. dataIndex: 'author',
  206. key: 'author',
  207. }, {
  208. title: '来源',
  209. dataIndex: 'source',
  210. key: 'source',
  211. }, {
  212. title: '简介',
  213. dataIndex: 'summary',
  214. key: 'summary',
  215. width: '50%'
  216. }
  217. ],
  218. tableData: []
  219. };
  220. },
  221. tableRowClick(record, index) {
  222. this.state.RowData = record;
  223. this.loadDetailData();
  224. this.setState({
  225. visible: true
  226. });
  227. },
  228. componentWillMount() {
  229. this.loadData();
  230. },
  231. componentWillReceiveProps(nextProps) {
  232. if (this.props.newsType != nextProps.newsType) {
  233. this.loadData(1, nextProps.newsType);
  234. };
  235. },
  236. handleRichText(value) {
  237. console.log(value);
  238. this.state.RowData.content = value;
  239. this.setState({ RowData: this.state.RowData });
  240. },
  241. submit() {
  242. if (!this.state.RowData.title) {
  243. message.warning('必须填写一个标题!');
  244. return;
  245. };
  246. if (!this.state.RowData.createTime) {
  247. message.warning('必须选择一个发布时间!');
  248. return;
  249. };
  250. if (!this.state.RowData.author) {
  251. message.warning('必须填写一个作者!');
  252. return;
  253. };
  254. this.setState({
  255. loading: true
  256. });
  257. $.ajax({
  258. method: "post",
  259. dataType: "json",
  260. crossDomain: false,
  261. url: globalConfig.context + (this.state.RowData.id ? '/api/admin/news/update' : '/api/admin/news/add'),
  262. data: {
  263. id: this.state.RowData.id || null, //主键,如果不填主键则为新增记录,填主键则为更新
  264. title: this.state.RowData.title,
  265. titleImg: this.state.RowData.titleImg,
  266. author: this.state.RowData.author,
  267. type: this.props.newsType,
  268. hot: this.state.RowData.hot,
  269. source: this.state.RowData.source,
  270. sourceUrl: this.state.RowData.sourceUrl,
  271. summary: this.state.RowData.summary,
  272. content: this.state.RowData.content,
  273. createTimeFormattedDate: this.state.RowData.createTimeFormattedDate,
  274. },
  275. success: function (data) {
  276. if (data.error && data.error.length) {
  277. message.warning(data.error[0].message);
  278. } else {
  279. message.success('保存成功!');
  280. this.setState({ visible: false });
  281. this.loadData();
  282. };
  283. }.bind(this),
  284. }).always(function () {
  285. this.setState({
  286. loading: false
  287. });
  288. }.bind(this));
  289. },
  290. delectRow() {
  291. let deletedIds = [];
  292. for (let idx = 0; idx < this.state.selectedRows.length; idx++) {
  293. let rowItem = this.state.selectedRows[idx];
  294. if (rowItem.id) {
  295. deletedIds.push(rowItem.id)
  296. };
  297. };
  298. this.setState({
  299. selectedRowKeys: [],
  300. loading: deletedIds.length > 0
  301. });
  302. $.ajax({
  303. method: "POST",
  304. dataType: "json",
  305. crossDomain: false,
  306. url: globalConfig.context + "/api/admin/news/delete",
  307. data: {
  308. ids: deletedIds
  309. }
  310. }).done(function (data) {
  311. if (data.error && data.error.length) {
  312. message.warning(data.error[0].message);
  313. } else {
  314. message.success('删除成功!');
  315. };
  316. this.setState({
  317. loading: false,
  318. });
  319. this.loadData();
  320. }.bind(this));
  321. },
  322. reset() {
  323. this.state.searchKey = undefined;
  324. this.loadData();
  325. },
  326. render() {
  327. const rowSelection = {
  328. selectedRowKeys: this.state.selectedRowKeys,
  329. onChange: (selectedRowKeys, selectedRows) => {
  330. this.setState({
  331. selectedRows: selectedRows.slice(-1),
  332. selectedRowKeys: selectedRowKeys.slice(-1)
  333. });
  334. }
  335. };
  336. const hasSelected = this.state.selectedRowKeys.length > 0;
  337. const modules = {
  338. toolbar: {
  339. container: [
  340. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  341. [{ 'header': [1, 2, 3, false] }],
  342. ['bold', 'italic', 'underline', 'strike', 'blockquote'], // toggled buttons
  343. // [{ 'header': 1 }, { 'header': 2 }], // custom button values
  344. [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  345. // [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript
  346. [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent
  347. // [{ 'direction': 'rtl' }], // text direction
  348. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  349. // [{ 'font': [] }],
  350. [{ 'align': [] }],
  351. ['link', 'image'],
  352. ['clean']
  353. ],
  354. handlers: { 'image': fileSelect }
  355. }
  356. };
  357. const formats = [
  358. 'size', 'header',
  359. 'bold', 'italic', 'underline', 'strike', 'blockquote',
  360. 'list', 'bullet', 'indent',
  361. 'color', 'background',
  362. 'align',
  363. 'link', 'image'
  364. ];
  365. return (
  366. <div className="admin-content" >
  367. <div className="admin-content-title">科技快讯管理</div>
  368. <div className="admin-content-warp">
  369. <div className="admin-content-header">科技快讯列表</div>
  370. <div className="admin-content-search">
  371. <Input placeholder="标题" style={{ width: 200 }}
  372. value={this.state.searchTitle}
  373. onChange={(e) => { this.setState({ searchTitle: e.target.value }); }} />
  374. <Input placeholder="作者" style={{ width: 100 }}
  375. value={this.state.searchAuthor}
  376. onChange={(e) => { this.setState({ searchAuthor: e.target.value }); }} />
  377. <Input placeholder="来源" style={{ width: 100 }}
  378. value={this.state.searchSource}
  379. onChange={(e) => { this.setState({ searchSource: e.target.value }); }} />
  380. <Select placeholder="显示在首页"
  381. style={{ width: 100 }}
  382. value={this.state.searchHot}
  383. onChange={(e) => { this.setState({ searchHot: e }) }}>
  384. <Select.Option value="0" >不显示</Select.Option>
  385. <Select.Option value="1" >显示</Select.Option>
  386. </Select>
  387. <span>发布时间</span>
  388. <DatePicker style={{ width: 110 }}
  389. format="YYYY-MM-DD"
  390. placeholder="选择开始时间"
  391. value={this.state.startDate ? moment(this.state.startDate, "YYYY-MM-DD") : undefined}
  392. onChange={(t, str) => {
  393. this.setState({ startDate: str });
  394. }} />
  395. <span>到</span>
  396. <DatePicker style={{ width: 110 }}
  397. format="YYYY-MM-DD"
  398. placeholder="选择结束时间"
  399. value={this.state.endDate ? moment(this.state.endDate, "YYYY-MM-DD") : undefined}
  400. onChange={(t, str) => {
  401. this.setState({ endDate: str });
  402. }} />
  403. <Button type="primary" onClick={() => { this.loadData() }}>搜索</Button>
  404. <Button onClick={this.reset}>重置</Button>
  405. <Button type='danger' disabled={!hasSelected} onClick={this.delectRow}>
  406. 删除<Icon type="minus" />
  407. </Button>
  408. <Button type="primary" style={{ float: 'right' }}
  409. onClick={() => { this.setState({ RowData: {}, visible: true }) }}>
  410. 添加<Icon type="plus" />
  411. </Button>
  412. </div>
  413. <Spin spinning={this.state.loading}>
  414. <Table className="no-all-select" columns={this.state.columns}
  415. dataSource={this.state.tableData}
  416. pagination={this.state.pagination}
  417. rowSelection={rowSelection}
  418. onRowClick={this.tableRowClick} />
  419. </Spin>
  420. </div>
  421. <Modal title="新闻详情" width={1000} className="news-modal"
  422. visible={this.state.visible}
  423. maskClosable={false}
  424. footer={null}
  425. afterClose={() => { this.state.RowData = {}; }}
  426. onCancel={() => { this.setState({ visible: false }) }} >
  427. <Spin spinning={this.state.loading}>
  428. <div className="clearfix">
  429. <div className="modal-box">
  430. <span className="modal-box-title">新闻标题</span>
  431. <div className="modal-box-detail">
  432. <Input value={this.state.RowData.title}
  433. onChange={(e) => {
  434. this.state.RowData.title = e.target.value;
  435. this.setState({ RowData: this.state.RowData });
  436. }} />
  437. </div>
  438. </div>
  439. <div className="modal-box">
  440. <span className="modal-box-title">简介</span>
  441. <div className="modal-box-detail">
  442. <Input value={this.state.RowData.summary}
  443. onChange={(e) => {
  444. this.state.RowData.summary = e.target.value;
  445. this.setState({ RowData: this.state.RowData });
  446. }} />
  447. </div>
  448. </div>
  449. <div className="modal-box">
  450. <span className="modal-box-title">来源</span>
  451. <div className="modal-box-detail">
  452. <Input value={this.state.RowData.source}
  453. onChange={(e) => {
  454. this.state.RowData.source = e.target.value;
  455. this.setState({ RowData: this.state.RowData });
  456. }} />
  457. </div>
  458. </div>
  459. <div className="modal-box">
  460. <span className="modal-box-title">来源链接</span>
  461. <div className="modal-box-detail">
  462. <Input value={this.state.RowData.sourceUrl}
  463. onChange={(e) => {
  464. this.state.RowData.sourceUrl = e.target.value;
  465. this.setState({ RowData: this.state.RowData });
  466. }} />
  467. </div>
  468. </div>
  469. <div className="modal-box" style={{ position: 'absolute', right: '0', width: '480px' }}>
  470. <span className="modal-box-title">标题图片</span>
  471. <div className="modal-box-detail clearfix" style={{ width: '360px' }}>
  472. <Avatar sign={'news'} visible={this.state.visible}
  473. imageUrl={this.state.RowData.titleImg ? (globalConfig.avatarHost + "/upload" + this.state.RowData.imgurl) : null}
  474. urlData={(url) => { this.state.RowData.titleImg = url }} />
  475. </div>
  476. </div>
  477. </div>
  478. <div className="clearfix">
  479. <div className="modal-box news-box">
  480. <span className="modal-box-title">作者</span>
  481. <div className="modal-box-detail">
  482. <Input value={this.state.RowData.author}
  483. onChange={(e) => {
  484. this.state.RowData.author = e.target.value;
  485. this.setState({ RowData: this.state.RowData });
  486. }} />
  487. </div>
  488. </div>
  489. <div className="modal-box news-box">
  490. <span className="modal-box-title">发布时间</span>
  491. <div className="modal-box-detail">
  492. <DatePicker style={{ width: '120px' }}
  493. format="YYYY-MM-DD"
  494. placeholder="选择发布时间"
  495. value={this.state.RowData.createTimeFormattedDate ? moment(this.state.RowData.createTimeFormattedDate, "YYYY-MM-DD") : undefined}
  496. onChange={(t, str) => {
  497. this.state.RowData.createTimeFormattedDate = str;
  498. this.setState({ RowData: this.state.RowData });
  499. }} />
  500. </div>
  501. </div>
  502. <div className="modal-box news-box">
  503. <span className="modal-box-title">发布在首页</span>
  504. <div className="modal-box-detail">
  505. <Radio.Group value={this.state.RowData.hot} onChange={(e) => {
  506. this.state.RowData.hot = e.target.value;
  507. this.setState({ RowData: this.state.RowData });
  508. }} >
  509. <Radio value={0}>否</Radio>
  510. <Radio value={1}>是</Radio>
  511. </Radio.Group>
  512. </div>
  513. </div>
  514. </div>
  515. <div className="modal-box">
  516. <span className="modal-box-title">新闻内容</span>
  517. </div>
  518. <ReactQuill theme="snow"
  519. value={this.state.RowData.content}
  520. modules={modules}
  521. formats={formats}
  522. onChange={this.handleRichText} />
  523. <input type="file"
  524. name="fileToUpload"
  525. id="fileToUpload"
  526. style={{ "display": "none" }} />
  527. <div className="modal-box">
  528. <Button type="primary" onClick={this.submit}>提交</Button>
  529. <Button type="ghost" onClick={() => { this.setState({ visible: false }) }}>取消</Button>
  530. </div>
  531. </Spin>
  532. </Modal>
  533. </div >
  534. );
  535. }
  536. });
  537. export default News;