device_select.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import React, {Component} from 'react';
  2. import {FlatList, Alert, SafeAreaView, DeviceEventEmitter} from 'react-native';
  3. import {SwipeAction, Radio} from '@ant-design/react-native';
  4. import {RetrieveData, IndividualStorageData} from '../../../data/storage';
  5. import {RequestNetwork} from '../../../data/encryption';
  6. import Spinner from 'react-native-loading-spinner-overlay';
  7. import loading_css from '../../../source/css/loading_css';
  8. import public_css from '../../../source/css/public_css';
  9. import {CleanAll} from '../../../components/abnormalMessage/abnormal_message';
  10. import {ToastShow} from '../../../components/toast/toast';
  11. const RadioItem = Radio.RadioItem;
  12. export default class device_select extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. listData: [],
  17. status: 0,
  18. isLoading: false,
  19. loadingText: 'loading',
  20. entTaxId: '',
  21. companyName: '',
  22. deviceKey: '',
  23. };
  24. }
  25. render() {
  26. return (
  27. <SafeAreaView style={public_css.body}>
  28. <FlatList
  29. data={this.state.listData}
  30. renderItem={(item) => this.renderItem(item)}
  31. />
  32. </SafeAreaView>
  33. );
  34. }
  35. //页面加载完成后加载数据
  36. async componentDidMount() {
  37. let deviceKey = this.props.route.params.deviceKey;
  38. let entTaxId = this.props.route.params.entTaxId;
  39. this.setState({
  40. deviceKey: deviceKey,
  41. entTaxId: entTaxId,
  42. });
  43. await this.getDeviceList();
  44. }
  45. //搜索
  46. searchData = async (text) => {
  47. this.setState({
  48. listData: [],
  49. companyName: text,
  50. showFoot: 0,
  51. pageNo: 1,
  52. });
  53. await this.getCompanyList();
  54. };
  55. //数据初始化
  56. initData = () => {
  57. this.setState({
  58. listData: [],
  59. isLoading: false,
  60. showFoot: 0,
  61. status: 0,
  62. pageNo: 1,
  63. companyName: '',
  64. });
  65. this.getCompanyList();
  66. };
  67. //列表上拉加载
  68. onEndReached = async () => {
  69. if (this.state.showFoot === 2) {
  70. if (this.state.pageNo >= this.state.totalPage) {
  71. this.setState({showFoot: 0});
  72. return;
  73. } else {
  74. this.setState({showFoot: 1, pageNo: this.state.pageNo + 1});
  75. await this.getCompanyList();
  76. }
  77. }
  78. };
  79. //获取数据
  80. getDeviceList = async () => {
  81. const token = await RetrieveData('token');
  82. if (token) {
  83. const url = '/sys/entDevice/findDevices';
  84. let response = await RequestNetwork(
  85. url,
  86. token,
  87. {
  88. entTaxId: this.state.entTaxId,
  89. },
  90. false,
  91. 2,
  92. );
  93. if (response) {
  94. if (response.code === 0) {
  95. this.setList(response.data);
  96. }
  97. }
  98. }
  99. };
  100. //设置设备列表
  101. setList = (data) => {
  102. let listData = data.map((_, i) => ({
  103. deviceType: data[i].deviceType,
  104. taxDiscId: data[i].taxDiscId,
  105. deviceLabel: data[i].deviceLabel,
  106. deviceId: data[i].deviceId,
  107. deviceKey: data[i].deviceKey,
  108. entTaxId: data[i].entTaxId,
  109. }));
  110. let list = this.state.listData.concat(listData);
  111. this.setState({
  112. showFoot: 0,
  113. isLoading: false,
  114. listData: list,
  115. });
  116. };
  117. //切换设备信息
  118. onChange = async (value) => {
  119. this.props.route.params.getDeviceInfo(
  120. value.item.deviceLabel,
  121. value.item.deviceKey,
  122. );
  123. this.props.navigation.goBack();
  124. };
  125. //显示发票抬头列表
  126. renderItem = (data) => (
  127. <SwipeAction autoClose style={{backgroundColor: 'transparent'}}>
  128. <RadioItem
  129. key={data.item.deviceId}
  130. checked={data.item.deviceKey === this.state.deviceKey}
  131. onChange={async (event) => {
  132. if (event.target.checked) {
  133. await this.onChange(data);
  134. }
  135. }}>
  136. {data.item.deviceLabel}
  137. </RadioItem>
  138. </SwipeAction>
  139. );
  140. // 设置load层是否显示
  141. setLoadingStatus = (isLoading) => {
  142. this.setState({
  143. spinner: isLoading,
  144. });
  145. };
  146. // 处理网络请求数据
  147. abnormalMessage = async (jason) => {
  148. if (jason.code === 401) {
  149. await Alert.alert(
  150. '登录失效',
  151. '登录状态已失效,请重新登录!',
  152. [
  153. {
  154. text: '确定',
  155. onPress: () => {
  156. CleanAll();
  157. this.props.navigation.popToTop();
  158. },
  159. },
  160. ],
  161. {cancelable: false},
  162. );
  163. }
  164. if (jason.code === 403) {
  165. Alert.alert(
  166. '权限',
  167. '暂无权限操作此模块!',
  168. [
  169. {
  170. text: '确定',
  171. onPress: () => {
  172. this.props.navigation.goBack();
  173. },
  174. },
  175. ],
  176. {cancelable: false},
  177. );
  178. }
  179. if (jason.code !== 401 && jason.code !== 403) {
  180. ToastShow(1, jason.msg);
  181. }
  182. };
  183. }