news.jsx 25 KB

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