import React, {Component} from 'react'; import { View, Image, Text, Dimensions, FlatList, RefreshControl, ActivityIndicator, DeviceEventEmitter, } from 'react-native'; import {List, SearchBar, SwipeAction} from '@ant-design/react-native'; import {RetrieveData} from '../../data/storage'; import {GetDataPost} from '../../data/encryption'; import {ShowToast} from '../../components/rootToast/root_toast'; import customer_css from './customer_css'; let pageNo = 1; //当前第几页 let totalPage = 5; //总的页数 export default class customer_information extends Component { constructor(props) { super(props); this.props.navigation.dangerouslyGetParent().setOptions({ tabBarVisible: false, }); this.state = { listData: [], search_data: '', showFoot: 0, // 控制foot, 0:隐藏footer 1:已加载完成,没有更多数据 2 :显示加载中 isLoading: false, }; } render() { return ( Alert.alert(value)} onCancel={() => this.searchClear()} onChange={(value) => this.searchData(value)} showCancelButton={false} style={{borderRadius: 15}} /> this.renderItem(item)} refreshControl={ { this.initData(); }} /> } ListFooterComponent={this._renderFooter.bind(this)} onEndReached={this._onEndReached.bind(this)} onEndReachedThreshold={0.1} /> ); } //搜索数据 searchData = value => { this.setState({ search_data: value, }); this.timer = setTimeout(() => { this.initData(); }, 1000); }; //清空搜索 searchClear = () => { this.setState({ search_data: '', }); this.initData(); }; //数据初始化 initData = () => { this.setState({ listData: [], isLoading: false, showFoot: 0, }); pageNo = 1; this.getCustomerData(); }; //上拉加载 _onEndReached() { if (this.state.showFoot === 0) { if (pageNo >= totalPage) { this.setState({showFoot: 1}); return; } else { pageNo++; this.setState({showFoot: 2}); //获取数据 this.getCustomerData(); } } } //显示list尾部 _renderFooter() { if (this.state.showFoot === 1) { return ( 没有更多数据了 ); } else if (this.state.showFoot === 2) { return ( 正在加载更多数据... ); } else if (this.state.showFoot === 0) { return ( ); } } //显示页面加载 componentDidMount(): void { this.getCustomerData(); //收到监听 this.listener = DeviceEventEmitter.addListener('刷新列表', (message) => { //收到监听后想做的事情 this.initData(); }); } //关闭页面加载 componentWillUnmount(): void { pageNo = 1; this.timer && clearTimeout(this.timer); //移除监听 if (this.listener) { this.listener.remove(); } } //获取客户数据 getCustomerData = async () => { let account = await RetrieveData('account'); let token = await RetrieveData('token'); if (token && account) { account = account.substring(1, account.length - 1); token = token.substring(1, token.length - 1); const url = '/sys/customer/findPage'; GetDataPost( url, token, { mobile: account, reqChannel: 3, pageNum: pageNo, pageSize: 10, customerName: this.state.search_data, }, false, 2, ).then(res => { totalPage = res.data.pages; this.setList(res.data.records); }); } }; //删除客户数据 deleteData = async data => { let account = await RetrieveData('account'); let token = await RetrieveData('token'); if (account && token) { account = account.substring(1, account.length - 1); token = token.substring(1, token.length - 1); const url = '/sys/customer/delete'; GetDataPost( url, token, { mobile: account, reqChannel: 3, ip: '192.168.0.12', delList: [ { customerId: data.key, // customerCode: data.customerCode, manageUserId: data.manageUserId, }, ], }, false, 1, ) .then(res => { if (res) { if (res.code === 0) { ShowToast('删除成功'); this.initData(); } else { ShowToast('删除失败'); } } else { ShowToast('删除失败'); } }) .catch(err => { ShowToast('删除失败'); }); } }; //设置客户数据列表 setList = data => { let listDatas = data.map((_, i) => ({ key: data[i].customerId, customerName: data[i].customerName, entTaxId: data[i].entTaxId, customerMobile: data[i].customerMobile, manageUserId: data[i].manageUserId, })); let list = this.state.listData.concat(listDatas); this.setState({ listData: list, showFoot: 0, }); }; //加载客户列表数据 renderItem = data => ( { this.props.navigation.navigate('customer_see', { companyId: data.item.key, }); }}> {data.item.customerName} {data.item.entTaxId} {data.item.customerMobile} ); //显示侧滑按钮 right = data => [ { text: '编辑', onPress: () => this.props.navigation.navigate('customer_edit', { companyId: data.item.key, }), style: {backgroundColor: 'orange', color: 'white'}, }, { text: '删除', onPress: () => { this.deleteData(data.item); }, style: {backgroundColor: 'red', color: 'white'}, }, ]; }