You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
duomendian/mixins/location.js

117 lines
2.9 KiB

3 months ago
import { getGeocoder } from '@/api/user.js';
import { getnearbyStore } from '@/api/new_store.js';
import { mapMutations } from 'vuex';
export default {
data() {
return {
location: {},
addressInfo:'选择地址',
addressInfos:"选择地址",
storeName: '',
storeId:0
};
},
watch: {
location(value) {
this.getnearbyStore(value);
}
},
mounted(){
uni.$off('activeFn');
uni.$on('activeFn', data => {
if(data){
this.storeName = data.name;
this.storeId = data.id;
this.SET_NEARBY(data.id);
}
});
},
methods: {
...mapMutations(['SET_NEARBY']),
selfLocation() {
let self = this
// #ifdef MP || APP-PLUS
uni.getLocation({
type: 'gcj02',
success: (res) => {
try {
uni.setStorageSync('user_latitude', res.latitude);
uni.setStorageSync('user_longitude', res.longitude);
self.getGeocoderCity(res.latitude,res.longitude);
self.location = { latitude: res.latitude, longitude: res.longitude };
} catch {}
},
fail:(res)=>{
// #ifdef MP
uni.getSetting({
success: res=>{
if(typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']){
uni.setStorageSync('refuseLocation', true);
}
}
})
// #endif
}
});
// #endif
// #ifdef H5
if (this.$wechat.isWeixin()) {
this.$wechat.location().then(res => {
uni.setStorageSync('user_latitude', res.latitude);
uni.setStorageSync('user_longitude', res.longitude);
self.getGeocoderCity(res.latitude,res.longitude);
})
} else {
uni.getLocation({
type: 'gcj02',
success: function(res) {
try {
uni.setStorageSync('user_latitude', res.latitude);
uni.setStorageSync('user_longitude', res.longitude);
self.getGeocoderCity(res.latitude,res.longitude);
} catch {}
}
});
}
// #endif
},
getGeocoderCity(latitude,longitude){
getGeocoder({
lat: latitude,
long: longitude
}).then(res=>{
let address = res.data.address_component;
this.addressInfo = address?address.city.slice(0,4) : '选择地址';
this.addressInfos = address?(address.city+address.district).slice(0,7) : '选择地址';
})
},
chooseLocation: function() {
let that = this;
if(that.fixConfig == 0){
uni.navigateTo({
url:'/pages/store/list/index?type=1&isCollage=1&storeId='+this.storeId
})
}else{
uni.chooseLocation({
success: (res) => {
let address = that.$util.addressInfo(res.address);
this.addressInfo = address?address.city.slice(0,4) : '选择地址';
this.addressInfos = address?(address.city+address.district).slice(0,7) : '选择地址';
that.location = { latitude: res.latitude, longitude: res.longitude };
},
fail: (err)=>{
console.log(err)
}
})
}
},
getnearbyStore(data) {
getnearbyStore(data).then(res => {
this.storeId = res.data.info.id;
this.SET_NEARBY(res.data.info.id);
this.storeName = res.data.info.name;
});
}
}
};