123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import myAmapFun from "@/utils/initmap.js"
- import {showConfirm} from '@/utils/common'
- var __CFG = null;
- //系统参数
- __CFG = __CFG || {
- 'lat': '',
- 'lng': '',
- 'address': '',
- 'city':'',
- "UxLocation": {
- "lng": "",
- "lat": "",
- "address": "",
- 'city':'',
- },
- };
- 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;
- }
- //设置浏览器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 getLocation(callback) {
-
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- var num=1;
- // #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);
- // uni.hideLoading();
- // console.log(uxlocation,23)
- callback(uxlocation);
- //成功回调
- },
- fail: function(info) {
- //失败回调
- }
- })
- // 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);
- // uni.hideLoading();
- // console.log(uxlocation,23)
- callback(uxlocation);
- //成功回调
- },
- fail: function(info) {
- //失败回调
- console.log(info,223)
- }
- })
- // 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))
- }
- })
- }
- const self = {
- getLocation,
- setUxLocation,
- __CFG
- }
- export default self
|