component.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view><!-- 以下是测试用例 -->
  3. <u-button type="primary" text="开启定位" @click="startPosition()"></u-button>
  4. <u-button type="primary" :plain="true" text="增加定位" @click="addPosition(Math.floor(Math.random() * 10) + ``)"></u-button>
  5. <u--input v-model="value" placeholder="请输入需要清除的id" border="surround"></u--input>
  6. <u-button type="primary" text="关闭定位" @click="closePosition()"></u-button>
  7. <u-button type="primary" :plain="true" text="清空缓存" @click="cleanData()"></u-button>
  8. </view>
  9. </template>
  10. <script>
  11. import {updateLngAndLat} from '@/api/api.js';
  12. export default {
  13. name: "positionInfo",
  14. data() {
  15. return {
  16. value: '',
  17. positionInfo: {watchLocation: null,positionData: [],location: null},
  18. maximumAge: 30 * 1000};},
  19. methods: {/*** 检测是否开启定位*/
  20. checkOpenGPSServiceByAndroidIOS() {
  21. let system = uni.getSystemInfoSync(); // 获取系统信息
  22. if (system.platform === 'android') { // 判断平台
  23. var context = plus.android.importClass("android.content.Context");
  24. var locationManager = plus.android.importClass("android.location.LocationManager");
  25. var main = plus.android.runtimeMainActivity();
  26. var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
  27. if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
  28. uni.showModal({title: '提示',content: '请打开定位服务功能',showCancel: false, // 不显示取消按钮
  29. success() {
  30. if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
  31. var Intent = plus.android.importClass('android.content.Intent');
  32. var Settings = plus.android.importClass('android.provider.Settings');
  33. var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  34. main.startActivity(intent); // 打开系统设置GPS服务页面
  35. } else {
  36. return true;
  37. console.log('GPS功能已开启');
  38. }
  39. }
  40. });
  41. } else {
  42. return true;
  43. }
  44. } else if (system.platform === 'ios') {
  45. console.log("苹果");
  46. var cllocationManger = plus.ios.import("CLLocationManager");
  47. var enable = cllocationManger.locationServicesEnabled();
  48. var status = cllocationManger.authorizationStatus();
  49. plus.ios.deleteObject(cllocationManger);
  50. if (enable && status != 2) {
  51. return true;
  52. console.log("手机系统的定位已经打开");
  53. } else {
  54. console.log("手机系统的定位没有打开");
  55. uni.showModal({title: '提示',content: '请前往设置-隐私-定位服务打开定位服务功能',showCancel: false, // 不显示取消按钮
  56. success() {
  57. var UIApplication = plus.ios.import("UIApplication");
  58. var application2 = UIApplication.sharedApplication();
  59. var NSURL2 = plus.ios.import("NSURL");//
  60. var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");//
  61. var setting2 = NSURL2.URLWithString("App-Prefs:root=LOCATION_SERVICES");
  62. var setting2 = NSURL2.URLWithString("app-settings:");//
  63. var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");//
  64. var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION_SERVICES");
  65. application2.openURL(setting2);
  66. plus.ios.deleteObject(setting2);
  67. plus.ios.deleteObject(NSURL2);
  68. plus.ios.deleteObject(application2);
  69. }
  70. });
  71. }
  72. }
  73. return false;
  74. },
  75. /*** 启动获取定位信息(建议登录时调用)*/
  76. startPosition() {
  77. let _this = this;
  78. if (this.locationInfo != undefined) {
  79. let positionData = this.locationInfo.positionData;
  80. if (positionData.length > 0) {
  81. let flag = _this.checkOpenGPSServiceByAndroidIOS();
  82. if (flag) {
  83. let watchLocation = plus.geolocation.watchPosition((position) => {
  84. let coords = position.coords;
  85. _this.locationInfo.location = coords;
  86. // console.log("获取经纬度信息:", coords.longitude, coords.latitude)//更新经纬度
  87. _this.pushPosition(coords.longitude, coords.latitude);
  88. }, function(e) {
  89. console.log('Gelocation Error: code - ' + e.code + '; message - ' + e.message);
  90. }, {maximumAge: _this.maximumAge});
  91. _this.locationInfo.watchLocation = watchLocation;//缓存经纬度信息
  92. this.$u.vuex('locationInfo', _this.locationInfo);
  93. }
  94. }
  95. }
  96. },
  97. /*** 增加获取位置信息的数量* @param {Object} id 传入的id(例如:repairId)*/
  98. addPosition(id) {
  99. let flag = this.checkOpenGPSServiceByAndroidIOS();
  100. if (flag) {
  101. let _this = this;
  102. let positionData = _this.locationInfo === undefined ? [] : _this.locationInfo.positionData;
  103. if (positionData.length === 0) {
  104. var timer = plus.geolocation.watchPosition((position) => {
  105. let coords = position.coords;
  106. this.$nextTick(() => {this.positionInfo = {...this.locationInfo};
  107. this.positionInfo.location = coords;//
  108. console.log("获取经纬度信息:", this.positionInfo)
  109. this.$u.vuex('locationInfo', this.positionInfo);
  110. })//更新经纬度
  111. _this.pushPosition(coords.longitude, coords.latitude);
  112. }, function(e) {
  113. console.log('Gelocation Error: code - ' + e.code + '; message - ' + e.message);
  114. }, {maximumAge: _this.maximumAge});
  115. _this.positionInfo.watchLocation = timer;
  116. } else {
  117. let num = positionData.filter(t => t === id).length;
  118. if (num > 0) {
  119. return;
  120. }
  121. _this.positionInfo = {..._this.locationInfo};
  122. }
  123. positionData.push(id);
  124. _this.positionInfo.positionData = positionData;//缓存位置信息
  125. this.$u.vuex('locationInfo', _this.positionInfo);//
  126. console.log("新增缓存位置信息:", this.locationInfo)
  127. }
  128. },
  129. /*** 清除定时任务* @param {Object} id 传入的id(例如:repairId)*/
  130. closePosition() {
  131. let id = this.value;
  132. let _this = this;//清除传入的id
  133. let positionData = _this.locationInfo.positionData.filter(t => t !== id);
  134. if (positionData.length === 0) {
  135. plus.geolocation.clearWatch(_this.locationInfo.watchLocation);
  136. _this.positionInfo = {watchLocation: null,positionData: [],location: null};
  137. } else {
  138. _this.positionInfo = {positionData: positionData,watchLocation: _this.locationInfo.watchLocation,location: _this.locationInfo.location};
  139. }
  140. _this.$u.vuex('locationInfo', _this.positionInfo);//
  141. console.log("清除任务后的缓存:", _this.locationInfo)
  142. },
  143. /*** 往后台推送最新的经纬度信息* @param {Object} 传到后台存入redis的key(多个逗号分开)* @param {Object} longitude 经度* @param {Object} latitude 纬度*/
  144. pushPosition(longitude, latitude) {
  145. let id = this.locationInfo.positionData.join(",");
  146. updateLngAndLat({key: id,longitude: longitude,latitude: latitude})
  147. .then(res => {
  148. console.log("更新经纬度信息", res)});
  149. },
  150. cleanData() {
  151. this.$u.vuex('locationInfo', undefined);//
  152. console.log("清空后的缓存:", this.locationInfo)
  153. },
  154. },
  155. }
  156. </script><style></style>