LocationPermission.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * 获取位置权限处理
  3. * @邠心vbe on 2023/02/15
  4. */
  5. import React from 'react';
  6. import { StyleSheet, View } from 'react-native';
  7. import { check, openSettings, PERMISSIONS, request, RESULTS } from 'react-native-permissions';
  8. import Button from '../../../components/Button';
  9. import TextView from '../../../components/TextView';
  10. import LocationEnabler from 'react-native-location-enabler';
  11. //global.hasPermission = false;
  12. class LocationListener {
  13. constructor() {
  14. this.listener = undefined;
  15. this.config = {
  16. priority: LocationEnabler.PRIORITIES.HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
  17. alwaysShow: false, // default false
  18. needBle: false // default false
  19. };
  20. this.status = undefined;
  21. }
  22. addListener() {
  23. this.listener = LocationEnabler?.addListener(({ locationEnabled }) => {
  24. if (!locationEnabled) {
  25. console.log("status: " + this.status + "," + locationEnabled);
  26. if (this.status != locationEnabled) {
  27. this.status = locationEnabled;
  28. LocationEnabler.requestResolutionSettings(this.config);
  29. }
  30. }
  31. })
  32. }
  33. removeListener() {
  34. if (this.listener) {
  35. this.listener.remove();
  36. }
  37. }
  38. check() {
  39. this.status = undefined;
  40. LocationEnabler.checkSettings(this.config)
  41. }
  42. }
  43. /**
  44. * 检查是否有定位权限
  45. * @param {function} back callback(hasPermission, canRequestPermission)
  46. */
  47. const checkPermission = (back) => {
  48. check(
  49. isIOS
  50. ? PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
  51. : PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
  52. .then(res => {
  53. console.log('[LocationPermission] checkPermission', res);
  54. switch (res) {
  55. case RESULTS.UNAVAILABLE:
  56. console.log('此功能不可用(在此设备上/在此上下文中)');
  57. if (isIOS) {
  58. back(true, true);
  59. } else {
  60. back(false, true);
  61. }
  62. break;
  63. case RESULTS.DENIED:
  64. console.log('权限未被请求/被拒绝,但可以请求');
  65. back(false, true);
  66. break;
  67. case RESULTS.LIMITED:
  68. console.log('权限是有限的:有些操作是可能的');
  69. back(true, true);
  70. break;
  71. case RESULTS.GRANTED:
  72. console.log('许可被授予');
  73. back(true, true);
  74. break;
  75. case RESULTS.BLOCKED:
  76. console.log('权限被拒绝,不再可请求');
  77. back(false, false);
  78. /*Dialog.showDialog({
  79. title: 'Error',
  80. message: 'Can not get charge station, Please grant location permissions.',
  81. ok: 'SETTING',
  82. callback: btn => {
  83. if (btn == Dialog.BUTTON_OK) {
  84. console.log('ok');
  85. openSettings().catch(() => console.warn('cannot open settings'));
  86. }
  87. }
  88. });*/
  89. break;
  90. }
  91. }).catch(erros => {
  92. console.log('[LocationPermission] checkPermission-catch', erros);
  93. back(false, false);
  94. });
  95. }
  96. /**
  97. * 请求定位权限
  98. * @param {function} back callback(hasPermission)
  99. */
  100. const getPermission = (back) => {
  101. request(
  102. isIOS
  103. ? PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
  104. : PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
  105. .then(res => {
  106. console.log('[LocationPermission] requestPermission', res);
  107. back(res == RESULTS.GRANTED);
  108. }).catch(erros => {
  109. console.info('[LocationPermission] requestPermission-catch', erros)
  110. back(false);
  111. });
  112. }
  113. const LocationPermission = ({visible=false, bottomDivide=false, onView}) => (
  114. visible
  115. ? <View style={[styles.permissionView, (bottomDivide ? styles.bottomTabDivide : {})]}>
  116. <TextView
  117. style={styles.permissionText}
  118. numberOfLines={1}>{$t("home.locationPermissionTips")}</TextView>
  119. <Button
  120. style={styles.viewButton}
  121. viewStyle={styles.viewButtonStyle}
  122. onClick={() => {
  123. openSettings().then(res => {
  124. if (onView) onView();
  125. }).catch(() => console.warn('cannot open settings'))
  126. }}>
  127. <TextView style={styles.buttonText}>{$t("nav.view")}</TextView>
  128. <FontAwesome
  129. size={16}
  130. color={colorPrimary}
  131. name='angle-right'/>
  132. </Button>
  133. </View>
  134. : <></>
  135. );
  136. const styles = StyleSheet.create({
  137. permissionView: {
  138. //top: 80,
  139. left: 16,
  140. right: 16,
  141. bottom: 32,
  142. opacity: .8,
  143. zIndex: 5,
  144. overflow: 'hidden',
  145. borderRadius: 30,
  146. position: 'absolute',
  147. alignItems: 'center',
  148. flexDirection: 'row',
  149. backgroundColor: '#FEB751'
  150. },
  151. bottomTabDivide: {
  152. bottom: 76
  153. },
  154. permissionText: {
  155. flex: 1,
  156. color: textDark,
  157. fontSize: 11,
  158. paddingLeft: 16,
  159. paddingRight: 2
  160. },
  161. viewButton: {
  162. borderRadius: 0,
  163. backgroundColor: 'transparent'
  164. },
  165. viewButtonStyle: {
  166. alignItems: 'center',
  167. flexDirection: 'row',
  168. ...$padding(8, 12, 8, 8),
  169. },
  170. buttonText: {
  171. color: colorPrimary,
  172. fontSize: 11,
  173. paddingRight: 4
  174. }
  175. })
  176. export default {
  177. VIEW: LocationPermission,
  178. checkPermission: checkPermission,
  179. requestPermission: getPermission,
  180. LocationListener: LocationListener
  181. }