location.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {
  2. CHANGE_LOCATION_INFO,
  3. CHANGE_LOACTION_DETAIL_INFO
  4. } from './type'
  5. import { T_SELECTED_ADDRESS } from '../../constant'
  6. import {
  7. getAdressDetailByLngLat,
  8. getLngLatByAddress
  9. } from '@/utils'
  10. export default {
  11. namespaced: true,
  12. state() {
  13. return {
  14. // TODO: 默认值改掉
  15. locationInfo: {
  16. city: '佛山市',
  17. province: '广东省',
  18. adcode: '440606', // 区,三级
  19. district: '顺德区',
  20. towncode: '440606004000',
  21. streetNumber: {
  22. number: '1号',
  23. location: '113.300463,22.801715', // 113.06092 , 22.89223
  24. direction: '东北',
  25. distance: '165.82',
  26. street: '碧水路'
  27. },
  28. country: '中国',
  29. township: '大良街道',
  30. businessAreas: [ [] ],
  31. building: { name: [], type: [] },
  32. neighborhood: { name: [], type: [] },
  33. citycode: '0757'
  34. },
  35. detailAddress: '', // 广东省佛山市顺德区大良街道碧水路顺德市民活动中心
  36. currentCity: uni.getStorageSync(T_SELECTED_ADDRESS)?.data?.town || '大良街道', // 社区项目地址,只显示四级,来自:定位,地址选择或请求返回
  37. currentShopCity: uni.getStorageSync(T_SELECTED_ADDRESS)?.data?.town || '大良街道', // 商圈项目地址,可一二三四级,默认四级,来自:定位,地址选择
  38. obtainLocationCount: 0
  39. }
  40. },
  41. mutations: {
  42. [CHANGE_LOCATION_INFO](state, location) {
  43. state.locationInfo = location.addressComponent
  44. typeof state.locationInfo.province === 'object' ? state.locationInfo.province = '' : ''
  45. typeof state.locationInfo.city === 'object' ? state.locationInfo.city = '' : ''
  46. typeof state.locationInfo.district === 'object' ? state.locationInfo.district = '' : ''
  47. typeof state.locationInfo.adcode === 'object' ? state.locationInfo.adcode = '' : ''
  48. typeof state.locationInfo.township === 'object' ? state.locationInfo.township = '' : ''
  49. typeof state.locationInfo.towncode === 'object' ? state.locationInfo.towncode = '' : ''
  50. state.detailAddress = typeof location.formatted_address === 'object' ? '' : location.formatted_address
  51. state.currentCity = typeof location.addressComponent.township === 'object' ? '' : location.addressComponent.township
  52. state.currentShopCity = typeof location.addressComponent.township === 'object' ? '' : location.addressComponent.township
  53. state.obtainLocationCount = state.obtainLocationCount + 1
  54. uni.setStorageSync(T_SELECTED_ADDRESS, {
  55. type: 'default',
  56. data: {
  57. city: state.locationInfo.city,
  58. distinguish: state.locationInfo.district,
  59. town: state.locationInfo.township
  60. }
  61. })
  62. },
  63. [CHANGE_LOACTION_DETAIL_INFO](state, { detailInfo, currentCity, currentShopCity }) { // 触发该方法来自:地址选择,上次选择或定位。
  64. state.locationInfo = {
  65. city: typeof detailInfo.city === 'object' ? '' : detailInfo.city,
  66. province: typeof detailInfo.province === 'object' ? '' : detailInfo.province,
  67. adcode: typeof detailInfo.adcode === 'object' ? '' : detailInfo.adcode,
  68. district: typeof detailInfo.district === 'object' ? '' : detailInfo.district,
  69. towncode: '',
  70. streetNumber: {
  71. number: '',
  72. location: detailInfo.location,
  73. direction: '',
  74. distance: '',
  75. street: ''
  76. },
  77. country: '中国',
  78. township: typeof detailInfo.township === 'object' ? '' : detailInfo.township,
  79. businessAreas: [ [] ],
  80. building: { name: [], type: [] },
  81. neighborhood: { name: [], type: [] },
  82. citycode: ''
  83. }
  84. state.detailAddress = typeof detailInfo.formatted_address === 'object' ? '' : detailInfo.formatted_address
  85. state.currentCity = currentCity || state.locationInfo.township
  86. state.currentShopCity = currentShopCity
  87. state.obtainLocationCount = state.obtainLocationCount + 1
  88. }
  89. },
  90. actions: {
  91. getCurrentLocation({ commit, dispatch }, onSuccess) {
  92. return new Promise(async (resolve, reject) => {
  93. try {
  94. // MapLoader(
  95. // (res) => {
  96. // getAdressDetailByLngLat(res.latitude, res.longitude)
  97. // .then((res) => {
  98. // if (res.status === "1") {
  99. // commit(CHANGE_LOCATION_INFO, res.regeocode);
  100. // const addressDetail = res.regeocode;
  101. // onSuccess &&
  102. // typeof onSuccess === "function" &&
  103. // onSuccess(
  104. // addressDetail.addressComponent.province +
  105. // addressDetail.addressComponent.city +
  106. // addressDetail.addressComponent.district +
  107. // addressDetail.addressComponent.township
  108. // );
  109. // }
  110. // })
  111. // .catch(() => {
  112. // // _this.address = '定位失败'
  113. // });
  114. // },
  115. // () => {
  116. // uni.getLocation({
  117. // type: "gcj02",
  118. // success(res) {
  119. // getAdressDetailByLngLat(res.latitude, res.longitude)
  120. // .then((res) => {
  121. // if (res.status === "1") {
  122. // commit(CHANGE_LOCATION_INFO, res.regeocode);
  123. // const addressDetail = res.regeocode;
  124. // onSuccess &&
  125. // typeof onSuccess === "function" &&
  126. // onSuccess(
  127. // addressDetail.addressComponent.province +
  128. // addressDetail.addressComponent.city +
  129. // addressDetail.addressComponent.district +
  130. // addressDetail.addressComponent.township
  131. // );
  132. // }
  133. // })
  134. // .catch(() => {
  135. // // _this.address = '定位失败'
  136. // });
  137. // },
  138. // });
  139. // }
  140. // );
  141. uni.getLocation({
  142. type: 'gcj02',
  143. // altitude: true,
  144. // geocode: false,
  145. // highAccuracyExpireTime: 4000,
  146. // timeout: 5,
  147. // cacheTimeout: 4000,
  148. // accuracy: 'high',
  149. // isHighAccuracy: true,
  150. success(res) {
  151. // console.log(res)
  152. getAdressDetailByLngLat(res.latitude, res.longitude)
  153. .then((res) => {
  154. // console.log(res);
  155. if (res.status === '1') {
  156. commit(CHANGE_LOCATION_INFO, res.regeocode)
  157. const addressDetail = res.regeocode
  158. onSuccess &&
  159. typeof onSuccess === 'function' &&
  160. onSuccess({
  161. detail:
  162. addressDetail.addressComponent.province +
  163. addressDetail.addressComponent.city +
  164. addressDetail.addressComponent.district +
  165. addressDetail.addressComponent.township,
  166. town: addressDetail.addressComponent.township
  167. })
  168. }
  169. })
  170. .catch((e) => {
  171. console.log('获取位置失败', e)
  172. reject(false)
  173. })
  174. },
  175. fail: (e) => {
  176. console.log('定位失败', e)
  177. reject(false)
  178. }
  179. // complete: (e) => {
  180. // console.log('bbbb', e)
  181. // }
  182. })
  183. } catch (error) {
  184. if (error === 'prompt') {
  185. uni.showToast({
  186. title: '为了精准为您服务,请授权访问您的位置',
  187. icon: 'none'
  188. })
  189. }
  190. reject(false)
  191. }
  192. })
  193. },
  194. async getDetailAddress({ commit, dispatch }, data) {
  195. const res = await getLngLatByAddress(data.city + data.distinguish + data.town)
  196. if (res.status == '1') {
  197. const detailInfo = res.geocodes[0]
  198. commit(CHANGE_LOACTION_DETAIL_INFO, {
  199. detailInfo,
  200. currentCity: data.town || '',
  201. currentShopCity: data.town || data.distinguish || data.city
  202. })
  203. }
  204. }
  205. }
  206. }