LocationPermission.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. 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}) => (
  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={() => openSettings().catch(() => console.warn('cannot open settings'))}>
  91. <TextView style={styles.buttonText}>{$t("nav.view")}</TextView>
  92. <FontAwesome
  93. size={16}
  94. color={colorPrimary}
  95. name='angle-right'/>
  96. </Button>
  97. </View>
  98. : <></>
  99. );
  100. const styles = StyleSheet.create({
  101. permissionView: {
  102. //top: 80,
  103. left: 16,
  104. right: 16,
  105. bottom: 32,
  106. opacity: .8,
  107. zIndex: 5,
  108. overflow: 'hidden',
  109. borderRadius: 30,
  110. position: 'absolute',
  111. alignItems: 'center',
  112. flexDirection: 'row',
  113. backgroundColor: '#FEB751'
  114. },
  115. permissionText: {
  116. flex: 1,
  117. color: textDark,
  118. fontSize: 11,
  119. paddingLeft: 16,
  120. paddingRight: 2
  121. },
  122. viewButton: {
  123. borderRadius: 0,
  124. backgroundColor: 'transparent'
  125. },
  126. viewButtonStyle: {
  127. alignItems: 'center',
  128. flexDirection: 'row',
  129. ...$padding(8, 12, 8, 8),
  130. },
  131. buttonText: {
  132. color: colorPrimary,
  133. fontSize: 11,
  134. paddingRight: 4
  135. }
  136. })
  137. export default {
  138. VIEW: LocationPermission,
  139. checkPermission: checkPermission,
  140. requestPermission: getPermission
  141. }