customer_information.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import React, {Component} from 'react';
  2. import {
  3. View,
  4. Image,
  5. Text,
  6. Dimensions,
  7. FlatList,
  8. RefreshControl,
  9. ActivityIndicator,
  10. DeviceEventEmitter,
  11. } from 'react-native';
  12. import {List, SearchBar, SwipeAction} from '@ant-design/react-native';
  13. import {RetrieveData} from '../../data/storage';
  14. import {GetDataPost} from '../../data/encryption';
  15. import {ShowToast} from '../../components/rootToast/root_toast';
  16. import customer_css from './customer_css';
  17. let pageNo = 1; //当前第几页
  18. let totalPage = 5; //总的页数
  19. export default class customer_information extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.props.navigation.dangerouslyGetParent().setOptions({
  23. tabBarVisible: false,
  24. });
  25. this.state = {
  26. listData: [],
  27. search_data: '',
  28. showFoot: 0, // 控制foot, 0:隐藏footer 1:已加载完成,没有更多数据 2 :显示加载中
  29. isLoading: false,
  30. };
  31. }
  32. render() {
  33. return (
  34. <View style={{flex: 1, backgroundColor: '#ffffff'}}>
  35. <View
  36. style={{
  37. width: Dimensions.get('window').width,
  38. alignItems: 'center',
  39. justifyContent: 'center',
  40. backgroundColor: '#ffffff',
  41. height: 50,
  42. }}>
  43. <SearchBar
  44. placeholder="搜索"
  45. value={this.state.search_data}
  46. // onSubmit={value => Alert.alert(value)}
  47. onCancel={() => this.searchClear()}
  48. onChange={(value) => this.searchData(value)}
  49. showCancelButton={false}
  50. style={{borderRadius: 15}}
  51. />
  52. </View>
  53. <View style={customer_css.container}>
  54. <FlatList
  55. data={this.state.listData}
  56. renderItem={item => this.renderItem(item)}
  57. refreshControl={
  58. <RefreshControl
  59. title={'加载中...'}
  60. colors={['#B7B7B7']} //此颜色无效
  61. tintColor={'#B7B7B7'}
  62. titleColor={'#B7B7B7'} //只有ios有效
  63. refreshing={this.state.isLoading}
  64. onRefresh={() => {
  65. this.initData();
  66. }}
  67. />
  68. }
  69. ListFooterComponent={this._renderFooter.bind(this)}
  70. onEndReached={this._onEndReached.bind(this)}
  71. onEndReachedThreshold={0.1}
  72. />
  73. </View>
  74. </View>
  75. );
  76. }
  77. //搜索数据
  78. searchData = value => {
  79. this.setState({
  80. search_data: value,
  81. });
  82. this.timer = setTimeout(() => {
  83. this.initData();
  84. }, 1000);
  85. };
  86. //清空搜索
  87. searchClear = () => {
  88. this.setState({
  89. search_data: '',
  90. });
  91. this.initData();
  92. };
  93. //数据初始化
  94. initData = () => {
  95. this.setState({
  96. listData: [],
  97. isLoading: false,
  98. showFoot: 0,
  99. });
  100. pageNo = 1;
  101. this.getCustomerData();
  102. };
  103. //上拉加载
  104. _onEndReached() {
  105. if (this.state.showFoot === 0) {
  106. if (pageNo >= totalPage) {
  107. this.setState({showFoot: 1});
  108. return;
  109. } else {
  110. pageNo++;
  111. this.setState({showFoot: 2});
  112. //获取数据
  113. this.getCustomerData();
  114. }
  115. }
  116. }
  117. //显示list尾部
  118. _renderFooter() {
  119. if (this.state.showFoot === 1) {
  120. return (
  121. <View
  122. style={{
  123. height: 30,
  124. alignItems: 'center',
  125. justifyContent: 'flex-start',
  126. }}>
  127. <Text
  128. style={{
  129. color: '#999999',
  130. fontSize: 14,
  131. marginTop: 5,
  132. marginBottom: 5,
  133. }}>
  134. 没有更多数据了
  135. </Text>
  136. </View>
  137. );
  138. } else if (this.state.showFoot === 2) {
  139. return (
  140. <View style={customer_css.footer}>
  141. <ActivityIndicator />
  142. <Text>正在加载更多数据...</Text>
  143. </View>
  144. );
  145. } else if (this.state.showFoot === 0) {
  146. return (
  147. <View style={customer_css.footer}>
  148. <Text />
  149. </View>
  150. );
  151. }
  152. }
  153. //显示页面加载
  154. componentDidMount(): void {
  155. this.getCustomerData();
  156. //收到监听
  157. this.listener = DeviceEventEmitter.addListener('刷新列表', (message) => {
  158. //收到监听后想做的事情
  159. this.initData();
  160. });
  161. }
  162. //关闭页面加载
  163. componentWillUnmount(): void {
  164. pageNo = 1;
  165. this.timer && clearTimeout(this.timer);
  166. //移除监听
  167. if (this.listener) {
  168. this.listener.remove();
  169. }
  170. }
  171. //获取客户数据
  172. getCustomerData = async () => {
  173. let account = await RetrieveData('account');
  174. let token = await RetrieveData('token');
  175. if (token && account) {
  176. account = account.substring(1, account.length - 1);
  177. token = token.substring(1, token.length - 1);
  178. const url = '/sys/customer/findPage';
  179. GetDataPost(
  180. url,
  181. token,
  182. {
  183. mobile: account,
  184. reqChannel: 3,
  185. pageNum: pageNo,
  186. pageSize: 10,
  187. customerName: this.state.search_data,
  188. },
  189. false,
  190. 2,
  191. ).then(res => {
  192. totalPage = res.data.pages;
  193. this.setList(res.data.records);
  194. });
  195. }
  196. };
  197. //删除客户数据
  198. deleteData = async data => {
  199. let account = await RetrieveData('account');
  200. let token = await RetrieveData('token');
  201. if (account && token) {
  202. account = account.substring(1, account.length - 1);
  203. token = token.substring(1, token.length - 1);
  204. const url = '/sys/customer/delete';
  205. GetDataPost(
  206. url,
  207. token,
  208. {
  209. mobile: account,
  210. reqChannel: 3,
  211. ip: '192.168.0.12',
  212. delList: [
  213. {
  214. customerId: data.key,
  215. // customerCode: data.customerCode,
  216. manageUserId: data.manageUserId,
  217. },
  218. ],
  219. },
  220. false,
  221. 1,
  222. )
  223. .then(res => {
  224. if (res) {
  225. if (res.code === 0) {
  226. ShowToast('删除成功');
  227. this.initData();
  228. } else {
  229. ShowToast('删除失败');
  230. }
  231. } else {
  232. ShowToast('删除失败');
  233. }
  234. })
  235. .catch(err => {
  236. ShowToast('删除失败');
  237. });
  238. }
  239. };
  240. //设置客户数据列表
  241. setList = data => {
  242. let listDatas = data.map((_, i) => ({
  243. key: data[i].customerId,
  244. customerName: data[i].customerName,
  245. entTaxId: data[i].entTaxId,
  246. customerMobile: data[i].customerMobile,
  247. manageUserId: data[i].manageUserId,
  248. }));
  249. let list = this.state.listData.concat(listDatas);
  250. this.setState({
  251. listData: list,
  252. showFoot: 0,
  253. });
  254. };
  255. //加载客户列表数据
  256. renderItem = data => (
  257. <SwipeAction
  258. autoClose
  259. style={{backgroundColor: 'transparent'}}
  260. right={this.right(data)}>
  261. <List.Item
  262. onPress={() => {
  263. this.props.navigation.navigate('customer_see', {
  264. companyId: data.item.key,
  265. });
  266. }}>
  267. <View style={{flex: 1}}>
  268. <View style={{flexDirection: 'row', alignItems: 'center'}}>
  269. <Image
  270. source={require('../../source/img/customer/company.png')}
  271. style={{width: 21, height: 19}}
  272. />
  273. <Text
  274. style={{
  275. fontSize: 18,
  276. fontFamily: 'PingFang-SC-Regular',
  277. color: '#333333',
  278. }}>
  279. {data.item.customerName}
  280. </Text>
  281. </View>
  282. <View style={{flexDirection: 'row', alignItems: 'center'}}>
  283. <Image
  284. source={require('../../source/img/customer/companyEnt.png')}
  285. style={{width: 21, height: 19}}
  286. />
  287. <Text
  288. style={{
  289. fontSize: 18,
  290. fontFamily: 'PingFang-SC-Regular',
  291. color: '#333333',
  292. }}>
  293. {data.item.entTaxId}
  294. </Text>
  295. </View>
  296. <View style={{flexDirection: 'row', alignItems: 'center'}}>
  297. <Image
  298. source={require('../../source/img/customer/phone.png')}
  299. style={{width: 21, height: 19}}
  300. />
  301. <Text
  302. style={{
  303. fontSize: 18,
  304. fontFamily: 'PingFang-SC-Regular',
  305. color: '#333333',
  306. }}>
  307. {data.item.customerMobile}
  308. </Text>
  309. </View>
  310. </View>
  311. </List.Item>
  312. </SwipeAction>
  313. );
  314. //显示侧滑按钮
  315. right = data => [
  316. {
  317. text: '编辑',
  318. onPress: () =>
  319. this.props.navigation.navigate('customer_edit', {
  320. companyId: data.item.key,
  321. }),
  322. style: {backgroundColor: 'orange', color: 'white'},
  323. },
  324. {
  325. text: '删除',
  326. onPress: () => {
  327. this.deleteData(data.item);
  328. },
  329. style: {backgroundColor: 'red', color: 'white'},
  330. },
  331. ];
  332. }