news.jsx 24 KB

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