bannerManage.jsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import React from 'react';
  2. import { Icon, Button, Spin, message, Table, Input, Modal, DatePicker, Upload, Select, Pagination } from 'antd';
  3. import { beforeUpload, getBase64 } from '../../../tools';
  4. import moment from 'moment';
  5. import ajax from 'jquery/src/ajax/xhr.js';
  6. import $ from 'jquery/src/ajax';
  7. const Avatar = React.createClass({
  8. getInitialState() {
  9. return {
  10. imageUrl: ''
  11. }
  12. },
  13. handleChange(info) {
  14. if (info.file.status === 'done') {
  15. // Get this url from response in real world.
  16. getBase64(info.file.originFileObj, imageUrl => this.setState({ imageUrl }));
  17. this.props.urlData(info.file.response.data);
  18. }
  19. },
  20. componentWillReceiveProps(nextProps) {
  21. if (this.props.visible && !nextProps.visible) {
  22. this.state.imageUrl = null;
  23. };
  24. },
  25. render() {
  26. const imageUrl = this.state.imageUrl;
  27. return (
  28. <Upload
  29. className="avatar-uploader"
  30. name={this.props.name}
  31. showUploadList={false}
  32. action={globalConfig.context + "/api/admin/banners/upload"}
  33. data={{ 'sign': this.props.sign }}
  34. beforeUpload={beforeUpload}
  35. onChange={this.handleChange} >
  36. {
  37. (imageUrl || this.props.imageUrl) ?
  38. <img src={imageUrl || this.props.imageUrl} role="presentation" id={this.props.name} /> :
  39. <Icon type="plus" className="avatar-uploader-trigger" />
  40. }
  41. </Upload>
  42. );
  43. }
  44. });
  45. const Lecture = React.createClass({
  46. loadData(pageNo) {
  47. this.setState({
  48. loading: true
  49. });
  50. $.ajax({
  51. method: "get",
  52. dataType: "json",
  53. crossDomain: false,
  54. url: globalConfig.context + '/api/admin/banners/list',
  55. data: {
  56. pageNo: pageNo || 1,
  57. pageSize: this.state.pagination.pageSize,
  58. key: this.state.searchKey
  59. },
  60. success: function (data) {
  61. let theArr = [];
  62. if (!data.data || !data.data.list) {
  63. if (data.error && data.error.length) {
  64. message.warning(data.error[0].message);
  65. };
  66. } else {
  67. for (let i = 0; i < data.data.list.length; i++) {
  68. let thisdata = data.data.list[i];
  69. theArr.push({
  70. key: i,
  71. id: thisdata.id, //主键
  72. sign: thisdata.sign, // 位置标示
  73. text: thisdata.text, //广告图描述
  74. imgurl: thisdata.imgurl, //广告图url
  75. url: thisdata.url, //跳转链接
  76. width: thisdata.width, //宽度
  77. height: thisdata.height, //高度
  78. startDate: thisdata.startDate, //广告图开始时间
  79. endDate: thisdata.endDate //广告图结束时间
  80. });
  81. };
  82. this.state.pagination.pageNo = pageNo || 1;
  83. this.state.pagination.total = data.data.total || 0;
  84. };
  85. this.setState({
  86. tableData: theArr,
  87. pagination: this.state.pagination
  88. });
  89. }.bind(this),
  90. }).always(function () {
  91. this.setState({
  92. loading: false
  93. });
  94. }.bind(this));
  95. },
  96. loadKeys() {
  97. this.setState({
  98. loading: true
  99. });
  100. $.ajax({
  101. method: "get",
  102. dataType: "json",
  103. url: globalConfig.context + '/api/admin/banners/keys',
  104. success: function (data) {
  105. let theArr = [];
  106. if (!data.data) {
  107. if (data.error && data.error.length) {
  108. message.warning(data.error[0].message);
  109. };
  110. } else {
  111. for (let item in data.data) {
  112. let thisdata = data.data[item];
  113. theArr.push(
  114. <Select.Option value={item} key={item}>{thisdata}</Select.Option>
  115. );
  116. };
  117. };
  118. this.setState({
  119. keysOption: theArr,
  120. keysObj: data.data
  121. });
  122. }.bind(this),
  123. }).always(function () {
  124. this.setState({
  125. loading: false
  126. });
  127. }.bind(this));
  128. },
  129. getInitialState() {
  130. return {
  131. loading: false,
  132. visible: false,
  133. selectedRowKeys: [],
  134. selectedRows: [],
  135. keysObj: {},
  136. RowData: {},
  137. pagination: {
  138. pageNo: 1,
  139. pageSize: 10,
  140. total: 0
  141. },
  142. columns: [
  143. {
  144. title: '位置',
  145. dataIndex: 'sign',
  146. key: 'sign',
  147. render: text => { return this.state.keysObj[text] }
  148. }, {
  149. title: '广告图描述',
  150. dataIndex: 'text',
  151. key: 'text',
  152. }, {
  153. title: '广告图url',
  154. dataIndex: 'imgurl',
  155. key: 'imgurl',
  156. }, {
  157. title: '跳转链接',
  158. dataIndex: 'url',
  159. key: 'url',
  160. }, {
  161. title: '宽度',
  162. dataIndex: 'width',
  163. key: 'width',
  164. }, {
  165. title: '高度',
  166. dataIndex: 'height',
  167. key: 'height',
  168. }, {
  169. title: '开始时间',
  170. dataIndex: 'startDate',
  171. key: 'startDate',
  172. }, {
  173. title: '结束时间',
  174. dataIndex: 'endDate',
  175. key: 'endDate',
  176. }
  177. ],
  178. tableData: []
  179. };
  180. },
  181. tableRowClick(record, index) {
  182. this.state.RowData = record;
  183. this.setState({
  184. visible: true
  185. });
  186. },
  187. componentWillMount() {
  188. this.loadKeys();
  189. this.loadData();
  190. },
  191. clearCache() {
  192. if (!this.state.searchKey) {
  193. message.warning('必须选择一个位置!');
  194. return;
  195. };
  196. this.setState({
  197. loading: true
  198. });
  199. $.ajax({
  200. method: "get",
  201. dataType: "json",
  202. crossDomain: false,
  203. url: globalConfig.context + '/api/admin/banners/clearCache',
  204. data: {
  205. key: this.state.searchKey // 位置标示,必填
  206. },
  207. success: function (data) {
  208. if (data.error && data.error.length) {
  209. message.warning(data.error[0].message);
  210. } else {
  211. message.success('更新缓存成功!');
  212. };
  213. }.bind(this),
  214. }).always(function () {
  215. this.setState({
  216. loading: false
  217. });
  218. }.bind(this));
  219. },
  220. submit() {
  221. if (!this.state.RowData.sign) {
  222. message.warning('必须选择一个位置!');
  223. return;
  224. };
  225. if (!this.state.RowData.imgurl) {
  226. message.warning('必须上传一张广告图!');
  227. return;
  228. };
  229. if (!this.state.RowData.url) {
  230. message.warning('必须填写跳转链接!');
  231. return;
  232. };
  233. this.setState({
  234. loading: true
  235. });
  236. $.ajax({
  237. method: "post",
  238. dataType: "json",
  239. crossDomain: false,
  240. url: globalConfig.context + '/api/admin/banners/save',
  241. data: {
  242. id: this.state.RowData.id || null, //主键,如果不填主键则为新增记录,填主键则为更新
  243. sign: this.state.RowData.sign, // 位置标示,必填
  244. text: this.state.RowData.text, //广告图描述,长度 255
  245. imgurl: this.state.RowData.imgurl, //广告图url,必填,长度 255
  246. url: this.state.RowData.url, //跳转链接,必填,长度255
  247. width: this.state.RowData.width, //宽度,非必填,最大99999
  248. height: this.state.RowData.height, //高度,非必填,最大99999
  249. startDate: this.state.RowData.startDate, //广告图开始时间,非必填
  250. endDate: this.state.RowData.endDate //广告图结束时间,非必填
  251. },
  252. success: function (data) {
  253. if (data.error && data.error.length) {
  254. message.warning(data.error[0].message);
  255. } else {
  256. message.success('保存成功!');
  257. this.setState({ visible: false });
  258. this.loadData();
  259. };
  260. }.bind(this),
  261. }).always(function () {
  262. this.setState({
  263. loading: false
  264. });
  265. }.bind(this));
  266. },
  267. delectRow() {
  268. if (this.state.selectedRows.length > 1) {
  269. return;
  270. };
  271. this.setState({
  272. selectedRowKeys: [],
  273. loading: this.state.selectedRows.length > 0
  274. });
  275. $.ajax({
  276. method: "POST",
  277. dataType: "json",
  278. crossDomain: false,
  279. url: globalConfig.context + "/api/admin/banners/delete",
  280. data: {
  281. id: this.state.selectedRows[0].id
  282. }
  283. }).done(function (data) {
  284. if (data.error && data.error.length) {
  285. message.warning(data.error[0].message);
  286. } else {
  287. message.success('删除成功!');
  288. };
  289. this.setState({
  290. loading: false,
  291. });
  292. this.loadData();
  293. }.bind(this));
  294. },
  295. reset() {
  296. this.state.searchKey = undefined;
  297. this.loadData();
  298. },
  299. render() {
  300. const rowSelection = {
  301. selectedRowKeys: this.state.selectedRowKeys,
  302. onChange: (selectedRowKeys, selectedRows) => {
  303. this.setState({
  304. selectedRows: selectedRows.slice(-1),
  305. selectedRowKeys: selectedRowKeys.slice(-1)
  306. });
  307. }
  308. };
  309. const hasSelected = this.state.selectedRowKeys.length > 0;
  310. const { pageNo, pageSize, total } = this.state.pagination;
  311. return (
  312. <div className="admin-content" >
  313. <div className="admin-content-title">广告图管理</div>
  314. <div className="admin-content-warp">
  315. <div className="admin-content-header">广告图列表</div>
  316. <div className="admin-content-search">
  317. <Select placeholder="选择广告图位置"
  318. style={{ width: 200 }}
  319. value={this.state.searchKey}
  320. onChange={(e) => { this.setState({ searchKey: e }) }}>
  321. {this.state.keysOption}
  322. </Select>
  323. <Button type="primary" onClick={() => { this.loadData() }}>搜索</Button>
  324. <Button type="ghost" onClick={this.clearCache}>清除缓存</Button>
  325. <Button onClick={this.reset}>重置</Button>
  326. <Button type='danger' disabled={!hasSelected} onClick={this.delectRow}>
  327. 删除<Icon type="minus" />
  328. </Button>
  329. <Button type="primary" style={{ float: 'right' }}
  330. onClick={() => { this.setState({ RowData: {}, visible: true }) }}>
  331. 添加<Icon type="plus" />
  332. </Button>
  333. </div>
  334. <Spin spinning={this.state.loading}>
  335. <Table className="no-all-select" columns={this.state.columns}
  336. dataSource={this.state.tableData}
  337. pagination={false}
  338. rowSelection={rowSelection}
  339. onRowClick={this.tableRowClick} />
  340. <div className="clearfix">
  341. <Pagination style={{ marginTop: '10px', float: 'right' }}
  342. onChange={(e) => { this.loadData(e); }}
  343. total={total}
  344. showTotal={() => { return '共' + total + '条数据' }}
  345. current={pageNo}
  346. pageSize={pageSize} />
  347. </div>
  348. </Spin>
  349. </div>
  350. <Modal title="广告图详情" width={1000}
  351. visible={this.state.visible}
  352. maskClosable={false}
  353. footer={null}
  354. afterClose={() => { this.state.RowData = {}; }}
  355. onCancel={() => { this.setState({ visible: false }) }} >
  356. <Spin spinning={this.state.loading}>
  357. <div className="modal-box">
  358. <span className="modal-box-title">广告图位置</span>
  359. {this.state.RowData.id ? <div className="modal-box-detail">
  360. {this.state.keysObj[this.state.RowData.sign]}
  361. </div> : <div className="modal-box-detail">
  362. <Select placeholder="选择广告图位置"
  363. style={{ width: 200 }}
  364. value={this.state.RowData.sign}
  365. onChange={(e) => {
  366. this.state.RowData.sign = e;
  367. this.setState({ RowData: this.state.RowData })
  368. }}>
  369. {this.state.keysOption}
  370. </Select>
  371. </div>}
  372. </div>
  373. <div className="modal-box">
  374. <span className="modal-box-title">广告图描述</span>
  375. <div className="modal-box-detail">
  376. <Input value={this.state.RowData.text}
  377. onChange={(e) => {
  378. this.state.RowData.text = e.target.value;
  379. this.setState({ RowData: this.state.RowData });
  380. }} />
  381. </div>
  382. </div>
  383. <div className="modal-box">
  384. <span className="modal-box-title">跳转链接</span>
  385. <div className="modal-box-detail">
  386. <Input value={this.state.RowData.url}
  387. onChange={(e) => {
  388. this.state.RowData.url = e.target.value;
  389. this.setState({ RowData: this.state.RowData });
  390. }} />
  391. </div>
  392. </div>
  393. <div className="modal-box">
  394. <span className="modal-box-title">图片宽度</span>
  395. <div className="modal-box-detail">
  396. <Input value={this.state.RowData.width}
  397. onChange={(e) => {
  398. this.state.RowData.width = e.target.value;
  399. this.setState({ RowData: this.state.RowData });
  400. }} />
  401. </div>
  402. </div>
  403. <div className="modal-box">
  404. <span className="modal-box-title">图片高度</span>
  405. <div className="modal-box-detail">
  406. <Input value={this.state.RowData.height}
  407. onChange={(e) => {
  408. this.state.RowData.height = e.target.value;
  409. this.setState({ RowData: this.state.RowData });
  410. }} />
  411. </div>
  412. </div>
  413. <div className="modal-box">
  414. <span className="modal-box-title">广告图播放时间</span>
  415. <div className="modal-box-detail">
  416. <DatePicker
  417. showTime
  418. format="YYYY-MM-DD HH:mm:ss"
  419. placeholder="选择开始时间"
  420. value={this.state.RowData.startDate ? moment(this.state.RowData.startDate, "YYYY-MM-DD HH:mm:ss") : undefined}
  421. onChange={(t, str) => {
  422. this.state.RowData.startDate = str;
  423. this.setState({ RowData: this.state.RowData });
  424. }} />
  425. <span>到</span>
  426. <DatePicker
  427. showTime
  428. format="YYYY-MM-DD HH:mm:ss"
  429. placeholder="选择结束时间"
  430. value={this.state.RowData.endDate ? moment(this.state.RowData.endDate, "YYYY-MM-DD HH:mm:ss") : undefined}
  431. onChange={(t, str) => {
  432. this.state.RowData.endDate = str;
  433. this.setState({ RowData: this.state.RowData });
  434. }} />
  435. </div>
  436. </div>
  437. <div className="modal-box">
  438. <span className="modal-box-title">上传图片</span>
  439. <div className="modal-box-detail clearfix">
  440. <Avatar sign={this.state.RowData.sign} id={this.state.RowData.id}
  441. imageUrl={this.state.RowData.imgurl ? (globalConfig.avatarHost + "/upload" + this.state.RowData.imgurl) : null}
  442. visible={this.state.visible}
  443. urlData={(url) => { this.state.RowData.imgurl = url }} />
  444. </div>
  445. </div>
  446. <div className="modal-box">
  447. <Button type="primary" onClick={this.submit}>提交</Button>
  448. <Button type="ghost" onClick={() => { this.setState({ visible: false }) }}>取消</Button>
  449. </div>
  450. </Spin>
  451. </Modal>
  452. </div>
  453. );
  454. }
  455. });
  456. export default Lecture;