location.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import myAmapFun from "@/utils/initmap.js"
  2. import {showConfirm} from '@/utils/common'
  3. var __CFG = null;
  4. //系统参数
  5. __CFG = __CFG || {
  6. 'lat': '',
  7. 'lng': '',
  8. 'address': '',
  9. 'city':'',
  10. "UxLocation": {
  11. "lng": "",
  12. "lat": "",
  13. "address": "",
  14. 'city':'',
  15. 'temperature':'',
  16. 'weather':'',
  17. 'icon':''
  18. },
  19. iconWeatherMap:[{tit:'风',val:'weaiona',children:['有风', '平静', '微风', '和风', '清风', '强风/劲风', '疾风', '大风', '烈风', '风暴', '狂爆风', '飓风', '热带风暴', '龙卷风']},
  20. {tit:'多云',val:'weaionb',children:['少云','多云']},
  21. {tit:'晴间多云',val:'weaionc',children:['晴间多云', ]},
  22. {tit:'雪',val:'weaiond',children:['雪', '阵雪', '小雪', '中雪', '大雪', '暴雪', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '冷']},
  23. {tit:'雾',val:'weaione',children:['浮尘', '扬沙', '沙尘暴', '强沙尘暴', '雾', '浓雾', '强浓雾', '轻雾', '大雾', '特强浓雾']},
  24. {tit:'晴',val:'weaionf',children:['晴', '热']},
  25. {tit:'雨夹雪',val:'weaiong',children:['雨雪天气', '雨夹雪', '阵雨夹雪']},
  26. {tit:'雨',val:'weaionh',children:['阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '小雨', '中雨', '大雨', '暴雨', '大暴雨', '特大暴雨', '强阵雨', '强雷阵雨', '极端降雨', '毛毛雨/细雨', '雨', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '冻雨']},
  27. {tit:'阴',val:'weaioni',children:['阴','未知']},
  28. {tit:'霾',val:'weaionj',children:['霾', '中度霾', '重度霾', '严重霾',]},
  29. ],
  30. };
  31. function setUxLocation(UxLocation){
  32. UxLocation = UxLocation || {};
  33. __CFG.lng = UxLocation.lng || '';
  34. __CFG.lat = UxLocation.lat || '';
  35. __CFG.address = UxLocation.address || UxLocation.addr || '';
  36. __CFG.city = UxLocation.city || UxLocation.city || '';
  37. __CFG.UxLocation = UxLocation;
  38. }
  39. //设置浏览器cookie
  40. function set_cookie(key, value, expire, path){
  41. var keys = __CFG.C_PREFIX + key;
  42. path = path || '/';
  43. var a = keys + "=" + value;
  44. if(typeof(expire) != 'undefined'){
  45. var date = new Date();
  46. expire = parseInt(expire,10);
  47. date.setTime(date.getTime() + expire*1000);
  48. a += "; expires="+date.toGMTString();
  49. }
  50. a += ";path="+path;
  51. if(__CFG.C_DOMAIN){
  52. a += ";domain="+__CFG.C_DOMAIN;
  53. }
  54. document.cookie = a;
  55. };
  56. //删除浏览器cookie
  57. function remove_cookie(key){
  58. if (!key) {
  59. return false;
  60. } else {
  61. var keys = __CFG.C_PREFIX + key;
  62. document.cookie = keys+"=;expires="+(new Date(0)).toGMTString();
  63. }
  64. };
  65. //获取浏览器cookie
  66. function get_cookie(key) {
  67. if (!key) {
  68. return false;
  69. } else {
  70. var keys = __CFG.C_PREFIX + key;
  71. var reg = new RegExp("(^| )" + keys + "=([^;]*)(;|\x24)");
  72. var result = reg.exec(document.cookie);
  73. if (result) {
  74. return result[2] || '';
  75. }
  76. return '';
  77. }
  78. };
  79. function getLocation(callback) {
  80. callback = typeof(callback) == 'function' ? callback : function(res) {};
  81. var num=1;
  82. // #ifdef MP-WEIXIN
  83. uni.getSetting({
  84. success(res) {
  85. if(res.authSetting['scope.userLocation'] != undefined){
  86. if (res.authSetting['scope.userLocation']) {
  87. getLocationaddr(callback)
  88. // 已经授权,可以直接调用 getLocation 获取地理位置
  89. } else {
  90. showConfirm('您还未开启定位,是否去开启定位?').then(cres => {
  91. if (cres.confirm) {
  92. uni.openSetting({
  93. success(res2) {
  94. if (res2.authSetting['scope.userLocation']) {
  95. }
  96. }
  97. });
  98. callback(-1)
  99. }
  100. })
  101. };
  102. }else{
  103. var uxlocation = {"lng": "", "lat": "", "address": ""};
  104. // var citynum=1;
  105. uni.getLocation({
  106. type: 'gcj02',
  107. geocode: 'true',
  108. success: function(ares) {
  109. uxlocation.lat = ares.latitude;
  110. uxlocation.lng = res.longitude;
  111. // myAmapFun.getRegeo({
  112. // success: function(data) {
  113. // // citynum=0;
  114. // uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
  115. // uxlocation.city = data[0].regeocodeData.addressComponent.city;
  116. // setUxLocation(uxlocation);
  117. // callback(uxlocation);
  118. // //成功回调
  119. // },
  120. // fail: function(info) {
  121. // //失败回调
  122. // }
  123. // })
  124. // 获取天气
  125. myAmapFun.getWeather({
  126. success(res){
  127. //temperature 温度
  128. //weather 天气现象
  129. uxlocation.city = res.city.data;
  130. uxlocation.temperature = res.temperature.data;
  131. uxlocation.weather = res.weather.data;
  132. var weather=res.weather.data;
  133. var weatherMap=__CFG.iconWeatherMap;
  134. var url=require('@/static/images/weather/weaionf.png');
  135. Object.keys(weatherMap).some((key) => {
  136. if (weatherMap[key].children.indexOf(weather)!=-1) {
  137. url=require(`@/static/images/weather/${weatherMap[key].val}.png`)
  138. return true;
  139. }
  140. })
  141. uxlocation.icon = url;
  142. setUxLocation(uxlocation);
  143. callback(uxlocation);
  144. },
  145. fail(err){
  146. console.log('fail')
  147. }
  148. })
  149. // if(citynum==1){
  150. // setUxLocation(uxlocation);
  151. // callback(uxlocation);
  152. // }
  153. },
  154. fail: function(error) {
  155. uni.showToast({
  156. icon: 'none',
  157. title: '定位失败'
  158. })
  159. // #ifdef APP-PLUS
  160. // #endif
  161. },
  162. complete: function(rs) {
  163. // console.log('complete:'+JSON.stringify(rs))
  164. }
  165. })
  166. }
  167. },
  168. fail(err) {
  169. console.log(err)
  170. },
  171. })
  172. // #endif
  173. // #ifdef APP-PLUS
  174. let system = uni.getSystemInfoSync();
  175. if(system.platform === "android"){
  176. var context = plus.android.importClass("android.content.Context");
  177. var locationManger = plus.android.importClass("android.location.LocationManager");
  178. var main = plus.android.runtimeMainActivity();
  179. var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  180. if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
  181. uni.showModal({
  182. title:"提示",
  183. content:"请授权位置获取地址",
  184. showCancel:false,
  185. success() {
  186. if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
  187. var Intent = plus.android.importClass("android.content.Intent");
  188. var Settings = plus.android.importClass("android.provider.Settings");
  189. var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  190. main.startActivity(intent);
  191. }else{
  192. console.log("定位已开启")
  193. _self.setSite()
  194. }
  195. }
  196. })
  197. }else{
  198. plus.android.requestPermissions(
  199. ['android.permission.ACCESS_FINE_LOCATION'], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  200. function(resultObj) {
  201. var result = 0;
  202. for (var i = 0; i < resultObj.granted.length; i++) {
  203. var grantedPermission = resultObj.granted[i];
  204. console.log('已获取的权限:' + grantedPermission);
  205. result = 1
  206. }
  207. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  208. var deniedPresentPermission = resultObj.deniedPresent[i];
  209. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  210. result = 0
  211. }
  212. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  213. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  214. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  215. result = -1
  216. }
  217. // resolve(result);
  218. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  219. if (result != 1) {
  220. showConfirm('您还未开启定位,是否去开启定位?').then(res => {
  221. if (res.confirm) {
  222. gotoAppPermissionSetting()
  223. callback(result)
  224. }
  225. })
  226. }else{
  227. getLocationaddr(callback)
  228. }
  229. },
  230. function(error) {
  231. console.log('申请权限错误:' + error.code + " = " + error.message);
  232. // resolve({
  233. // code: error.code,
  234. // message: error.message
  235. // });
  236. }
  237. );
  238. }
  239. }
  240. // #endif
  241. };
  242. // 跳转到**应用**的权限页面
  243. function gotoAppPermissionSetting() {
  244. var isIos
  245. // #ifdef APP-PLUS
  246. isIos = (plus.os.name == "iOS")
  247. // #endif
  248. if (isIos) {
  249. var UIApplication = plus.ios.import("UIApplication");
  250. var application2 = UIApplication.sharedApplication();
  251. var NSURL2 = plus.ios.import("NSURL");
  252. // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
  253. var setting2 = NSURL2.URLWithString("app-settings:");
  254. application2.openURL(setting2);
  255. plus.ios.deleteObject(setting2);
  256. plus.ios.deleteObject(NSURL2);
  257. plus.ios.deleteObject(application2);
  258. } else {
  259. var Intent = plus.android.importClass("android.content.Intent");
  260. var Settings = plus.android.importClass("android.provider.Settings");
  261. var Uri = plus.android.importClass("android.net.Uri");
  262. var mainActivity = plus.android.runtimeMainActivity();
  263. var intent = new Intent();
  264. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  265. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  266. intent.setData(uri);
  267. mainActivity.startActivity(intent);
  268. }
  269. }
  270. function getLocationaddr(callback){
  271. callback = typeof(callback) == 'function' ? callback : function(res) {};
  272. var uxlocation = {"lng": "", "lat": "", "address": ""};
  273. uni.getLocation({
  274. type: 'gcj02',
  275. geocode: 'true',
  276. success: function(res) {
  277. uxlocation.lat = res.latitude;
  278. uxlocation.lng = res.longitude;
  279. // myAmapFun.getRegeo({
  280. // success: function(data) {
  281. // uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
  282. // uxlocation.city = data[0].regeocodeData.addressComponent.city;
  283. // setUxLocation(uxlocation);
  284. // callback(uxlocation);
  285. // },
  286. // fail: function(info) {
  287. // //失败回调
  288. // console.log(info,223)
  289. // }
  290. // })
  291. // 获取天气
  292. myAmapFun.getWeather({
  293. success(res){
  294. //temperature 温度
  295. //weather 天气现象
  296. uxlocation.city = res.city.data;
  297. uxlocation.temperature = res.temperature.data;
  298. uxlocation.weather = res.weather.data;
  299. var weather=res.weather.data;
  300. var weatherMap=__CFG.iconWeatherMap;
  301. var url=require('@/static/images/weather/weaionf.png');
  302. Object.keys(weatherMap).some((key) => {
  303. if (weatherMap[key].children.indexOf(weather)!=-1) {
  304. url=require(`@/static/images/weather/${weatherMap[key].val}.png`)
  305. return true;
  306. }
  307. })
  308. uxlocation.icon = url;
  309. setUxLocation(uxlocation);
  310. callback(uxlocation);
  311. },
  312. fail(err){
  313. console.log('fail')
  314. }
  315. })
  316. },
  317. fail: function(error) {
  318. uni.showToast({
  319. icon: 'none',
  320. title: '定位失败'
  321. })
  322. // #ifdef APP-PLUS
  323. // #endif
  324. },
  325. complete: function(rs) {
  326. // console.log('complete:'+JSON.stringify(rs))
  327. }
  328. })
  329. }
  330. const self = {
  331. getLocation,
  332. setUxLocation,
  333. __CFG
  334. }
  335. export default self