LocationPermission.js 3.7 KB

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