message.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 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.getMessageList();
  57. }
  58. //数据初始化
  59. initData = async () => {
  60. this.setState({
  61. listData: [],
  62. isLoading: false,
  63. showFoot: 0,
  64. });
  65. pageNo = 1;
  66. await this.getMessageList();
  67. };
  68. //上拉加载
  69. onEndReached = async () => {
  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. await this.getMessageList();
  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. getMessageList = async () => {
  120. let account = await RetrieveData('account');
  121. let token = await RetrieveData('token');
  122. if (token && account) {
  123. const url = '/auth/user/msg/findUserMsg';
  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. console.log(res);
  137. if (res.code === 0) {
  138. totalPage = res.data.pages;
  139. this.setList(res.data.records);
  140. } else {
  141. await this.abnormalMessage(res);
  142. }
  143. }
  144. }
  145. };
  146. //设置抬头列表
  147. setList = (data) => {
  148. let listDatas = data.map((_, i) => ({
  149. key: data[i].msgId,
  150. msgType: data[i].msgType,
  151. msgTitle: data[i].msgTitle,
  152. msgBrief: data[i].msgBrief,
  153. entTaxId: data[i].entTaxId,
  154. ispId: data[i].ispId,
  155. userId: data[i].userId,
  156. createTime: data[i].createTime,
  157. }));
  158. let list = this.state.listData.concat(listDatas);
  159. this.setState({
  160. listData: list,
  161. showFoot: 0,
  162. });
  163. };
  164. //显示发票抬头列表
  165. renderItem = (data) => (
  166. <List.Item
  167. style={{backgroundColor: '#F7F7F7'}}
  168. onPress={() => {
  169. this.props.children.props.navigation.navigate('message_info', {
  170. id: data.item.key,
  171. });
  172. }}>
  173. <View style={{backgroundColor: '#ffffff'}}>
  174. <View style={{margin: 10}}>
  175. <Text
  176. style={{fontWeight: 'bold', fontSize: 16, color: '#050505'}}
  177. ellipsizeMode={'tail'}
  178. numberOfLines={1}>
  179. {data.item.msgTitle}
  180. </Text>
  181. <Text style={{color: '#898989', marginTop: 5}}>
  182. 简介:{data.item.msgBrief}
  183. </Text>
  184. </View>
  185. <WingBlank>
  186. <View style={{borderTopWidth: 1, borderColor: '#D9D9D9'}} />
  187. </WingBlank>
  188. <View style={{flexDirection: 'row', margin: 10}}>
  189. <View style={{flex: 1, justifyContent: 'center'}}>
  190. <Text>{data.item.createTime}</Text>
  191. </View>
  192. </View>
  193. </View>
  194. </List.Item>
  195. );
  196. // 处理网络请求数据
  197. abnormalMessage = async (jason) => {
  198. if (jason.code === 401) {
  199. await Alert.alert(
  200. '登录失效',
  201. '登录状态已失效,请重新登录!',
  202. [
  203. {
  204. text: '确定',
  205. onPress: () => {
  206. CleanAll();
  207. this.props.navigation.popToTop();
  208. },
  209. },
  210. ],
  211. {cancelable: false},
  212. );
  213. }
  214. if (jason.code === 403) {
  215. Alert.alert(
  216. '权限',
  217. '暂无权限操作此模块!',
  218. [
  219. {
  220. text: '确定',
  221. onPress: () => {
  222. this.props.navigation.goBack();
  223. },
  224. },
  225. ],
  226. {cancelable: false},
  227. );
  228. }
  229. if (jason.code !== 401 && jason.code !== 403) {
  230. ToastShow(1, jason.msg);
  231. }
  232. };
  233. }