city.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <view class="city">
  3. <!-- 当前定位城市 -->
  4. <view class="city-title xs">当前定位城市</view>
  5. <view class="city-current bg-white flex row-between">
  6. <view>
  7. <u-icon name="map-fill" size="34"></u-icon>
  8. <text class="m-l-8 nr" v-if="!isLoading">{{ cityInfo.name || '城市' }}</text>
  9. <text class="m-l-8 nr" v-else>定位中...</text>
  10. </view>
  11. <view class="reselect nr flex" @click="reLocationFunc">
  12. <u-loading mode="flower" color="#528FFF" size="28" v-if="isLoading"></u-loading>
  13. <text class="m-l-8">重新定位</text>
  14. </view>
  15. </view>
  16. <!-- 历史浏览城市 -->
  17. <view class="city-title xs" v-if="historyList.length">历史浏览城市</view>
  18. <view class="city-list bg-white">
  19. <block v-for="(hisItem, hisIndex) in historyList" :key="hisIndex">
  20. <view class="city-list-item line-1 nr" @click="chooseCity(hisItem)">{{ hisItem.name }}</view>
  21. </block>
  22. </view>
  23. <!-- 热门城市 -->
  24. <view class="city-title xs">热门城市</view>
  25. <view class="city-list bg-white">
  26. <block v-for="(hotItem, hotIndex) in hotList" :key="hotIndex">
  27. <view class="city-list-item line-1 nr" @click="chooseCity(hotItem)">{{ hotItem.name }}</view>
  28. </block>
  29. </view>
  30. <!-- 普通城市列表 -->
  31. <view v-for="(cityItem, cityIndex) in cityList" :key="cityIndex">
  32. <view class="city-title anchor xs">{{ cityIndex }}</view>
  33. <view class="city-list bg-white">
  34. <block v-for="(cityItem2, cityIndex2) in cityItem" :key="cityIndex2">
  35. <view class="city-list-item line-1 nr" @click="chooseCity(cityItem2)">
  36. {{ cityItem2.name }}
  37. </view>
  38. </block>
  39. </view>
  40. </view>
  41. <!-- 侧边导航条 -->
  42. <view class="city-bar__sidebar" @touchstart.stop.prevent="onTouchMove" @touchmove.stop.prevent="onTouchMove"
  43. @touchend.stop.prevent="onTouchStop" @touchcancel.stop.prevent="onTouchStop">
  44. <view v-for="(barItem, barIndex) in labelList" :key="barIndex" class="city-bar__index"
  45. :style="{color: touchmoveIndex === barIndex ? '#528FFF' : ''}">
  46. {{ barItem }}
  47. </view>
  48. </view>
  49. <!-- 侧边弹窗 -->
  50. <view class="city-list-alert" v-if="touchmove && labelList[touchmoveIndex+'']">
  51. <text>{{labelList[touchmoveIndex]}}</text>
  52. </view>
  53. <loading-view v-if="isFirstLoading"></loading-view>
  54. </view>
  55. </template>
  56. <script>
  57. import { mapActions, mapMutations, mapGetters } from 'vuex'
  58. import { getCityLists } from "@/api/store.js"
  59. import { currentPage, toast } from "@/utils/tools.js"
  60. import cache from "@/utils/cache"
  61. export default {
  62. data() {
  63. return {
  64. isLoading: false,
  65. cityList: [],
  66. labelList: [],
  67. historyList: [],
  68. hotList: [
  69. { name: '北京市', gcj02_lat: "39.929986", gcj02_lng: "116.395645", id: 110100 },
  70. { name: '上海市', gcj02_lat: "31.249162", gcj02_lng: "121.487899", id: 310100 },
  71. { name: '广州市', gcj02_lat: "23.120049", gcj02_lng: "113.30765", id: 440100 },
  72. { name: '深圳市', gcj02_lat: "22.546054", gcj02_lng: "114.025974", id: 440300 },
  73. { name: '重庆市', gcj02_lat: "29.544606", gcj02_lng: "106.530635", id: 110100 },
  74. { name: '成都市', gcj02_lat: "30.679943", gcj02_lng: "104.067923", id: 510100 },
  75. { name: '杭州市', gcj02_lat: "30.259244", gcj02_lng: "120.219375", id: 110100 },
  76. { name: '苏州市', gcj02_lat: "31.317987", gcj02_lng: "120.619907", id: 320500 },
  77. { name: '武汉市', gcj02_lat: "30.581084", gcj02_lng: "114.3162", id: 420100 },
  78. { name: '沈阳市', gcj02_lat: "41.808645", gcj02_lng: "123.432791", id: 210100 }
  79. ],
  80. isFirstLoading: true,
  81. touchmove: false,
  82. touchmoveIndex: 0,
  83. }
  84. },
  85. onLoad() {
  86. this.getCityListsFunc()
  87. this.historyList = cache.get('HISTORY') || []
  88. },
  89. onPageScroll({ scrollTop }) {
  90. const anchor = this.anchor;
  91. const index = anchor.findIndex(item => item >= scrollTop);
  92. const isLessTop = index !== -1;
  93. if (isLessTop && !this.touchmmove) this.touchmoveIndex = index;
  94. },
  95. methods: {
  96. ...mapActions(['initLocationFunc']),
  97. ...mapMutations(['setCityInfo']),
  98. async reLocationFunc() {
  99. this.isLoading = true;
  100. await this.initLocationFunc()
  101. this.isLoading = false;
  102. },
  103. chooseCity(city) {
  104. try{
  105. this.setCityInfo(city);
  106. // 返回上一页
  107. this.$Router.back(1, {
  108. success: () => {
  109. const {
  110. onLoad,
  111. options
  112. } = currentPage()
  113. // 刷新上一个页面
  114. onLoad && onLoad({ ...options, refresh: true })
  115. }
  116. })
  117. console.log(city)
  118. const historyList = this.historyList;
  119. const result = historyList.filter(item => item.name == city.name);
  120. const isLessTop = result.length === 0;
  121. if ( isLessTop ) {
  122. historyList.unshift(city)
  123. cache.set('HISTORY', historyList)
  124. }
  125. }catch(e){
  126. console.log(e)
  127. toast({ title: '选择有误,请联系管理员' })
  128. //TODO handle the exception
  129. }
  130. },
  131. getCityListsFunc() {
  132. getCityLists()
  133. .then(res => {
  134. if (res.code == 1) {
  135. this.cityList = res.data;
  136. this.labelList = Object.keys(res.data ?? {});
  137. this.setRect()
  138. }
  139. this.isFirstLoading = false;
  140. })
  141. },
  142. onTouchMove(event) {
  143. const y = parseInt(event.changedTouches[0].clientY)
  144. const len = this.labelList.length;
  145. const itemHeight = parseInt(this.sidebar.height / len);
  146. let index = Math.floor((y - this.sidebar.top) / itemHeight);
  147. if (index < 0) {
  148. index = 0;
  149. } else if (index > len - 1) {
  150. index = len - 1;
  151. }
  152. if (this.touchmoveIndex != index) {
  153. this.touchmove = true;
  154. this.touchmoveIndex = index
  155. uni.pageScrollTo({
  156. duration: 0,
  157. scrollTop: (this.anchor[index])
  158. })
  159. }
  160. },
  161. onTouchStop() {
  162. this.touchmove = false;
  163. this.scrollToAnchorIndex = null;
  164. },
  165. async setRect() {
  166. await this.$nextTick()
  167. const sidebar = uni.createSelectorQuery().selectAll(".city-bar__sidebar");
  168. sidebar.boundingClientRect(res => {
  169. this.sidebar = {
  170. height: res[0].height,
  171. top: res[0].top
  172. }
  173. }).exec();
  174. const anchor = uni.createSelectorQuery().selectAll(".anchor");
  175. anchor.boundingClientRect(res => {
  176. res.top = parseInt(res.top)
  177. this.anchor = res.map(item => parseInt(item.top));
  178. }).exec();
  179. }
  180. },
  181. computed: {
  182. ...mapGetters(['cityInfo'])
  183. }
  184. }
  185. </script>
  186. <style lang="scss" scoped>
  187. .city {
  188. padding: 12rpx 0;
  189. &-title {
  190. color: #AAAAAA;
  191. padding: 12rpx 34rpx;
  192. }
  193. .city-current {
  194. color: #575757;
  195. padding: 0 36rpx;
  196. .reselect {
  197. color: #528FFF;
  198. padding: 26rpx 10rpx;
  199. }
  200. }
  201. &-list {
  202. color: #575757;
  203. padding: 0 40rpx;
  204. display: flex;
  205. flex-wrap: wrap;
  206. &-item {
  207. width: 25%;
  208. height: 96rpx;
  209. line-height: 96rpx;
  210. padding-left: 34rpx;
  211. }
  212. }
  213. &-bar__sidebar {
  214. position: fixed;
  215. top: 50%;
  216. right: 0;
  217. display: flex;
  218. flex-direction: column;
  219. text-align: center;
  220. transform: translateY(-50%);
  221. user-select: none;
  222. z-index: 99;
  223. }
  224. &-bar__index {
  225. font-weight: 500;
  226. padding: 8rpx 18rpx;
  227. font-size: 22rpx;
  228. line-height: 1
  229. }
  230. &-list-alert {
  231. position: fixed;
  232. width: 120rpx;
  233. height: 120rpx;
  234. right: 90rpx;
  235. top: 50%;
  236. margin-top: -60rpx;
  237. border-radius: 24rpx;
  238. font-size: 50rpx;
  239. color: #fff;
  240. background-color: rgba(0, 0, 0, 0.65);
  241. display: flex;
  242. justify-content: center;
  243. align-items: center;
  244. padding: 0;
  245. z-index: 9999999;
  246. text {
  247. line-height: 50rpx;
  248. }
  249. }
  250. }
  251. </style>