news.jsx 24 KB

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