notice_message.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import React, {Component} from 'react';
  2. import {
  3. View,
  4. FlatList,
  5. Text,
  6. ActivityIndicator,
  7. RefreshControl,
  8. Alert,
  9. SafeAreaView,
  10. } from 'react-native';
  11. import {List, SwipeAction, WingBlank} from '@ant-design/react-native';
  12. import public_css from '../../source/css/public_css';
  13. import {RetrieveData} from '../../data/storage';
  14. import {RequestNetwork} from '../../data/encryption';
  15. import notice_css from './notice_css';
  16. import {ToastShow} from '../../components/toast/toast';
  17. import {CleanAll} from '../../components/abnormalMessage/abnormal_message';
  18. let pageNo = 1; //当前第几页
  19. let totalPage = 5; //总的页数
  20. export default class notice_message extends Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. listData: [],
  25. showFoot: 0, // 控制foot, 0:隐藏footer 1:已加载完成,没有更多数据 2 :显示加载中
  26. isLoading: false,
  27. };
  28. }
  29. render() {
  30. return (
  31. <SafeAreaView style={public_css.body}>
  32. <FlatList
  33. data={this.state.listData}
  34. renderItem={(item) => this.renderItem(item)}
  35. refreshControl={
  36. <RefreshControl
  37. title={'加载中...'}
  38. colors={['#B7B7B7']} //此颜色无效
  39. tintColor={'#B7B7B7'}
  40. titleColor={'#B7B7B7'} //只有ios有效
  41. refreshing={this.state.isLoading}
  42. onRefresh={() => {
  43. this.initData();
  44. }}
  45. />
  46. }
  47. ListFooterComponent={() => this.renderFooter()}
  48. onEndReached={() => this.onEndReached()}
  49. onEndReachedThreshold={0.1}
  50. />
  51. </SafeAreaView>
  52. );
  53. }
  54. //页面加载完成后加载数据
  55. async componentDidMount(): void {
  56. await this.getNoticeMessageList();
  57. }
  58. //数据初始化
  59. initData = async () => {
  60. this.setState({
  61. listData: [],
  62. isLoading: false,
  63. showFoot: 0,
  64. });
  65. pageNo = 1;
  66. await this.getNoticeMessageList();
  67. };
  68. //上拉加载
  69. onEndReached = () => {
  70. if (this.state.showFoot === 0) {
  71. if (pageNo >= totalPage) {
  72. this.setState({showFoot: 1});
  73. return;
  74. } else {
  75. pageNo++;
  76. this.setState({showFoot: 2});
  77. //获取数据
  78. this.getNoticeMessageList();
  79. }
  80. }
  81. };
  82. //列表尾部显示
  83. renderFooter = () => {
  84. if (this.state.showFoot === 1) {
  85. return (
  86. <View
  87. style={{
  88. height: 30,
  89. alignItems: 'center',
  90. justifyContent: 'flex-start',
  91. }}>
  92. <Text
  93. style={{
  94. color: '#999999',
  95. fontSize: 14,
  96. marginTop: 5,
  97. marginBottom: 5,
  98. }}>
  99. 没有更多数据了
  100. </Text>
  101. </View>
  102. );
  103. } else if (this.state.showFoot === 2) {
  104. return (
  105. <View style={notice_css.footer}>
  106. <ActivityIndicator />
  107. <Text>正在加载更多数据...</Text>
  108. </View>
  109. );
  110. } else if (this.state.showFoot === 0) {
  111. return (
  112. <View style={notice_css.footer}>
  113. <Text />
  114. </View>
  115. );
  116. }
  117. };
  118. //获取公告列表数据
  119. getNoticeMessageList = async () => {
  120. let account = await RetrieveData('account');
  121. let token = await RetrieveData('token');
  122. if (token && account) {
  123. const url = '/auth/user/msg/findGlobalNotice';
  124. let res = await RequestNetwork(
  125. url,
  126. token,
  127. {
  128. mobile: account,
  129. pageNum: pageNo,
  130. pageSize: 10,
  131. },
  132. false,
  133. 2,
  134. );
  135. if (res) {
  136. if (res.code === 0) {
  137. totalPage = res.data.pages;
  138. this.setList(res.data.records);
  139. } else {
  140. await this.abnormalMessage(res);
  141. }
  142. }
  143. }
  144. };
  145. //设置抬头列表
  146. setList = (data) => {
  147. let listDatas = data.map((_, i) => ({
  148. key: data[i].msgId,
  149. msgType: data[i].msgType,
  150. msgTitle: data[i].msgTitle,
  151. msgBrief: data[i].msgBrief,
  152. hasRead: data[i].hasRead,
  153. createTime: data[i].createTime,
  154. }));
  155. let list = this.state.listData.concat(listDatas);
  156. this.setState({
  157. listData: list,
  158. showFoot: 0,
  159. });
  160. };
  161. //显示发票抬头列表
  162. renderItem = (data) => (
  163. <SwipeAction autoClose style={{backgroundColor: 'transparent'}}>
  164. <List.Item style={{backgroundColor: '#F7F7F7'}}>
  165. <View style={{backgroundColor: '#ffffff'}}>
  166. <View style={{margin: 10}}>
  167. <Text
  168. style={{fontWeight: 'bold', fontSize: 16, color: '#050505'}}
  169. ellipsizeMode={'tail'}
  170. numberOfLines={1}>
  171. {data.item.msgTitle}
  172. </Text>
  173. <Text style={{color: '#898989', marginTop: 5}}>
  174. 简介:{data.item.msgBrief}
  175. </Text>
  176. </View>
  177. <WingBlank>
  178. <View style={{borderTopWidth: 1, borderColor: '#D9D9D9'}} />
  179. </WingBlank>
  180. <View style={{flexDirection: 'row', margin: 10}}>
  181. <View style={{flex: 1, justifyContent: 'center'}}>
  182. <Text>{data.item.createTime}</Text>
  183. </View>
  184. </View>
  185. </View>
  186. </List.Item>
  187. </SwipeAction>
  188. );
  189. // 处理网络请求数据
  190. abnormalMessage = async (jason) => {
  191. if (jason.code === 401) {
  192. await Alert.alert(
  193. '登录失效',
  194. '登录状态已失效,请重新登录!',
  195. [
  196. {
  197. text: '确定',
  198. onPress: () => {
  199. CleanAll();
  200. this.props.navigation.popToTop();
  201. },
  202. },
  203. ],
  204. {cancelable: false},
  205. );
  206. }
  207. if (jason.code === 403) {
  208. Alert.alert(
  209. '权限',
  210. '暂无权限操作此模块!',
  211. [
  212. {
  213. text: '确定',
  214. onPress: () => {
  215. this.props.navigation.goBack();
  216. },
  217. },
  218. ],
  219. {cancelable: false},
  220. );
  221. }
  222. if (jason.code !== 401 && jason.code !== 403) {
  223. ToastShow(1, jason.msg);
  224. }
  225. };
  226. }