location.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. var that = this;
  83. // #ifdef H5
  84. var lng = '117.211954';
  85. var lat = '31.839676';
  86. that.__CFG.lng = lng;
  87. that.__CFG.lat = lat;
  88. callback();
  89. return
  90. uni.getLocation({
  91. type: 'gcj02',
  92. geocode: true,
  93. success: function(res) {
  94. // console.log(res)
  95. // var lng=res.longitude;
  96. // var lat=res.latitude;
  97. // that.user.lng=res.longitude;
  98. // that.user.lat=res.latitude;
  99. var lng = '117.211954';
  100. var lat = '31.839676';
  101. that.__CFG.lng = lng;
  102. that.__CFG.lat = lng;
  103. callback();
  104. // console.log('当前位置的经度:' + res.longitude);
  105. // console.log('当前位置的纬度:' + res.latitude);
  106. },
  107. fail: function(red) {
  108. console.log(red,88)
  109. },
  110. })
  111. // #endif
  112. // #ifdef MP-WEIXIN
  113. uni.getSetting({
  114. success(res) {
  115. if(res.authSetting['scope.userLocation'] != undefined){
  116. if (res.authSetting['scope.userLocation']) {
  117. getLocationaddr(callback)
  118. // 已经授权,可以直接调用 getLocation 获取地理位置
  119. } else {
  120. showConfirm('您还未开启定位,是否去开启定位?').then(cres => {
  121. if (cres.confirm) {
  122. uni.openSetting({
  123. success(res2) {
  124. if (res2.authSetting['scope.userLocation']) {
  125. }
  126. }
  127. });
  128. callback(-1)
  129. }
  130. })
  131. };
  132. }else{
  133. var uxlocation = {"lng": "", "lat": "", "address": ""};
  134. // var citynum=1;
  135. uni.getLocation({
  136. type: 'gcj02',
  137. geocode: 'true',
  138. success: function(ares) {
  139. uxlocation.lat = ares.latitude;
  140. uxlocation.lng = res.longitude;
  141. // myAmapFun.getRegeo({
  142. // success: function(data) {
  143. // // citynum=0;
  144. // uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
  145. // uxlocation.city = data[0].regeocodeData.addressComponent.city;
  146. // setUxLocation(uxlocation);
  147. // callback(uxlocation);
  148. // //成功回调
  149. // },
  150. // fail: function(info) {
  151. // //失败回调
  152. // }
  153. // })
  154. // 获取天气
  155. myAmapFun.getWeather({
  156. success(res){
  157. //temperature 温度
  158. //weather 天气现象
  159. uxlocation.city = res.city.data;
  160. uxlocation.temperature = res.temperature.data;
  161. uxlocation.weather = res.weather.data;
  162. var weather=res.weather.data;
  163. var weatherMap=__CFG.iconWeatherMap;
  164. var url=require('@/static/images/weather/weaionf.png');
  165. Object.keys(weatherMap).some((key) => {
  166. if (weatherMap[key].children.indexOf(weather)!=-1) {
  167. url=require(`@/static/images/weather/${weatherMap[key].val}.png`)
  168. return true;
  169. }
  170. })
  171. uxlocation.icon = url;
  172. setUxLocation(uxlocation);
  173. callback(uxlocation);
  174. },
  175. fail(err){
  176. console.log('fail')
  177. }
  178. })
  179. // if(citynum==1){
  180. // setUxLocation(uxlocation);
  181. // callback(uxlocation);
  182. // }
  183. },
  184. fail: function(error) {
  185. uni.showToast({
  186. icon: 'none',
  187. title: '定位失败'
  188. })
  189. // #ifdef APP-PLUS
  190. // #endif
  191. },
  192. complete: function(rs) {
  193. // console.log('complete:'+JSON.stringify(rs))
  194. }
  195. })
  196. }
  197. },
  198. fail(err) {
  199. console.log(err)
  200. },
  201. })
  202. // #endif
  203. // #ifdef APP-PLUS
  204. let system = uni.getSystemInfoSync();
  205. if(system.platform === "android"){
  206. var context = plus.android.importClass("android.content.Context");
  207. var locationManger = plus.android.importClass("android.location.LocationManager");
  208. var main = plus.android.runtimeMainActivity();
  209. var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  210. if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
  211. uni.showModal({
  212. title:"提示",
  213. content:"请授权位置获取地址",
  214. showCancel:false,
  215. success() {
  216. if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
  217. var Intent = plus.android.importClass("android.content.Intent");
  218. var Settings = plus.android.importClass("android.provider.Settings");
  219. var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  220. main.startActivity(intent);
  221. }else{
  222. console.log("定位已开启")
  223. _self.setSite()
  224. }
  225. }
  226. })
  227. }else{
  228. plus.android.requestPermissions(
  229. ['android.permission.ACCESS_FINE_LOCATION'], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  230. function(resultObj) {
  231. var result = 0;
  232. for (var i = 0; i < resultObj.granted.length; i++) {
  233. var grantedPermission = resultObj.granted[i];
  234. console.log('已获取的权限:' + grantedPermission);
  235. result = 1
  236. }
  237. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  238. var deniedPresentPermission = resultObj.deniedPresent[i];
  239. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  240. result = 0
  241. }
  242. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  243. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  244. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  245. result = -1
  246. }
  247. // resolve(result);
  248. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  249. if (result != 1) {
  250. showConfirm('您还未开启定位,是否去开启定位?').then(res => {
  251. if (res.confirm) {
  252. gotoAppPermissionSetting()
  253. callback(result)
  254. }
  255. })
  256. }else{
  257. getLocationaddr(callback)
  258. }
  259. },
  260. function(error) {
  261. console.log('申请权限错误:' + error.code + " = " + error.message);
  262. // resolve({
  263. // code: error.code,
  264. // message: error.message
  265. // });
  266. }
  267. );
  268. }
  269. }
  270. // #endif
  271. };
  272. // 跳转到**应用**的权限页面
  273. function gotoAppPermissionSetting() {
  274. var isIos
  275. // #ifdef APP-PLUS
  276. isIos = (plus.os.name == "iOS")
  277. // #endif
  278. if (isIos) {
  279. var UIApplication = plus.ios.import("UIApplication");
  280. var application2 = UIApplication.sharedApplication();
  281. var NSURL2 = plus.ios.import("NSURL");
  282. // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
  283. var setting2 = NSURL2.URLWithString("app-settings:");
  284. application2.openURL(setting2);
  285. plus.ios.deleteObject(setting2);
  286. plus.ios.deleteObject(NSURL2);
  287. plus.ios.deleteObject(application2);
  288. } else {
  289. var Intent = plus.android.importClass("android.content.Intent");
  290. var Settings = plus.android.importClass("android.provider.Settings");
  291. var Uri = plus.android.importClass("android.net.Uri");
  292. var mainActivity = plus.android.runtimeMainActivity();
  293. var intent = new Intent();
  294. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  295. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  296. intent.setData(uri);
  297. mainActivity.startActivity(intent);
  298. }
  299. }
  300. function getLocationaddr(callback){
  301. callback = typeof(callback) == 'function' ? callback : function(res) {};
  302. var uxlocation = {"lng": "", "lat": "", "address": ""};
  303. uni.getLocation({
  304. type: 'gcj02',
  305. geocode: 'true',
  306. success: function(res) {
  307. uxlocation.lat = res.latitude;
  308. uxlocation.lng = res.longitude;
  309. // myAmapFun.getRegeo({
  310. // success: function(data) {
  311. // uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
  312. // uxlocation.city = data[0].regeocodeData.addressComponent.city;
  313. // setUxLocation(uxlocation);
  314. // callback(uxlocation);
  315. // },
  316. // fail: function(info) {
  317. // //失败回调
  318. // console.log(info,223)
  319. // }
  320. // })
  321. // 获取天气
  322. myAmapFun.getWeather({
  323. success(res){
  324. //temperature 温度
  325. //weather 天气现象
  326. uxlocation.city = res.city.data;
  327. uxlocation.temperature = res.temperature.data;
  328. uxlocation.weather = res.weather.data;
  329. var weather=res.weather.data;
  330. var weatherMap=__CFG.iconWeatherMap;
  331. var url=require('@/static/images/weather/weaionf.png');
  332. Object.keys(weatherMap).some((key) => {
  333. if (weatherMap[key].children.indexOf(weather)!=-1) {
  334. url=require(`@/static/images/weather/${weatherMap[key].val}.png`)
  335. return true;
  336. }
  337. })
  338. uxlocation.icon = url;
  339. setUxLocation(uxlocation);
  340. callback(uxlocation);
  341. },
  342. fail(err){
  343. console.log('fail')
  344. }
  345. })
  346. },
  347. fail: function(error) {
  348. uni.showToast({
  349. icon: 'none',
  350. title: '定位失败'
  351. })
  352. // #ifdef APP-PLUS
  353. // #endif
  354. },
  355. complete: function(rs) {
  356. // console.log('complete:'+JSON.stringify(rs))
  357. }
  358. })
  359. }
  360. const self = {
  361. getLocation,
  362. setUxLocation,
  363. __CFG
  364. }
  365. export default self