location.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. },
  16. };
  17. function setUxLocation(UxLocation){
  18. UxLocation = UxLocation || {};
  19. __CFG.lng = UxLocation.lng || '';
  20. __CFG.lat = UxLocation.lat || '';
  21. __CFG.address = UxLocation.address || UxLocation.addr || '';
  22. __CFG.city = UxLocation.city || UxLocation.city || '';
  23. __CFG.UxLocation = UxLocation;
  24. }
  25. //设置浏览器cookie
  26. function set_cookie(key, value, expire, path){
  27. var keys = __CFG.C_PREFIX + key;
  28. path = path || '/';
  29. var a = keys + "=" + value;
  30. if(typeof(expire) != 'undefined'){
  31. var date = new Date();
  32. expire = parseInt(expire,10);
  33. date.setTime(date.getTime() + expire*1000);
  34. a += "; expires="+date.toGMTString();
  35. }
  36. a += ";path="+path;
  37. if(__CFG.C_DOMAIN){
  38. a += ";domain="+__CFG.C_DOMAIN;
  39. }
  40. document.cookie = a;
  41. };
  42. //删除浏览器cookie
  43. function remove_cookie(key){
  44. if (!key) {
  45. return false;
  46. } else {
  47. var keys = __CFG.C_PREFIX + key;
  48. document.cookie = keys+"=;expires="+(new Date(0)).toGMTString();
  49. }
  50. };
  51. //获取浏览器cookie
  52. function get_cookie(key) {
  53. if (!key) {
  54. return false;
  55. } else {
  56. var keys = __CFG.C_PREFIX + key;
  57. var reg = new RegExp("(^| )" + keys + "=([^;]*)(;|\x24)");
  58. var result = reg.exec(document.cookie);
  59. if (result) {
  60. return result[2] || '';
  61. }
  62. return '';
  63. }
  64. };
  65. function getLocation(callback) {
  66. callback = typeof(callback) == 'function' ? callback : function(res) {};
  67. var num=1;
  68. // #ifdef MP-WEIXIN
  69. uni.getSetting({
  70. success(res) {
  71. if(res.authSetting['scope.userLocation'] != undefined){
  72. if (res.authSetting['scope.userLocation']) {
  73. getLocationaddr(callback)
  74. // 已经授权,可以直接调用 getLocation 获取地理位置
  75. } else {
  76. showConfirm('您还未开启定位,是否去开启定位?').then(cres => {
  77. if (cres.confirm) {
  78. uni.openSetting({
  79. success(res2) {
  80. if (res2.authSetting['scope.userLocation']) {
  81. }
  82. }
  83. });
  84. callback(-1)
  85. }
  86. })
  87. };
  88. }else{
  89. var uxlocation = {"lng": "", "lat": "", "address": ""};
  90. // var citynum=1;
  91. uni.getLocation({
  92. type: 'gcj02',
  93. geocode: 'true',
  94. success: function(ares) {
  95. uxlocation.lat = ares.latitude;
  96. uxlocation.lng = res.longitude;
  97. myAmapFun.getRegeo({
  98. success: function(data) {
  99. // citynum=0;
  100. uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
  101. uxlocation.city = data[0].regeocodeData.addressComponent.city;
  102. setUxLocation(uxlocation);
  103. // uni.hideLoading();
  104. // console.log(uxlocation,23)
  105. callback(uxlocation);
  106. //成功回调
  107. },
  108. fail: function(info) {
  109. //失败回调
  110. }
  111. })
  112. // if(citynum==1){
  113. // setUxLocation(uxlocation);
  114. // callback(uxlocation);
  115. // }
  116. },
  117. fail: function(error) {
  118. uni.showToast({
  119. icon: 'none',
  120. title: '定位失败'
  121. })
  122. // #ifdef APP-PLUS
  123. // #endif
  124. },
  125. complete: function(rs) {
  126. // console.log('complete:'+JSON.stringify(rs))
  127. }
  128. })
  129. }
  130. },
  131. fail(err) {
  132. console.log(err)
  133. },
  134. })
  135. // #endif
  136. // #ifdef APP-PLUS
  137. let system = uni.getSystemInfoSync();
  138. if(system.platform === "android"){
  139. var context = plus.android.importClass("android.content.Context");
  140. var locationManger = plus.android.importClass("android.location.LocationManager");
  141. var main = plus.android.runtimeMainActivity();
  142. var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  143. if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
  144. uni.showModal({
  145. title:"提示",
  146. content:"请授权位置获取地址",
  147. showCancel:false,
  148. success() {
  149. if(!mainSvr.isProviderEnabled(locationManger.GPS_PROVIDER)){
  150. var Intent = plus.android.importClass("android.content.Intent");
  151. var Settings = plus.android.importClass("android.provider.Settings");
  152. var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  153. main.startActivity(intent);
  154. }else{
  155. console.log("定位已开启")
  156. _self.setSite()
  157. }
  158. }
  159. })
  160. }else{
  161. plus.android.requestPermissions(
  162. ['android.permission.ACCESS_FINE_LOCATION'], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  163. function(resultObj) {
  164. var result = 0;
  165. for (var i = 0; i < resultObj.granted.length; i++) {
  166. var grantedPermission = resultObj.granted[i];
  167. console.log('已获取的权限:' + grantedPermission);
  168. result = 1
  169. }
  170. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  171. var deniedPresentPermission = resultObj.deniedPresent[i];
  172. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  173. result = 0
  174. }
  175. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  176. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  177. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  178. result = -1
  179. }
  180. // resolve(result);
  181. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  182. if (result != 1) {
  183. showConfirm('您还未开启定位,是否去开启定位?').then(res => {
  184. if (res.confirm) {
  185. gotoAppPermissionSetting()
  186. callback(result)
  187. }
  188. })
  189. }else{
  190. getLocationaddr(callback)
  191. }
  192. },
  193. function(error) {
  194. console.log('申请权限错误:' + error.code + " = " + error.message);
  195. // resolve({
  196. // code: error.code,
  197. // message: error.message
  198. // });
  199. }
  200. );
  201. }
  202. }
  203. // #endif
  204. };
  205. // 跳转到**应用**的权限页面
  206. function gotoAppPermissionSetting() {
  207. var isIos
  208. // #ifdef APP-PLUS
  209. isIos = (plus.os.name == "iOS")
  210. // #endif
  211. if (isIos) {
  212. var UIApplication = plus.ios.import("UIApplication");
  213. var application2 = UIApplication.sharedApplication();
  214. var NSURL2 = plus.ios.import("NSURL");
  215. // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
  216. var setting2 = NSURL2.URLWithString("app-settings:");
  217. application2.openURL(setting2);
  218. plus.ios.deleteObject(setting2);
  219. plus.ios.deleteObject(NSURL2);
  220. plus.ios.deleteObject(application2);
  221. } else {
  222. var Intent = plus.android.importClass("android.content.Intent");
  223. var Settings = plus.android.importClass("android.provider.Settings");
  224. var Uri = plus.android.importClass("android.net.Uri");
  225. var mainActivity = plus.android.runtimeMainActivity();
  226. var intent = new Intent();
  227. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  228. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  229. intent.setData(uri);
  230. mainActivity.startActivity(intent);
  231. }
  232. }
  233. function getLocationaddr(callback){
  234. callback = typeof(callback) == 'function' ? callback : function(res) {};
  235. var uxlocation = {"lng": "", "lat": "", "address": ""};
  236. uni.getLocation({
  237. type: 'gcj02',
  238. geocode: 'true',
  239. success: function(res) {
  240. uxlocation.lat = res.latitude;
  241. uxlocation.lng = res.longitude;
  242. myAmapFun.getRegeo({
  243. success: function(data) {
  244. uxlocation.address = data[0].name+data[0].desc||data[0].regeocodeData.formatted_address;
  245. uxlocation.city = data[0].regeocodeData.addressComponent.city;
  246. setUxLocation(uxlocation);
  247. // uni.hideLoading();
  248. // console.log(uxlocation,23)
  249. callback(uxlocation);
  250. //成功回调
  251. },
  252. fail: function(info) {
  253. //失败回调
  254. console.log(info,223)
  255. }
  256. })
  257. // if(citynum==1){
  258. // setUxLocation(uxlocation);
  259. // callback(uxlocation);
  260. // }
  261. },
  262. fail: function(error) {
  263. uni.showToast({
  264. icon: 'none',
  265. title: '定位失败'
  266. })
  267. // #ifdef APP-PLUS
  268. // #endif
  269. },
  270. complete: function(rs) {
  271. // console.log('complete:'+JSON.stringify(rs))
  272. }
  273. })
  274. }
  275. const self = {
  276. getLocation,
  277. setUxLocation,
  278. __CFG
  279. }
  280. export default self