open-setting-location.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <view>
  3. <view v-if="is_show_open_setting" class="open-setting-view">
  4. <view class="content bg-white">
  5. <view class="msg cr-gray">开启相应的权限服务</view>
  6. <view class="value cr-base">获取[ <text>位置信息</text> ]权限</view>
  7. <button type="primary" open-type="openSetting" size="mini" @opensetting="setting_callback_event">打开设置页</button>
  8. <view class="tc margin-top-sm">
  9. <navigator open-type="navigateBack" class="cp cr-gray dis-inline-block" hover-class="none">返回</navigator>
  10. </view>
  11. </view>
  12. </view>
  13. <view v-else class="open-setting-loding">
  14. <image :src="common_static_url+'bg-loding.gif'" class="avatar dis-block" mode="widthFix"></image>
  15. <view class="tc margin-top-sm">
  16. <navigator open-type="navigateBack" class="cp cr-gray dis-inline-block" hover-class="none">返回</navigator>
  17. </view>
  18. </view>
  19. <view v-if="(error_msg || null) != null" class="cr-red margin-top-lg padding-horizontal-main">{{error_msg}}</view>
  20. </view>
  21. </template>
  22. <script>
  23. const app = getApp();
  24. var common_static_url = app.globalData.get_static_url('common');
  25. export default {
  26. data() {
  27. return {
  28. common_static_url: common_static_url,
  29. params: null,
  30. is_show_open_setting: false,
  31. auth: 'scope.userLocation',
  32. cache_key: app.globalData.data.cache_userlocation_key,
  33. error_msg: null
  34. };
  35. },
  36. components: {},
  37. props: {},
  38. onLoad: function(params) {
  39. this.setData({
  40. params: params
  41. });
  42. this.init();
  43. },
  44. methods: {
  45. // 获取权限
  46. init() {
  47. // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
  48. var self = this;
  49. uni.getSetting({
  50. success(res) {
  51. if (!res.authSetting[self.auth]) {
  52. uni.authorize({
  53. scope: self.auth,
  54. success(res) {
  55. self.choose_location();
  56. },
  57. fail: res => {
  58. self.setData({
  59. is_show_open_setting: true
  60. });
  61. }
  62. });
  63. } else {
  64. self.choose_location();
  65. }
  66. },
  67. fail: res => {
  68. app.globalData.showToast("请先获取授权");
  69. }
  70. });
  71. // #endif
  72. // #ifdef MP-ALIPAY || H5 || APP
  73. this.choose_location();
  74. // #endif
  75. // #ifdef MP-KUAISHOU
  76. app.globalData.showToast('不支持地理位置选择!');
  77. uni.navigateBack();
  78. // #endif
  79. },
  80. // 位置服务回调方法
  81. setting_callback_event(e) {
  82. if (e.detail.authSetting[this.auth]) {
  83. this.setData({
  84. is_show_open_setting: false
  85. });
  86. this.choose_location();
  87. }
  88. },
  89. // 打开位置服务
  90. choose_location() {
  91. uni.chooseLocation({
  92. success: res => {
  93. uni.setStorageSync(this.cache_key, res);
  94. setTimeout(function(){
  95. uni.navigateBack();
  96. }, 500);
  97. },
  98. fail: res => {
  99. // 取消则自动返回、则显示错误
  100. if(res.errMsg.indexOf('cancel') != -1) {
  101. uni.navigateBack();
  102. } else {
  103. this.setData({error_msg: res.errMsg});
  104. app.globalData.showToast(res.errMsg);
  105. }
  106. }
  107. });
  108. }
  109. }
  110. };
  111. </script>
  112. <style>
  113. @import './open-setting-location.css';
  114. </style>