gps.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // #ifdef APP-PLUS
  2. import permision from "./wa-permission/permission.js"
  3. // #endif
  4. class Gps {
  5. constructor(arg) {
  6. this.lock = false //锁防止重复请求
  7. }
  8. async getLocation(param = {
  9. type: 'wgs84'
  10. }) {
  11. return new Promise(async (callback) => {
  12. if (this.lock) {
  13. // console.log('已锁,已有另一个请求正在执行。无需重复请求');
  14. callback(false);
  15. return false
  16. }
  17. this.lock = true //加锁防止重复的请求
  18. uni.getLocation({
  19. ...param,
  20. success: res => {
  21. this.lock = false //成功后解锁
  22. //console.log(res);
  23. callback(res)
  24. },
  25. fail: async (err) => {
  26. uni.showToast({
  27. title: '定位获取失败',
  28. icon: 'none'
  29. });
  30. console.error(err)
  31. callback(false)
  32. // #ifdef APP-PLUS
  33. await this.checkGpsIsOpen()
  34. // #endif
  35. // #ifdef MP-WEIXIN
  36. if (err.errMsg == 'getLocation:fail auth deny') {
  37. uni.showModal({
  38. content: '应用无定位权限',
  39. confirmText: '前往设置',
  40. complete: (e) => {
  41. if (e.confirm) {
  42. uni.openSetting({
  43. success(res) {
  44. console.log(res.authSetting)
  45. }
  46. });
  47. }
  48. this.lock = false //解锁让回到界面重新获取
  49. }
  50. });
  51. }
  52. if (err.errMsg == 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
  53. uni.showModal({
  54. content: '未开启定位权限,请前往手机系统设置中开启',
  55. showCancel: false,
  56. confirmText:"知道了"
  57. });
  58. }
  59. // #endif
  60. }
  61. });
  62. })
  63. }
  64. // #ifdef APP-PLUS
  65. async checkGpsIsOpen() {
  66. this.lock = true //加锁防止重复的请求
  67. console.log('检查定位设置开启问题', permision.checkSystemEnableLocation());
  68. if (!permision.checkSystemEnableLocation()) {
  69. plus.nativeUI.confirm("手机定位权限没有开启,是否去开启?", (e) => {
  70. this.lock = false
  71. if (e.index == 0) {
  72. if (uni.getSystemInfoSync().platform == "ios") {
  73. plus.runtime.openURL("app-settings://");
  74. } else {
  75. var main = plus.android.runtimeMainActivity(); //获取activity
  76. var Intent = plus.android.importClass('android.content.Intent');
  77. var Settings = plus.android.importClass('android.provider.Settings');
  78. var intent = new Intent(Settings
  79. .ACTION_LOCATION_SOURCE_SETTINGS); //可设置表中所有Action字段
  80. main.startActivity(intent);
  81. }
  82. } else {
  83. uni.showToast({
  84. title: '设备无定位权限',
  85. icon: 'none'
  86. });
  87. callback(false)
  88. }
  89. }, {
  90. "buttons": ["去设置", "不开启"],
  91. "verticalAlign": "center"
  92. });
  93. return false
  94. }
  95. let checkAppGpsRes = await this.checkAppGps()
  96. console.log(checkAppGpsRes, 'checkAppGpsRes');
  97. if (!checkAppGpsRes) {
  98. plus.nativeUI.confirm("应用定位权限没有开启,是否去开启?", (e) => {
  99. this.lock = false
  100. if (e.index == 0) {
  101. permision.gotoAppPermissionSetting()
  102. callback(false)
  103. } else {
  104. uni.showToast({
  105. title: '应用无定位权限',
  106. icon: 'none'
  107. });
  108. return false
  109. }
  110. }, {
  111. "buttons": ["去设置", "不开启"],
  112. "verticalAlign": "center"
  113. });
  114. } else {
  115. this.lock = false
  116. }
  117. }
  118. async checkAppGps() {
  119. if (uni.getSystemInfoSync().platform == "ios" && !permision.judgeIosPermission("location")) {
  120. return false
  121. }
  122. if (uni.getSystemInfoSync().platform != "ios" && await permision.requestAndroidPermission(
  123. "android.permission.ACCESS_FINE_LOCATION") != 1) {
  124. return false
  125. }
  126. return true
  127. }
  128. // #endif
  129. }
  130. export default Gps