LocationPermission.js 3.9 KB

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