submitlLocation.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="task-form-item form-item-media">
  3. <view class="form-lable">
  4. <text><text class="form-must" v-if="rule && rule.required">* </text>定位:</text>
  5. <view :class="['form-lable-content' , Position.length > 0 ? 'lable-content-right' : '']" v-if="!checkPage">
  6. <view class="lable-content-btn" @click.stop="onLocation()">
  7. <text class="iconfont_yige">&#xe6a8;</text>
  8. <text>获取当前位置</text>
  9. </view>
  10. </view>
  11. </view>
  12. <view class="form-img-video form-value">
  13. {{Position}}
  14. </view>
  15. <mumu-getlocation ref='muLocation'></mumu-getlocation>
  16. </view>
  17. </template>
  18. <script>
  19. import {
  20. getLocation
  21. } from "@/utils/tool.js"
  22. import mumuGetlocation from '@/uni_modules/mumu-getlocation/components/mumu-getlocation/mumu-getlocation.vue'
  23. export default {
  24. name: 'submintContent',
  25. components: {
  26. mumuGetlocation
  27. },
  28. model: {
  29. prop: ['lbsPosition'],
  30. event: ['onChange']
  31. },
  32. props: {
  33. checkPage: {
  34. type: Boolean,
  35. default: false
  36. },
  37. rule: {
  38. type: Object,
  39. default: null
  40. },
  41. lbsPosition: {
  42. type: String,
  43. default: ''
  44. }
  45. },
  46. data() {
  47. return {
  48. Position: '',
  49. locationRef: null
  50. }
  51. },
  52. methods: {
  53. onLocation() {
  54. try {
  55. // #ifdef APP
  56. getLocation().then(res => {
  57. console.log('getLocation = ', res)
  58. const {
  59. address
  60. } = res || {};
  61. if (address) {
  62. this.Position =
  63. `${address.province || ''}${address.city || ''}${address.district || ''}${address.street || ''}${address.poiName || ''}${address.streetNum || ''}`
  64. }
  65. }).catch(err => {
  66. console.log('getLocation err= ', err)
  67. })
  68. // #endif
  69. // #ifdef H5
  70. uni.showLoading({
  71. title: '获取定位中...',
  72. mask: true
  73. })
  74. // 初始化组件,并且检测是否开启与授权定位
  75. this.$refs.muLocation.__init().then(location => {
  76. // 用户授权了和开启了定位,把定位对象保存到 data 中
  77. // this.locationRef = location
  78. // 调用 getLocation 方法获取精准定位
  79. location.getLocation().then(res => {
  80. console.log('location', res);
  81. try {
  82. const { province, city, district, addr } = res || {}
  83. this.Position = `${province}${city}${district}${addr}`;
  84. } catch (e) {
  85. //TODO handle the exception
  86. }
  87. // res 就是返回的数据
  88. // uni.showModal({
  89. // content: JSON.stringify(res, null, 4)
  90. // })
  91. })
  92. uni.hideLoading()
  93. }, err => {
  94. // 用户拒绝了定位请求,获取系统没有开启定位功能
  95. uni.hideLoading()
  96. if (err.code === 1) {
  97. uni.showModal({
  98. title: '获取定位权限失败',
  99. content: '你拒绝了位置授权服务。请允许当前页面获取定位授权,后刷新页面。'
  100. })
  101. } else {
  102. uni.showModal({
  103. title: '获取定位权限失败',
  104. content: '请确定手机定位已打开,并且当前浏览器允许获取定位,都开启后请刷新页面。'
  105. })
  106. }
  107. })
  108. // #endif
  109. } catch (e) {
  110. console.log('showToast = ', e)
  111. uni.showToast({
  112. title: '定位失败',
  113. icon: 'none'
  114. })
  115. //TODO handle the exception
  116. }
  117. }
  118. },
  119. watch: {
  120. lbsPosition: {
  121. handler(newL, oldL) {
  122. this.Position = newL
  123. },
  124. immediate: true
  125. },
  126. Position: {
  127. handler(newL, oldL) {
  128. if (this.Position !== this.lbsPosition) {
  129. this.$emit('onChange', this.Position)
  130. }
  131. },
  132. }
  133. }
  134. }
  135. </script>
  136. <style lang="scss" scoped>
  137. @import '~./submint.scss';
  138. </style>