LocationPermission.js 5.0 KB

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