device.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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. console.log(deviceKey);
  39. this.setState({
  40. deviceKey: deviceKey,
  41. });
  42. await this.getDeviceList();
  43. }
  44. //搜索
  45. searchData = async (text) => {
  46. this.setState({
  47. listData: [],
  48. companyName: text,
  49. showFoot: 0,
  50. pageNo: 1,
  51. });
  52. await this.getCompanyList();
  53. };
  54. //数据初始化
  55. initData = () => {
  56. this.setState({
  57. listData: [],
  58. isLoading: false,
  59. showFoot: 0,
  60. status: 0,
  61. pageNo: 1,
  62. companyName: '',
  63. });
  64. this.getCompanyList();
  65. };
  66. //列表上拉加载
  67. onEndReached = async () => {
  68. if (this.state.showFoot === 2) {
  69. if (this.state.pageNo >= this.state.totalPage) {
  70. this.setState({showFoot: 0});
  71. return;
  72. } else {
  73. this.setState({showFoot: 1, pageNo: this.state.pageNo + 1});
  74. await this.getCompanyList();
  75. }
  76. }
  77. };
  78. //获取数据
  79. getDeviceList = async () => {
  80. let company = await RetrieveData('company');
  81. if (company) {
  82. company = JSON.parse(company);
  83. }
  84. const token = await RetrieveData('token');
  85. if (token && company) {
  86. const url = '/sys/entDevice/findDevices';
  87. let response = await RequestNetwork(
  88. url,
  89. token,
  90. {
  91. entTaxId: company.entTaxId,
  92. },
  93. false,
  94. 2,
  95. );
  96. if (response) {
  97. if (response.code === 0) {
  98. this.setList(response.data);
  99. } else {
  100. await this.abnormalMessage(response);
  101. }
  102. }
  103. }
  104. };
  105. //设置设备列表
  106. setList = (data) => {
  107. let listData = data.map((_, i) => ({
  108. deviceType: data[i].deviceType,
  109. taxDiscId: data[i].taxDiscId,
  110. deviceLabel: data[i].deviceLabel,
  111. deviceId: data[i].deviceId,
  112. deviceKey: data[i].deviceKey,
  113. entTaxId: data[i].entTaxId,
  114. }));
  115. let list = this.state.listData.concat(listData);
  116. this.setState({
  117. showFoot: 0,
  118. isLoading: false,
  119. listData: list,
  120. });
  121. };
  122. //切换设备信息
  123. onChange = async (value) => {
  124. console.log(value);
  125. this.props.route.params.getDeviceInfo(value.item);
  126. this.props.navigation.goBack();
  127. };
  128. //显示发票抬头列表
  129. renderItem = (data) => (
  130. <SwipeAction autoClose style={{backgroundColor: 'transparent'}}>
  131. <RadioItem
  132. key={data.item.deviceId}
  133. checked={data.item.deviceKey === this.state.deviceKey}
  134. onChange={async (event) => {
  135. if (event.target.checked) {
  136. await this.onChange(data);
  137. }
  138. }}>
  139. {data.item.deviceLabel}
  140. </RadioItem>
  141. </SwipeAction>
  142. );
  143. // 设置load层是否显示
  144. setLoadingStatus = (isLoading) => {
  145. this.setState({
  146. spinner: isLoading,
  147. });
  148. };
  149. // 处理网络请求数据
  150. abnormalMessage = async (jason) => {
  151. if (jason.code === 401) {
  152. await Alert.alert(
  153. '登录失效',
  154. '登录状态已失效,请重新登录!',
  155. [
  156. {
  157. text: '确定',
  158. onPress: () => {
  159. CleanAll();
  160. this.props.navigation.popToTop();
  161. },
  162. },
  163. ],
  164. {cancelable: false},
  165. );
  166. }
  167. if (jason.code === 403) {
  168. Alert.alert(
  169. '权限',
  170. '暂无权限操作此模块!',
  171. [
  172. {
  173. text: '确定',
  174. onPress: () => {
  175. this.props.navigation.goBack();
  176. },
  177. },
  178. ],
  179. {cancelable: false},
  180. );
  181. }
  182. if (jason.code !== 401 && jason.code !== 403) {
  183. ToastShow(1, jason.msg);
  184. }
  185. };
  186. }