location.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import { getGeocoder } from '@/api/user.js';
  11. import { getnearbyStore } from '@/api/new_store.js';
  12. import { mapMutations } from 'vuex';
  13. export default {
  14. data() {
  15. return {
  16. location: {},
  17. addressInfo:'选择地址',
  18. addressInfos:"选择地址",
  19. storeName: '',
  20. storeId:0
  21. };
  22. },
  23. watch: {
  24. location(value) {
  25. this.getnearbyStore(value);
  26. }
  27. },
  28. mounted(){
  29. uni.$on('activeFn', data => {
  30. if(data){
  31. this.storeName = data.name;
  32. this.storeId = data.id;
  33. this.SET_NEARBY(data.id);
  34. }
  35. });
  36. },
  37. methods: {
  38. ...mapMutations(['SET_NEARBY']),
  39. selfLocation() {
  40. let self = this
  41. // #ifdef MP || APP-PLUS
  42. uni.getLocation({
  43. type: 'gcj02',
  44. success: (res) => {
  45. try {
  46. uni.setStorageSync('user_latitude', res.latitude);
  47. uni.setStorageSync('user_longitude', res.longitude);
  48. self.getGeocoderCity(res.latitude,res.longitude);
  49. self.location = { latitude: res.latitude, longitude: res.longitude };
  50. } catch {}
  51. },
  52. fail:(res)=>{
  53. // #ifdef MP
  54. uni.getSetting({
  55. success: res=>{
  56. if(typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']){
  57. uni.setStorageSync('refuseLocation', true);
  58. }
  59. }
  60. })
  61. // #endif
  62. }
  63. });
  64. // #endif
  65. // #ifdef H5
  66. if (this.$wechat.isWeixin()) {
  67. this.$wechat.location().then(res => {
  68. uni.setStorageSync('user_latitude', res.latitude);
  69. uni.setStorageSync('user_longitude', res.longitude);
  70. self.getGeocoderCity(res.latitude,res.longitude);
  71. })
  72. } else {
  73. uni.getLocation({
  74. type: 'gcj02',
  75. success: function(res) {
  76. try {
  77. uni.setStorageSync('user_latitude', res.latitude);
  78. uni.setStorageSync('user_longitude', res.longitude);
  79. self.getGeocoderCity(res.latitude,res.longitude);
  80. } catch {}
  81. }
  82. });
  83. }
  84. // #endif
  85. },
  86. getGeocoderCity(latitude,longitude){
  87. getGeocoder({
  88. lat: latitude,
  89. long: longitude
  90. }).then(res=>{
  91. let address = res.data.address_component;
  92. this.addressInfo = address?address.city.slice(0,4) : '选择地址';
  93. this.addressInfos = address?(address.city+address.district).slice(0,7) : '选择地址';
  94. })
  95. },
  96. chooseLocation: function() {
  97. let that = this;
  98. if(that.fixConfig == 0){
  99. uni.navigateTo({
  100. url:'/pages/store/list/index?type=1&isCollage=1&storeId='+this.storeId
  101. })
  102. }else{
  103. uni.chooseLocation({
  104. success: (res) => {
  105. let address = that.$util.addressInfo(res.address);
  106. this.addressInfo = address?address.city.slice(0,4) : '选择地址';
  107. this.addressInfos = address?(address.city+address.district).slice(0,7) : '选择地址';
  108. that.location = { latitude: res.latitude, longitude: res.longitude };
  109. },
  110. fail: (err)=>{
  111. console.log(err)
  112. }
  113. })
  114. }
  115. },
  116. getnearbyStore(data) {
  117. getnearbyStore(data).then(res => {
  118. this.storeId = res.data.info.id;
  119. this.SET_NEARBY(res.data.info.id);
  120. this.storeName = res.data.info.name;
  121. });
  122. }
  123. }
  124. };