123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- 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':'',
- '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;
- }
- //设置浏览器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 getFilePermissions(callback) {
- callback = typeof(callback) == 'function' ? callback : function(res) {};
- var num=1;
- // #ifdef MP-WEIXIN
-
- // #endif
- // #ifdef APP-PLUS
- plus.android.requestPermissions(
- ['android.permission.WRITE_EXTERNAL_STORAGE',
- 'android.permission.READ_EXTERNAL_STORAGE',], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
- 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{
- callback(result)
- }
- },
- function(error) {
- console.log('申请权限错误:' + error.code + " = " + error.message);
- // resolve({
- // code: error.code,
- // message: error.message
- // });
- }
- );
-
- // #endif
- };
- 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);
- // 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,
- getFilePermissions,
- __CFG
- }
- export default self
|