123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- import myAmapFun from "@/utils/initmap.js"
- var __CFG = null;
- //系统参数
- __CFG = __CFG || {
- 'lat': '',
- 'lng': '',
- 'address': '',
- 'city':'',
- "UxLocation": {
- "lng": "",
- "lat": "",
- "address": "",
- 'city':'',
- 'temperature':'',
- 'weather':'',
- 'icon':''
- },
- iconWeatherMap:[{tit:'风',val:'weaiona',children:['有风', '平静', '微风', '和风', '清风', '强风/劲风', '疾风', '大风', '烈风', '风暴', '狂爆风', '飓风', '热带风暴', '龙卷风']},
- {tit:'多云',val:'weaionb',children:['少云','多云']},
- {tit:'晴间多云',val:'weaionc',children:['晴间多云', ]},
- {tit:'雪',val:'weaiond',children:['雪', '阵雪', '小雪', '中雪', '大雪', '暴雪', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '冷']},
- {tit:'雾',val:'weaione',children:['浮尘', '扬沙', '沙尘暴', '强沙尘暴', '雾', '浓雾', '强浓雾', '轻雾', '大雾', '特强浓雾']},
- {tit:'晴',val:'weaionf',children:['晴', '热']},
- {tit:'雨夹雪',val:'weaiong',children:['雨雪天气', '雨夹雪', '阵雨夹雪']},
- {tit:'雨',val:'weaionh',children:['阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '小雨', '中雨', '大雨', '暴雨', '大暴雨', '特大暴雨', '强阵雨', '强雷阵雨', '极端降雨', '毛毛雨/细雨', '雨', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '冻雨']},
- {tit:'阴',val:'weaioni',children:['阴','未知']},
- {tit:'霾',val:'weaionj',children:['霾', '中度霾', '重度霾', '严重霾',]},
- ],
- };
- function setUxLocation(UxLocation){
- UxLocation = UxLocation || {};
- __CFG.lng = UxLocation.lng || '';
- __CFG.lat = UxLocation.lat || '';
- __CFG.address = UxLocation.address || UxLocation.addr || '';
- __CFG.city = UxLocation.city || UxLocation.city || '';
- __CFG.UxLocation = UxLocation;
- }
- //判断是否微信
- function checkIsWeixin() {
- if (/(micromessenger)/.test(navigator.userAgent.toLowerCase())) {
- return true;
- } else {
- return false;
- }
- };
- //设置浏览器cookie
- function set_cookie(key, value, expire, path){
- var keys = __CFG.C_PREFIX + key;
- path = path || '/';
- var a = keys + "=" + value;
- if(typeof(expire) != 'undefined'){
- var date = new Date();
- expire = parseInt(expire,10);
- date.setTime(date.getTime() + expire*1000);
- a += "; expires="+date.toGMTString();
- }
- a += ";path="+path;
- if(__CFG.C_DOMAIN){
- a += ";domain="+__CFG.C_DOMAIN;
- }
- document.cookie = a;
- };
- //删除浏览器cookie
- function remove_cookie(key){
- if (!key) {
- return false;
- } else {
- var keys = __CFG.C_PREFIX + key;
- document.cookie = keys+"=;expires="+(new Date(0)).toGMTString();
- }
- };
- //获取浏览器cookie
- function get_cookie(key) {
- if (!key) {
- return false;
- } else {
- var keys = __CFG.C_PREFIX + key;
- var reg = new RegExp("(^| )" + keys + "=([^;]*)(;|\x24)");
- var result = reg.exec(document.cookie);
- if (result) {
- return result[2] || '';
- }
- return '';
- }
- };
- function getLocations(callback) {
- var uxlocation = {"lng": "", "lat": "", "address": ""};
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- if (__CFG.lat == '' || __CFG.lng == '') {
- // #ifndef H5
- uni.showLoading({
- title: '获取位置信息中'
- })
- uni.getLocation({
- type: 'gcj02',
- geocode: 'true',
- success: function(res) {
- uxlocation.lat = res.latitude;
- uxlocation.lng = res.longitude;
- myAmapFun.getRegeo({
- success: function(data) {
- // #ifdef APP-PLUS
- /* var latlng = {
- "lat": res.latitude,
- "lng": res.longitude
- }
- plugins.appLnglat(latlng); */
- // #endif
- console.log('getRegeo', data);
- uxlocation.address = data[0].name;
- setUxLocation(uxlocation);
- uni.hideLoading();
- //成功回调
- },
- fail: function(info) {
- //失败回调
- // console.log(info)
- uni.navigateTo({
- url: '/pages/addr/selectReceive'
- })
- }
- })
- setUxLocation(uxlocation);
- callback();
- },
-
- fail: function(error) {
- if (count <= 1) {
- uni.showToast({
- icon: 'none',
- title: '定位失败'
- })
- }
- // #ifdef APP-PLUS
- // plus.nativeUI.alert(JSON.stringify(error))
- // #endif
- //callback();
- uni.navigateTo({
- url: '/pages/addr/selectReceive'
- })
- },
- complete: function(rs) {
- console.log('complete:'+JSON.stringify(rs))
- }
- })
- // #endif
-
- // #ifdef H5
- var selat = '', selng = '', seaddress = '';
- if(sessionStorage.getItem('UxLocation')){
- var uxl = JSON.parse(sessionStorage.getItem('UxLocation'));
- selat = uxl.lat || '';
- selng = uxl.lng || '';
- seaddress = uxl.addr || '';
- }
-
- if (selat && selng){
- uxlocation.lng = selng;
- uxlocation.lat = selat;
- uxlocation.address = seaddress;
- setUxLocation(uxlocation);
- callback();
- }else{
- if(checkIsWeixin()){
- getLocal(function(){
- callback();
- });
- }else{
- setTimeout(function (){
- AMap.plugin('AMap.Geolocation', function() {
- var geolocation = new AMap.Geolocation({
- enableHighAccuracy: false, // 是否使用高精度定位,默认:true
- timeout: 10000, // 设置定位超时时间,默认:无穷大
- // showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
- // showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
- // panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
- // zoomToAccuracy:true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
- extensions:'all' // 返回周边POI、道路交叉口等信息
- });
- geolocation.getCurrentPosition()
- AMap.event.addListener(geolocation, 'complete', onComplete)
- AMap.event.addListener(geolocation, 'error', onError)
- function onComplete (data) {
- console.log('onComplete:',data);
- var uaddress = '';
- if(data.pois && data.pois.length > 0){
- uaddress = data.pois[0].name;
- }else if(data.formattedAddress){
- uaddress = data.formattedAddress;
- }else if( data.addressComponent){
- uaddress = data.addressComponent.city + data.addressComponent.district
- }
-
- uxlocation.lng = data.position.lng || '';
- uxlocation.lat = data.position.lat || '';
- uxlocation.address = uaddress;
- setUxLocation(uxlocation);
- callback();
- };
- function onError (data) {
- // 定位出错
- uni.navigateTo({
- url: '/pages/addr/selectReceive'
- })
- };
- })
- }, 500);
- }
- }
- // #endif
- } else {
- callback();
- }
- };
- function getGeocoder(lng, lat, callback) {
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- AMap.plugin('AMap.Geocoder', function() {
- var geocoder = new AMap.Geocoder({
- // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
- city: '全国',
- extensions: 'all'
- })
-
- var lnglat = [lng, lat];
- geocoder.getAddress(lnglat, function(status, result) {
- console.log(status,result)
- if (status === 'complete' && result.info === 'OK') {
- // result为对应的地理位置详细信息
- let items = result.regeocode.pois;
- if(items.length > 0){
- var pos = {};
- for (let i in items) {
- var obj = {
- "name": items[i].name,
- "addr": items[i].address,
- "lng": items[i].location.lng,
- "lat": items[i].location.lat
- };
- if(i == '0'){
- pos = obj;
- }
- }
- callback(pos);
- }
- }else{
-
- }
- })
- })
- };
- //初始化sdk配置
- function initJssdk(callback ,url){
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- //服务端进行签名 ,可使用uni.request替换。 签名算法请看文档
- http('client/app/get_wechat',{url:url},function(res){
- if(res.data){
- jweixin.config({
- debug: false,
- appId: res.data.wxjs_config.appId,
- timestamp:res.data.wxjs_config.timestamp,
- nonceStr: res.data.wxjs_config.nonceStr,
- signature:res.data.wxjs_config.signature,
- jsApiList: ['checkJsAPi','getLocation','onMenuShareAppMessage','onMenuShareTimeline','onMenuShareQQ','onMenuShareQZone','onMenuShareWeibo','updateAppMessageShareData','updateTimelineShareData','chooseWXPay']
- });
- //配置完成后,再执行分享等功能
- if(callback){
- callback(res.data.wxjs_config);
- }
- }
- },true);
- };
-
- // 定位当前位置
- function getLocal(callback){
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- var urls = window.location.href;
- initJssdk(function(signData){
- uni.showLoading({
- title: '微信定位中...'
- })
- var uxlocation = {"lng": "", "lat": "", "address": ""};
- jweixin.ready(function () {
- jweixin.getLocation({
- type: 'gcj02',
- success: function (res) {
- uxlocation.lng = res.longitude || '';
- uxlocation.lat = res.latitude || '';
- setUxLocation(uxlocation);
- AMap.plugin('AMap.Geocoder', function() {
- var geocoder = new AMap.Geocoder({extensions:'all'});
- AMap.event.addListener(geocoder, 'error', error)
- var lnglat = [Number(res.longitude), Number(res.latitude)]
- geocoder.getAddress(lnglat, function(status, result) {
- if (status === 'complete' && result.info === 'OK') {
- console.log('complete:',result);
- uni.hideLoading();
- // result为对应的地理位置详细信息
- var addr = '';
- if(result.regeocode.pois && result.regeocode.pois.length > 0){
- addr = result.regeocode.pois[0].name;
- }else if(result.regeocode.formattedAddress){
- addr = result.regeocode.formattedAddress;
- }else{
- addr = result.regeocode.addressComponent.city + result.regeocode.addressComponent.district
- }
- uxlocation.address = addr;
- setUxLocation(uxlocation);
- callback();
- }else{
- uni.hideLoading();
- callback();
- }
- });
-
- function error(res){
- // getGeoLocation(res.longitude,res.latitude);
- alert('地址信息解析失败');
- callback();
- }
- });
- },
-
- fail: function (res) {
- uni.hideLoading();
- alert('微信获取位置失败');
- uni.navigateTo({
- url: '/pages/addr/selectReceive'
- })
- },
-
- cancel: function (res) {
- uni.hideLoading();
- alert('用户拒绝获取位置');
- uni.navigateTo({
- url: '/pages/addr/selectReceive'
- })
- }
- });
- });
- },urls)
- };
- function getLocation(callback) {
-
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- var num=1;
- // #ifdef H5
- getLocationaddr(callback)
- // #endif
- // #ifdef MP-WEIXIN
- uni.getSetting({
- success(res) {
- if(res.authSetting['scope.userLocation'] != undefined){
- if (res.authSetting['scope.userLocation']) {
- getLocationaddr(callback)
- // 已经授权,可以直接调用 getLocation 获取地理位置
- } else {
- showConfirm('您还未开启定位,是否去开启定位?').then(cres => {
- if (cres.confirm) {
- uni.openSetting({
- success(res2) {
- if (res2.authSetting['scope.userLocation']) {
-
- }
- }
- });
- callback(-1)
- }
- })
- };
- }else{
- var uxlocation = {"lng": "", "lat": "", "address": ""};
- // var citynum=1;
- uni.getLocation({
- type: 'gcj02',
- geocode: 'true',
- success: function(ares) {
- uxlocation.lat = ares.latitude;
- uxlocation.lng = res.longitude;
- myAmapFun.getRegeo({
- success: function(data) {
- // citynum=0;
- uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
- uxlocation.city = data[0].regeocodeData.addressComponent.city;
- setUxLocation(uxlocation);
- callback(uxlocation);
- //成功回调
- },
- fail: function(info) {
- //失败回调
- }
- })
- // 获取天气
- // myAmapFun.getWeather({
- // success(res){
- // //temperature 温度
- // //weather 天气现象
- // uxlocation.city = res.city.data;
- // uxlocation.temperature = res.temperature.data;
- // uxlocation.weather = res.weather.data;
- // var weather=res.weather.data;
- // var weatherMap=__CFG.iconWeatherMap;
- // var url=require('@/static/images/weather/weaionf.png');
- // Object.keys(weatherMap).some((key) => {
- // if (weatherMap[key].children.indexOf(weather)!=-1) {
- // url=require(`@/static/images/weather/${weatherMap[key].val}.png`)
- // return true;
- // }
- // })
- // uxlocation.icon = url;
- // setUxLocation(uxlocation);
- // callback(uxlocation);
- // },
- // fail(err){
- // console.log('fail')
- // }
- // })
- // if(citynum==1){
- // setUxLocation(uxlocation);
- // callback(uxlocation);
- // }
- },
-
- fail: function(error) {
- uni.showToast({
- icon: 'none',
- title: '定位失败'
- })
-
- // #ifdef APP-PLUS
- // #endif
-
- },
- complete: function(rs) {
- // console.log('complete:'+JSON.stringify(rs))
- }
- })
- }
- },
- fail(err) {
- console.log(err)
- },
- })
- // #endif
- // #ifdef APP-PLUS
- let system = uni.getSystemInfoSync();
- if(system.platform === "android"){
- var context = plus.android.importClass("android.content.Context");
- var locationManger = plus.android.importClass("android.location.LocationManager");
- var main = plus.android.runtimeMainActivity();
- var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
- if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
- uni.showModal({
- title:"提示",
- content:"请授权位置获取地址",
- showCancel:false,
- success() {
- if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
- var Intent = plus.android.importClass("android.content.Intent");
- var Settings = plus.android.importClass("android.provider.Settings");
- var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- main.startActivity(intent);
- }else{
- console.log("定位已开启")
- _self.setSite()
- }
- }
- })
- }else{
- plus.android.requestPermissions(
- ['android.permission.ACCESS_FINE_LOCATION'], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
- function(resultObj) {
- var result = 0;
- for (var i = 0; i < resultObj.granted.length; i++) {
- var grantedPermission = resultObj.granted[i];
- console.log('已获取的权限:' + grantedPermission);
- result = 1
- }
- for (var i = 0; i < resultObj.deniedPresent.length; i++) {
- var deniedPresentPermission = resultObj.deniedPresent[i];
- console.log('拒绝本次申请的权限:' + deniedPresentPermission);
- result = 0
- }
- for (var i = 0; i < resultObj.deniedAlways.length; i++) {
- var deniedAlwaysPermission = resultObj.deniedAlways[i];
- console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
- result = -1
- }
- // resolve(result);
- // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
- if (result != 1) {
- showConfirm('您还未开启定位,是否去开启定位?').then(res => {
- if (res.confirm) {
- gotoAppPermissionSetting()
- callback(result)
- }
- })
- }else{
- getLocationaddr(callback)
- }
- },
- function(error) {
- console.log('申请权限错误:' + error.code + " = " + error.message);
- // resolve({
- // code: error.code,
- // message: error.message
- // });
- }
- );
- }
- }
-
- // #endif
- };
- // 跳转到**应用**的权限页面
- function gotoAppPermissionSetting() {
- var isIos
- // #ifdef APP-PLUS
- isIos = (plus.os.name == "iOS")
- // #endif
- if (isIos) {
- var UIApplication = plus.ios.import("UIApplication");
- var application2 = UIApplication.sharedApplication();
- var NSURL2 = plus.ios.import("NSURL");
- // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
- var setting2 = NSURL2.URLWithString("app-settings:");
- application2.openURL(setting2);
- plus.ios.deleteObject(setting2);
- plus.ios.deleteObject(NSURL2);
- plus.ios.deleteObject(application2);
- } else {
- var Intent = plus.android.importClass("android.content.Intent");
- var Settings = plus.android.importClass("android.provider.Settings");
- var Uri = plus.android.importClass("android.net.Uri");
- var mainActivity = plus.android.runtimeMainActivity();
- var intent = new Intent();
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
- intent.setData(uri);
- mainActivity.startActivity(intent);
- }
- }
- function getLocationaddr(callback){
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- var uxlocation = {"lng": "", "lat": "", "address": ""};
- uni.getLocation({
- type: 'gcj02',
- geocode: 'true',
- success: function(res) {
- uxlocation.lat = res.latitude;
- uxlocation.lng = res.longitude;
- myAmapFun.getRegeo({
- success: function(data) {
- uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
- uxlocation.city = data[0].regeocodeData.addressComponent.city;
- setUxLocation(uxlocation);
- callback(uxlocation);
- },
- fail: function(info) {
- //失败回调
- console.log(info,223)
- }
- })
- // 获取天气
- // myAmapFun.getWeather({
- // success(res){
- // //temperature 温度
- // //weather 天气现象
- // uxlocation.city = res.city.data;
- // uxlocation.temperature = res.temperature.data;
- // uxlocation.weather = res.weather.data;
- // var weather=res.weather.data;
- // var weatherMap=__CFG.iconWeatherMap;
- // var url=require('@/static/images/weather/weaionf.png');
- // Object.keys(weatherMap).some((key) => {
- // if (weatherMap[key].children.indexOf(weather)!=-1) {
- // url=require(`@/static/images/weather/${weatherMap[key].val}.png`)
- // return true;
- // }
- // })
- // uxlocation.icon = url;
- // setUxLocation(uxlocation);
- // callback(uxlocation);
- // },
- // fail(err){
- // console.log('fail')
- // }
- // })
- },
-
- fail: function(error) {
- uni.showToast({
- icon: 'none',
- title: '定位失败'
- })
-
- // #ifdef APP-PLUS
- // #endif
-
- },
- complete: function(rs) {
- // console.log('complete:'+JSON.stringify(rs))
- }
- })
- }
- const self = {
- getLocation,
- setUxLocation,
- __CFG
- }
- export default self
|