| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /**
- * 获取位置权限处理
- * @邠心vbe on 2023/02/15
- */
- import React from 'react';
- import { StyleSheet, View } from 'react-native';
- import { check, openSettings, PERMISSIONS, request, RESULTS } from 'react-native-permissions';
- import Button from '../../../components/Button';
- import TextView from '../../../components/TextView';
- import utils from '../../../utils/utils';
- //import LocationEnabler from 'react-native-location-enabler';
- //global.hasPermission = false;
- class LocationListener {
- constructor() {
- this.listener = undefined;
- this.config = {
- priority: "",//LocationEnabler.PRIORITIES.HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
- alwaysShow: false, // default false
- needBle: false // default false
- };
- this.status = undefined;
- }
-
- addListener() {
- /*this.listener = LocationEnabler?.addListener(({ locationEnabled }) => {
- if (!locationEnabled) {
- console.log("status: " + this.status + "," + locationEnabled);
- if (this.status != locationEnabled) {
- this.status = locationEnabled;
- LocationEnabler.requestResolutionSettings(this.config);
- }
- }
- })*/
- }
- removeListener() {
- if (this.listener) {
- this.listener.remove();
- }
- }
- check() {
- this.status = undefined;
- //LocationEnabler.checkSettings(this.config)
- }
- }
- /**
- * 检查是否有定位权限
- * @param {function} back callback(hasPermission, canRequestPermission)
- */
- const checkPermission = (back) => {
- check(
- isIOS
- ? PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
- : PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
- .then(res => {
- console.log('[LocationPermission] checkPermission', res);
- switch (res) {
- case RESULTS.UNAVAILABLE:
- console.log('此功能不可用(在此设备上/在此上下文中)');
- if (isIOS) {
- back(true, true);
- } else {
- back(false, true);
- }
- break;
- case RESULTS.DENIED:
- console.log('权限未被请求/被拒绝,但可以请求');
- back(false, true);
- break;
- case RESULTS.LIMITED:
- console.log('权限是有限的:有些操作是可能的');
- back(true, true);
- break;
- case RESULTS.GRANTED:
- console.log('许可被授予');
- back(true, true);
- break;
- case RESULTS.BLOCKED:
- console.log('权限被拒绝,不再可请求');
- back(false, false);
- /*Dialog.showDialog({
- title: 'Error',
- message: 'Can not get charge station, Please grant location permissions.',
- ok: 'SETTING',
- callback: btn => {
- if (btn == Dialog.BUTTON_OK) {
- console.log('ok');
- openSettings().catch(() => console.warn('cannot open settings'));
- }
- }
- });*/
- break;
- }
- }).catch(erros => {
- console.log('[LocationPermission] checkPermission-catch', erros);
- back(false, false);
- });
- }
- /**
- * 请求定位权限
- * @param {function} back callback(hasPermission)
- */
- const getPermission = (back) => {
- utils.logEventTracking("permission_request: LOCATION")
- request(
- isIOS
- ? PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
- : PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
- .then(res => {
- console.log('[LocationPermission] requestPermission', res);
- utils.logEventTracking("permission_resut: " + res)
- back(res == RESULTS.GRANTED);
- }).catch(erros => {
- utils.logEventTracking("permission_resut: " + erros)
- console.info('[LocationPermission] requestPermission-catch', erros)
- back(false);
- });
- }
- const LocationPermission = ({visible=false, bottomDivide=false, onView}) => (
- visible
- ? <View style={[styles.permissionView, (bottomDivide ? styles.bottomTabDivide : {})]}>
- <TextView
- style={styles.permissionText}
- numberOfLines={1}>{$t("home.locationPermissionTips")}</TextView>
- <Button
- style={styles.viewButton}
- viewStyle={styles.viewButtonStyle}
- onClick={() => {
- openSettings().then(res => {
- if (onView) onView();
- }).catch(() => console.warn('cannot open settings'))
- }}>
- <TextView style={styles.buttonText}>{$t("nav.view")}</TextView>
- <FontAwesome
- size={16}
- color={colorPrimary}
- name='angle-right'/>
- </Button>
- </View>
- : <></>
- );
- const styles = StyleSheet.create({
- permissionView: {
- //top: 80,
- left: 16,
- right: 16,
- bottom: 32,
- opacity: .8,
- zIndex: 5,
- overflow: 'hidden',
- borderRadius: 30,
- position: 'absolute',
- alignItems: 'center',
- flexDirection: 'row',
- backgroundColor: '#FEB751'
- },
- bottomTabDivide: {
- bottom: 76
- },
- permissionText: {
- flex: 1,
- color: textDark,
- fontSize: 11,
- paddingLeft: 16,
- paddingRight: 2
- },
- viewButton: {
- borderRadius: 0,
- backgroundColor: 'transparent'
- },
- viewButtonStyle: {
- alignItems: 'center',
- flexDirection: 'row',
- ...$padding(8, 12, 8, 8),
- },
- buttonText: {
- color: colorPrimary,
- fontSize: 11,
- paddingRight: 4
- }
- })
- export default {
- VIEW: LocationPermission,
- checkPermission: checkPermission,
- requestPermission: getPermission,
- LocationListener: LocationListener
- }
|