modal.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Col, Drawer, Row, Empty, message, Spin } from 'antd';
  3. import BannerItem from './item';
  4. import { opraBanner, getBanner } from '@/services/notice';
  5. import { IBanner, EJumpType } from './types';
  6. import { Role } from '@/models/user';
  7. interface IProps {
  8. visible: boolean;
  9. opraModal: (_state: boolean, reloadTable?: boolean) => void;
  10. roleData: Role;
  11. }
  12. interface IState {
  13. bannerList: IBanner[];
  14. }
  15. const initBanner = {
  16. imgUrl: '',
  17. isShow: false,
  18. jumpAppId: '',
  19. jumpPath: '',
  20. jumpType: EJumpType.h5,
  21. sort: 0,
  22. roleId: '',
  23. roleName: '',
  24. };
  25. const BannerDrawer: React.FC<IProps> = ({ visible, opraModal, roleData }) => {
  26. const [state, setState] = useState<IState>({
  27. bannerList: [],
  28. });
  29. const [loading, setLoading] = useState<boolean>(false);
  30. const { bannerList } = state;
  31. useEffect(() => {
  32. console.log('我触发么');
  33. console.log(roleData, 'roleDataroleDataroleDataroleData');
  34. if (typeof roleData === 'object' && Reflect.ownKeys(roleData).length && visible) {
  35. GetBanner();
  36. }
  37. }, [visible]);
  38. // 新增banner
  39. const addBanner = () => {
  40. if (bannerList.length >= 8) {
  41. message.warn('最多添加八张轮播图');
  42. return;
  43. }
  44. const $par = Object.assign({}, initBanner, {
  45. sort: 0,
  46. roleId: roleData.id,
  47. roleName: roleData.label,
  48. uid: Math.random().toString(36).slice(2),
  49. });
  50. bannerList.unshift($par);
  51. console.log(bannerList, 'bannerListbannerList');
  52. setState({
  53. ...state,
  54. bannerList: bannerList.map((item: IBanner, index: number) => {
  55. return {
  56. ...item,
  57. sort: index,
  58. };
  59. }),
  60. });
  61. };
  62. // 查询banner
  63. const GetBanner = async () => {
  64. setLoading(true);
  65. const { status, data } = await getBanner(roleData.id);
  66. setLoading(false);
  67. console.log(status, data);
  68. const $r = data.map((item: IBanner) => {
  69. return {
  70. ...item,
  71. roleName: roleData.label,
  72. uid: item.id,
  73. };
  74. });
  75. if (status === 200) {
  76. setState({
  77. ...state,
  78. bannerList: $r,
  79. });
  80. }
  81. };
  82. // 收集所有item的结果
  83. const collecrtItem = (itemData: IBanner) => {
  84. console.log(itemData, '收集所有item的结果');
  85. const r = bannerList.map((_banner: IBanner) => {
  86. if (_banner.uid === itemData.uid) {
  87. return {
  88. ...itemData,
  89. jumpAppId: itemData.jumpAppId?.trim(),
  90. jumpPath: itemData.jumpPath?.trim(),
  91. };
  92. } else {
  93. return _banner;
  94. }
  95. });
  96. setState({
  97. bannerList: r,
  98. });
  99. };
  100. // 删除当前的item(还未保存前的)
  101. const delItem = (uid: string) => {
  102. const r = bannerList.filter((_banner) => _banner.uid !== uid);
  103. setState({
  104. ...state,
  105. bannerList: r,
  106. });
  107. };
  108. // 验证当前banner数据是否完整
  109. const verifyBanner = (bannerList: IBanner[]) => {
  110. let obj = {
  111. r: true,
  112. msg: '保存成功',
  113. index: 0,
  114. };
  115. bannerList.forEach((_banner, index) => {
  116. if (_banner.jumpType === EJumpType.h5 && !_banner.jumpPath) {
  117. obj = {
  118. r: false,
  119. msg: '存在跳转地址未填写',
  120. index,
  121. };
  122. }
  123. // else if (_banner.jumpType === EJumpType.h5 && !_banner.jumpPath) {
  124. // obj = {
  125. // r: false,
  126. // msg: '存在appid未填写',
  127. // index,
  128. // };
  129. // }
  130. else if (!_banner.imgUrl) {
  131. obj = {
  132. r: false,
  133. msg: '图片封面未上传',
  134. index,
  135. };
  136. }
  137. });
  138. if (!obj.r) {
  139. message.error('第' + (obj.index + 1) + '项' + obj.msg);
  140. return false;
  141. }
  142. return true;
  143. };
  144. // 对banner进行添加或者修改
  145. const OpraBanner = async () => {
  146. const r = verifyBanner(bannerList);
  147. if (!r) return;
  148. const { status, data } = await opraBanner(bannerList, roleData.id);
  149. if (status === 200 && data) {
  150. message.success('保存成功');
  151. opraModal(false, true);
  152. }
  153. };
  154. const DrawerFooter = () => {
  155. return (
  156. <Row justify="end">
  157. <Col span={6}>
  158. <Button onClick={() => opraModal(false)}> 关闭 </Button>
  159. </Col>
  160. <Col span={6}>
  161. <Button type="primary" onClick={OpraBanner}>
  162. 保存
  163. </Button>
  164. </Col>
  165. </Row>
  166. );
  167. };
  168. const DrawerTitle = () => {
  169. return (
  170. <Row align="middle">
  171. <Col span="16">编辑轮播图</Col>
  172. <Col span="6">
  173. <Button type="primary" onClick={addBanner}>
  174. 新增
  175. </Button>
  176. </Col>
  177. </Row>
  178. );
  179. };
  180. return (
  181. <Drawer
  182. title={DrawerTitle()}
  183. placement="right"
  184. visible={visible}
  185. width={420}
  186. closable={false}
  187. onClose={() => opraModal(false)}
  188. maskClosable
  189. footer={DrawerFooter()}
  190. >
  191. <Spin spinning={loading}>
  192. {bannerList.length ? (
  193. bannerList.map((item, index) => (
  194. <BannerItem key={index} itemData={item} delItem={delItem} collecrtItem={collecrtItem} />
  195. ))
  196. ) : (
  197. <Empty description="这里空空如也" image={Empty.PRESENTED_IMAGE_SIMPLE} />
  198. )}
  199. </Spin>
  200. </Drawer>
  201. );
  202. };
  203. export default BannerDrawer;