locationList.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view>
  3. <uni-search-bar v-model="keyword" @input="input">
  4. </uni-search-bar>
  5. <template v-if="!showSearchResults">
  6. <uni-notice-bar show-get-more more-text="完善 >" :text="addressesName" @getmore="getMore" />
  7. <uni-section title="我的收货地址">
  8. <template v-slot:right>
  9. <span style="color: red;" @click="addAddress">新增地址</span>
  10. </template>
  11. <uni-list :border="false">
  12. <uni-list-item v-for="item in myAddresses" :key="item.id">
  13. <template v-slot:header>
  14. <view @click="chooseLocation(item)">
  15. <view>
  16. <uni-tag text="默认" type="warning" v-if="item.defaultAddress===1" />
  17. <text style="margin-left: 10rpx;">{{item.address}}</text>
  18. </view>
  19. <view style="margin-top: 20rpx;font-size: 28rpx;color:#555">
  20. <text>{{item.consignee}}</text>
  21. <text style="margin-left: 10rpx;">{{item.phone}}</text>
  22. </view>
  23. </view>
  24. </template>
  25. <template v-slot:footer>
  26. <uni-icons type="paperplane-filled" v-if="item.latitude===lat&&item.longitude==lng"></uni-icons>
  27. <uni-icons type="gear" @click="editAddress(item)" style="margin-left: 18rpx"></uni-icons>
  28. </template>
  29. </uni-list-item>
  30. </uni-list>
  31. </uni-section>
  32. <uni-section title="附近地址">
  33. <uni-list :border="false">
  34. <uni-list-item v-for="item in aroundAddresses" :key="item.id">
  35. <template v-slot:header>
  36. <view @click="chooseLocation(item)" style="max-width: 70%;">
  37. <uni-icons type="paperplane-filled" v-if="item.location===location"></uni-icons>
  38. <text style="margin-left: 10rpx;">{{item.name}}</text>
  39. </view>
  40. </template>
  41. <template v-slot:footer>
  42. <span style="color: red;" v-if="item.location===location" @click="pickLoaction"><uni-icons
  43. type="location"></uni-icons>重新定位</span>
  44. </template>
  45. </uni-list-item>
  46. </uni-list>
  47. </uni-section>
  48. </template>
  49. <template v-else>
  50. <uni-list :border="false">
  51. <uni-list-item v-for="item in searchResults" :key="item.id">
  52. <template v-slot:header>
  53. <view @click="chooseLocation(item)" style="max-width: 70%;">
  54. <text style="margin-left: 10rpx;">{{item.name}}</text>
  55. </view>
  56. </template>
  57. </uni-list-item>
  58. </uni-list>
  59. </template>
  60. </view>
  61. </template>
  62. <script>
  63. import uniSection from '../../components/uni-section/uni-section'
  64. let getPlaceTimer = null
  65. export default {
  66. components: {
  67. uniSection
  68. },
  69. data() {
  70. return {
  71. keyword: '',
  72. lat: null,
  73. lng: null,
  74. myAddresses: [],
  75. aroundAddresses: [],
  76. addressesName: null,
  77. searchResults: []
  78. }
  79. },
  80. computed: {
  81. location() {
  82. return this.lng + ',' + this.lat
  83. },
  84. showSearchResults() {
  85. return !!this.keyword
  86. }
  87. },
  88. onShow() {
  89. const {
  90. lat,
  91. lng,
  92. addressesName
  93. } = uni.getStorageSync('addresses')
  94. this.addressesName = addressesName
  95. this.lat = lat
  96. this.lng = lng
  97. this.getMyAddresses()
  98. this.getAroundAddresses()
  99. },
  100. methods: {
  101. getMore() {
  102. const activeItem = this.myAddresses.find(item =>
  103. item.latitude === this.lat && item.longitude === this.lng
  104. );
  105. const {
  106. lat,
  107. lng,
  108. addressesName
  109. } = uni.getStorageSync('addresses')
  110. const item = {
  111. address: addressesName,
  112. latitude: lat,
  113. longitude: lng,
  114. province: activeItem ? activeItem.province : '',
  115. city: activeItem ? activeItem.city : '',
  116. county: activeItem ? activeItem.county : '',
  117. phone: activeItem ? activeItem.phone : undefined,
  118. consignee: activeItem ? activeItem.consignee : undefined,
  119. defaultAddress: activeItem ? activeItem.defaultAddress : undefined,
  120. id: activeItem ? activeItem.id : undefined // 如果没有匹配项,id 为 undefined
  121. }
  122. if(activeItem){
  123. uni.navigateTo({
  124. url: '/pages/address/create?type=edit&data=' + JSON.stringify(item)
  125. })
  126. }else {
  127. uni.navigateTo({
  128. url: '/pages/address/create?type=add&data=' + JSON.stringify(item)
  129. })
  130. }
  131. },
  132. addAddress() {
  133. uni.navigateTo({
  134. url: '/pages/address/create?type=add'
  135. })
  136. },
  137. editAddress(item) {
  138. uni.navigateTo({
  139. url: '/pages/address/create?type=edit&data=' + JSON.stringify(item)
  140. })
  141. },
  142. getMyAddresses() {
  143. this.$api.request('get', 'address/app/getAllAddress').then(res => {
  144. this.myAddresses = res.data
  145. })
  146. },
  147. getAroundAddresses() {
  148. var that = this;
  149. this.$api.request('get', 'https://restapi.amap.com/v5/place/around', {
  150. key: that.$api.defConfig().mapKey,
  151. location: this.location
  152. }).then(res => {
  153. this.aroundAddresses = res.pois
  154. })
  155. },
  156. getRecentlyStorage(lat, lng, name) {
  157. this.$api.request('get', 'storage/position/getRecentlyStorage', {
  158. lng,
  159. lat
  160. }).then(res => {
  161. // 设置距离值
  162. let addressesInfo = {};
  163. addressesInfo.lng = lng;
  164. addressesInfo.lat = lat;
  165. addressesInfo.distance = res.data.distance;
  166. addressesInfo.addressesName = name
  167. uni.setStorageSync('addresses', addressesInfo);
  168. this.$store.commit('setStorage', res.data.id)
  169. this.$store.commit('setStorageObj', {
  170. businessStartTime: res.data.businessStartTime,
  171. businessState: res.data.businessState,
  172. businessStopTime: res.data.businessStopTime,
  173. deliveryStartTime: res.data.deliveryStartTime,
  174. deliveryStopTime: res.data.deliveryStopTime,
  175. haveStorage: res.data.haveStorage,
  176. id: res.data.id
  177. })
  178. uni.switchTab({
  179. url: '/pages/index/index'
  180. })
  181. })
  182. },
  183. chooseLocation({
  184. latitude,
  185. longitude,
  186. address,
  187. location,
  188. name
  189. }) {
  190. if (location) {
  191. const _ = location.split(',');
  192. latitude = _[1]
  193. longitude = _[0]
  194. address = name
  195. }
  196. this.getRecentlyStorage(latitude, longitude, address)
  197. },
  198. pickLoaction() {
  199. const that = this
  200. uni.chooseLocation({
  201. success({
  202. errMsg,
  203. latitude,
  204. longitude,
  205. name
  206. }) {
  207. // address: "福建省福州市闽侯县甘蔗交通路2号"
  208. // errMsg: "chooseLocation:ok"
  209. // latitude: 26.150201
  210. // longitude: 119.13138
  211. // name: "中共闽侯县委员会"
  212. if (errMsg === "chooseLocation:ok") {
  213. that.getRecentlyStorage(latitude, longitude, name)
  214. }
  215. }
  216. })
  217. },
  218. getPlace() {
  219. if (getPlaceTimer) {
  220. clearTimeout(getPlaceTimer)
  221. getPlaceTimer = null
  222. }
  223. getPlaceTimer = setTimeout(() => {
  224. var that = this;
  225. this.$api.request('get', 'https://restapi.amap.com/v5/place/text', {
  226. key: that.$api.defConfig().mapKey,
  227. keywords: this.keyword
  228. }).then(res => {
  229. this.searchResults = res.pois
  230. })
  231. }, 500)
  232. },
  233. input() {
  234. this.getPlace()
  235. }
  236. }
  237. }
  238. </script>
  239. <style lang="scss" scoped>
  240. ::v-deep .uni-list-item__container {
  241. justify-content: space-between;
  242. align-items: center;
  243. }
  244. </style>