<script>
|
import { mapState } from 'vuex'
|
var QQMapWX = require('@/utils/qqmap-wx-jssdk.js')
|
export default {
|
computed: {
|
...mapState(['userInfo','token'])
|
},
|
onLaunch: async function() {
|
var that = this;
|
if (!this.token) {
|
uni.login({
|
provider: 'weixin',
|
success: async function (loginRes) {
|
let { code } = loginRes;
|
let res = await that.$u.api.wxLogin({ code })
|
if (res && res.code === 200) {
|
// 判断有无openid
|
if (res.data.openid) {
|
await that.$store.commit('setOpenId', res.data.openid)
|
}
|
// 判断有无token
|
if (res.data.token) {
|
await that.$store.commit('setToken', res.data.token)
|
}
|
// 判断有无用户信息
|
if (res.data.member) {
|
await that.$store.commit('setUserInfo', res.data.member)
|
await that.$store.commit('setOpenId', res.data.member.openId)
|
}
|
// 设置定位
|
await that.checkLocationAuth()
|
}
|
}
|
});
|
} else {
|
// 缓存用户信息
|
let res = await that.$u.api.getMemberInfo({})
|
if (res.code === 200) {
|
await that.$store.commit('setUserInfo', res.data)
|
await that.$store.commit('setOpenId', res.data.openId)
|
}
|
// 设置定位
|
await that.checkLocationAuth()
|
}
|
},
|
onShow: function() {
|
// this.getLocaltionBiz()
|
},
|
onHide: function() {
|
console.log('App Hide')
|
},
|
methods:{
|
// 定位
|
positioning() {
|
var that = this;
|
uni.getLocation({
|
type: 'gcj02',
|
highAccuracyExpireTime: 3000,
|
isHighAccuracy: true,
|
success: function (addr) {
|
const locParam = { latitude: addr.latitude, longitude: addr.longitude };
|
const qqmapsdk = new QQMapWX({
|
key: 'WE3BZ-HN6WS-ONDOH-62QCV-MNL6F-5NFNE'
|
});
|
qqmapsdk.reverseGeocoder({
|
locParam,
|
success: async function(res) {
|
console.log(res, '==================获取地址');
|
let info = res.result;
|
locParam.province = info.address_component.province;
|
locParam.city = info.address_component.city;
|
locParam.area = info.address_component.district;
|
locParam.street = info.address_component.street;
|
var ta = info.address || '地址获取失败' ;
|
if(info.formatted_addresses && info.formatted_addresses.recommend){
|
ta =info.formatted_addresses.recommend
|
}
|
locParam.address =ta
|
const resCity = await that.$u.api.getCityByName({ cityName: locParam.city })
|
if (resCity.code === 200) {
|
locParam.cityId = resCity.data.id
|
}
|
that.$store.commit('setPosition',locParam)
|
that.$isResolve()
|
},
|
fail: (err) => {
|
that.$isResolve()
|
}
|
});
|
}
|
});
|
},
|
goToAppSetting() {
|
var that = this;
|
uni.openSetting({
|
success: (res) => {
|
if (res.authSetting['scope.userLocation']) {
|
that.positioning()
|
}
|
that.$isResolve()
|
}
|
});
|
},
|
checkLocationAuth() {
|
uni.getSetting({
|
success: (res) => {
|
if (!res.authSetting['scope.userLocation']) {
|
uni.authorize({
|
scope: 'scope.userLocation',
|
success: () => {
|
this.positioning()
|
},
|
fail: () => {
|
uni.showModal({
|
title: '提示',
|
content: '需要获取您的位置信息,请在设置中开启位置权限',
|
confirmText: '去设置',
|
success: (res) => {
|
if (res.confirm) {
|
this.goToAppSetting();
|
} else {
|
this.$isResolve()
|
}
|
}
|
});
|
}
|
});
|
} else {
|
this.positioning()
|
}
|
}
|
});
|
},
|
getLocaltionBiz(){
|
var that =this
|
uni.getLocation({
|
type: 'wgs84',
|
success: (addr) => {
|
const locParam = { latitude: addr.latitude, longitude: addr.longitude };
|
uni.setStorageSync('address', locParam);
|
var address =null
|
const qqmapsdk = new QQMapWX({
|
key: 'WE3BZ-HN6WS-ONDOH-62QCV-MNL6F-5NFNE' //腾讯地图申请的key(后续需要公司提供,个人开发者额度有限)
|
});
|
// 腾讯地图逆地理编码
|
qqmapsdk.reverseGeocoder({
|
locParam,
|
success: function(res) {
|
console.log(res, '==================获取地址');
|
let info = res.result;
|
locParam.province = info.address_component.province;
|
locParam.city = info.address_component.city;
|
locParam.area = info.address_component.district;
|
locParam.street = info.address_component.street;
|
var ta = info.address || '地址获取失败' ;
|
if(info.formatted_addresses && info.formatted_addresses.recommend){
|
ta =info.formatted_addresses.recommend
|
}
|
locParam.address =ta
|
that.$store.commit('setPosition',locParam)
|
that.$isResolve()
|
},
|
fail: (err) => {
|
console.error('获取位置失败===========', err);
|
that.$store.commit('setPosition',locParam)
|
that.$isResolve()
|
}
|
});
|
that.$isResolve()
|
},
|
fail: (err) => {
|
console.error('获取位置失败', err);
|
that.$isResolve()
|
}
|
});
|
}
|
}
|
}
|
</script>
|
|
<style lang="scss">
|
@import "uview-ui/index.scss";
|
|
.toast-custom {
|
width: 500px !important;
|
font-size: 14px !important;
|
}
|
</style>
|