taro 用户授权和引导再次授权案例

    科技2022-08-18  99

    import React, { Component } from 'react' import { connect } from 'react-redux' import { View, Button, Text,ScrollView } from '@tarojs/components' import { AtButton } from 'taro-ui' import Taro from '@tarojs/taro' import './index.less' import TopSearch from '../../components/common/topSearch' import Banner from '../../components/common/banner' import Grid from './child/grid' import New from './child/new' import Hot from './child/hot' import Tabs from './child/tabs' import {api} from '../../utils/api' class Index extends Component { componentWillReceiveProps (nextProps) { console.log(this.props, nextProps) } state={ show:true } componentWillUnmount () { } componentDidShow () { } componentDidHide () { } async componentDidMount() { this.getLocationAuthorize(); } //获取授权并获取地理位置信息 getLocationAuthorize=()=>{ let that=this; //检测是否已经开启权限 Taro.getSetting({ success: function (res) { console.log(res.authSetting) //如果没有开启,则提示开启权限 if(!res.authSetting['scope.userLocation']) { Taro.authorize({ scope: 'scope.userLocation', success: function () { //根据获取经纬度后逆地址解析获取当前位置 that.getLocation(); }, fail:function() { Taro.showToast({ title:'获取位置信息失败', icon:'none' }) //拒绝授权引导用户手动授权 Taro.showModal({ title: '提示!', confirmText: '去设置', showCancel: false, content: '获取地理位置信息', success: function(res) { if (res.confirm) { Taro.openSetting({ success: function (res) { console.log(res.authSetting) } }) } } }) } }) } } }) } //获取地理位置信息 getLocation=()=>{ Taro.getLocation({ type: 'gcj02', //返回可以用于 Taro.openLocation的经纬度 success: function (res) { const latitude = res.latitude const longitude = res.longitude var QQMapWX = require('../..//utils/sdk/qqmap-wx-jssdk.min.js'); var qqmapsdk = new QQMapWX({ key: '腾讯地图密钥' // 必填 }); console.log(latitude,longitude); qqmapsdk.reverseGeocoder({ location:{latitude,longitude}, success: function(res){ Taro.showToast({ title:res.result.address_component.city }) } }) } }) } render () { return ( <View className='index'> <TopSearch /> <View className='i-c'> <ScrollView scrollY style='height:100%' > <Banner /> <Grid /> <New /> <Hot /> <Tabs /> </ScrollView> </View> </View> ) } } export default Index
    Processed: 0.010, SQL: 9