banner.jsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { Component } from 'react';
  2. import { Input, Select, Spin, Table, message, Button,Icon } from 'antd';
  3. import BannerFrom from '@/administration/banner/bannerDes.jsx';
  4. import { splitUrl} from '@/tools.js';
  5. import Crop from '@/crop/cropBlock';
  6. class Banner extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. loading: false,
  11. dataSource: [],
  12. showDesc:false,
  13. selectedRowKey:[],
  14. selectedRowKeys:[],
  15. pagination: {
  16. defaultCurrent: 1,
  17. defaultPageSize: 10,
  18. showQuickJumper: true,
  19. pageSize: 10,
  20. onChange: function(page) {
  21. this.loadData(page);
  22. this.setState({
  23. selectedRowKeys: []
  24. });
  25. }.bind(this),
  26. showTotal: function(total) {
  27. return '共' + total + '条数据';
  28. }
  29. },
  30. columns: [
  31. {
  32. title: '轮播图描述',
  33. dataIndex: 'text',
  34. key: 'text'
  35. },
  36. {
  37. title: '跳转地址',
  38. dataIndex: 'forwardUrl',
  39. key: 'forwardUrl'
  40. },
  41. // {
  42. // title: '位置',
  43. // dataIndex: 'client',
  44. // key: 'client',
  45. // render:(text=>{
  46. // return text==1?'APP':'网站'
  47. // })
  48. // },
  49. {
  50. title: '轮播图API',
  51. dataIndex: 'apiUrl',
  52. key: 'apiUrl'
  53. },
  54. ]
  55. };
  56. }
  57. loadData(pageNo) {
  58. this.setState({
  59. page:pageNo,
  60. loading: true,
  61. selectedRowKeys:[],
  62. });
  63. $.ajax({
  64. method: "get",
  65. dataType: "json",
  66. crossDomain: false,
  67. url: globalConfig.context + "/api/admin/banners/list",
  68. data: {
  69. pageNo: pageNo || 1,
  70. pageSize: this.state.pagination.pageSize,
  71. apiUrl:this.state.apiSearch,
  72. client:this.state.positionSearch,
  73. },
  74. success: function (data) {
  75. let theArr = [];
  76. if (!data.data || !data.data.list) {
  77. if (data.error && data.error.length) {
  78. message.warning(data.error[0].message);
  79. };
  80. } else {
  81. for (let i = 0; i < data.data.list.length; i++) {
  82. let thisdata = data.data.list[i];
  83. theArr.push({
  84. key:i,
  85. id: thisdata.id, //品类ID
  86. text: thisdata.text, //品类序号
  87. apiUrl:thisdata.apiUrl,
  88. forwardUrl:thisdata.forwardUrl,
  89. client:thisdata.client,
  90. imgUrl: thisdata.imgUrl ? splitUrl(thisdata.imgUrl, ',', globalConfig.avatarHost + '/upload') : [],
  91. });
  92. };
  93. }
  94. this.state.pagination.current = data.data.pageNo;
  95. this.state.pagination.total = data.data.totalCount;
  96. if(data.data&&data.data.list&&!data.data.list.length){
  97. this.state.pagination.current=0
  98. this.state.pagination.total=0
  99. }
  100. this.setState({
  101. dataSource: theArr,
  102. pagination: this.state.pagination
  103. });
  104. }.bind(this),
  105. }).always(function () {
  106. this.setState({
  107. loading: false
  108. });
  109. }.bind(this));
  110. }
  111. search() {
  112. this.loadData();
  113. }
  114. reset() {
  115. this.state.apiSearch = '';
  116. this.state.positionSearch = undefined;
  117. this.loadData();
  118. }
  119. closeDesc(e, s) {
  120. this.state.showDesc = e;
  121. if (s) {
  122. this.loadData(this.state.page);
  123. };
  124. }
  125. addClick() {
  126. this.state.RowData = {};
  127. this.setState({
  128. showDesc: true
  129. });
  130. }
  131. tableRowClick(record, index) {
  132. this.state.RowData = record;
  133. this.setState({
  134. showDesc: true,
  135. });
  136. }
  137. delectRow() {
  138. this.setState({
  139. loading:true
  140. })
  141. let deletedIds =[];
  142. let rowItem = this.state.selectedRowKeys[0];
  143. let data = this.state.dataSource ||[];
  144. if (data.length) {
  145. deletedIds.push(data[rowItem].id);
  146. }
  147. $.ajax({
  148. method: "POST",
  149. dataType: "json",
  150. crossDomain: false,
  151. url: globalConfig.context + "/api/admin/banners/delete",
  152. data: {
  153. id: deletedIds[0]
  154. }
  155. }).done(function (data) {
  156. if (!data.error.length) {
  157. message.success('删除成功!');
  158. this.setState({
  159. loading: false
  160. });
  161. this.loadData(this.state.page);
  162. } else {
  163. message.warning(data.error[0].message);
  164. };
  165. }.bind(this));
  166. }
  167. componentWillMount() {
  168. this.loadData();
  169. }
  170. render() {
  171. const rowSelection = {
  172. selectedRowKeys: this.state.selectedRowKeys,
  173. onChange: (selectedRowKeys, selectedRows) => {
  174. this.setState({
  175. selectedRows: selectedRows.slice(-1),
  176. selectedRowKeys: selectedRowKeys.slice(-1)
  177. });
  178. }
  179. };
  180. const hasSelect = this.state.selectedRowKeys.length;
  181. return (
  182. <div className="user-content">
  183. <div className="content-title">
  184. <div className="content-title">
  185. <span>轮播图库</span>
  186. <div className="patent-addNew">
  187. <Button type="primary" onClick={this.addClick.bind(this)}>新建轮播图<Icon type="plus" /></Button>
  188. </div>
  189. </div>
  190. <div className="user-search">
  191. <Input
  192. style={{ marginRight: 10,width:200 }}
  193. value={this.state.apiSearch}
  194. placeholder="API"
  195. onChange={(e) => {
  196. this.setState({ apiSearch: e.target.value });
  197. }}
  198. />
  199. <Select style={{width:150}}
  200. placeholder="轮播图位置"
  201. value={this.state.positionSearch}
  202. onChange={(e)=>{this.setState({positionSearch:e})}}>
  203. <Select.Option value="0">网站</Select.Option>
  204. <Select.Option value="1">App</Select.Option>
  205. </Select>
  206. <Button type="primary" onClick={this.search.bind(this)} style={{ marginRight: 10 }}>
  207. 搜索
  208. </Button>
  209. <Button onClick={this.reset.bind(this)} style={{ marginRight: 10 }}>重置</Button>
  210. <Button type="danger" disabled={!hasSelect} onClick={(e)=>{this.delectRow()}}>删除</Button>
  211. </div>
  212. <div className="patent-table">
  213. <Spin spinning={this.state.loading}>
  214. <Table
  215. columns={this.state.columns}
  216. dataSource={this.state.dataSource}
  217. rowSelection={rowSelection}
  218. pagination={this.state.pagination}
  219. onRowClick={this.tableRowClick.bind(this)}
  220. />
  221. </Spin>
  222. </div>
  223. </div>
  224. <BannerFrom
  225. data={this.state.RowData}
  226. showDesc={this.state.showDesc}
  227. closeDesc={this.closeDesc.bind(this)}
  228. />
  229. </div>
  230. );
  231. }
  232. }
  233. export default Banner;