location.js 13 KB

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