location.js 10.0 KB

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