洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.9 KiB

4 months ago
  1. import permision from '@/common/js/permission.js'
  2. export async function getLocation(cb) {
  3. // #ifdef APP-PLUS
  4. let status = await checkPermission();
  5. if (status !== 1) {
  6. return status;
  7. }
  8. // #endif
  9. uni.showModal({
  10. content: '为了您更好的给您推荐附近的驾校数据,需要获取位置信息,app想要获取您的位权限',
  11. confirmText: "确定",
  12. success: function(res) {
  13. if (res.confirm) {
  14. doGetLocation(cb);
  15. } else if (res.cancel) {
  16. console.log('用户点击取消');
  17. }
  18. }
  19. });
  20. }
  21. /**
  22. * 获取当前位置信息
  23. */
  24. export const doGetLocation = (cb) => {
  25. uni.getLocation({
  26. success: (res) => {
  27. console.log('当前位置:', res.longitude, res.latitude);
  28. if (typeof cb == 'function') cb && typeof cb == 'function' && cb(status)
  29. },
  30. fail: (err) => {
  31. if (err.errMsg.indexOf("auth deny") >= 0) {
  32. uni.showToast({
  33. title: "访问位置被拒绝",
  34. icon: 'none'
  35. })
  36. } else {
  37. uni.showToast({
  38. title: err.errMsg,
  39. icon: 'none'
  40. })
  41. }
  42. }
  43. })
  44. }
  45. /**
  46. * 检测定位权限
  47. */
  48. async function checkPermission() {
  49. let status = permision.isIOS ? await permision.requestIOS('location') :
  50. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  51. if (status === null || status === 1) {
  52. status = 1;
  53. } else if (status === 2) {
  54. uni.showModal({
  55. content: "系统定位已关闭",
  56. showCancel: false,
  57. success: function(res) {}
  58. })
  59. } else if (status.code) {
  60. uni.showModal({
  61. content: status.message
  62. })
  63. } else {
  64. uni.showModal({
  65. content: "为了您更好的体验App蓝牙功能,需要获取位置信息,请点击设置开启定位权限",
  66. confirmText: "设置",
  67. success: function(res) {
  68. if (res.confirm) {
  69. permision.gotoAppSetting();
  70. }
  71. }
  72. })
  73. }
  74. return status;
  75. }
  76. const location = {
  77. getLocation
  78. }
  79. export default location;