LocationPermission.js 5.2 KB

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