location.js 6.4 KB

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