changeCity.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view class="content">
  3. <u-index-list @select="selectList">
  4. <view class="top">
  5. <u-search placeholder="请输入城市名" v-model="searchCity" :showAction="false" @change="search"></u-search>
  6. <view class="result" v-show="show">
  7. <view @click="selectCity(result.name)">{{result.name}}</view>
  8. </view>
  9. </view>
  10. <view class="title">
  11. <view class="title_text">定位城市</view>
  12. <view class=" hflex acenter">
  13. <view class="hflex acenter jcenter city_box " :class="select ? 'active' : ''" @click="selectCity(city)">
  14. <u-icon name="map-fill" color="#fff" size="14" v-if="select"></u-icon>
  15. <u-icon name="map-fill" color="#506DFF" size="14" v-else></u-icon>
  16. <view style="margin-left: 8rpx;">{{city}}</view>
  17. </view>
  18. <view class="hflex acenter jcenter city_box" :class="select ? '' : 'active'" @click="selectCity('全国')">
  19. <!-- <u-icon name="map-fill" color="#fff" size="14"></u-icon> -->
  20. <view style="margin-left: 8rpx;">全国</view>
  21. </view>
  22. </view>
  23. </view>
  24. <view class="title vflex fwrap" v-if="history.length > 0">
  25. <view class="title_text">历史</view>
  26. <view class="hflex acenter fwrap">
  27. <block v-for="(item2,index) in history" :key="index">
  28. <view class="city_box hflex acenter jcenter"@click="selectCity(item2)">{{item2}}</view>
  29. </block>
  30. </view>
  31. </view>
  32. <view class="title vflex fwrap">
  33. <view class="title_text">
  34. 热门
  35. </view>
  36. <view class="hflex acenter fwrap">
  37. <block v-for="(item2,index) in hotList" :key="index">
  38. <view class="city_box hflex acenter jcenter"@click="selectCity(item2.name)">{{item2.name}}</view>
  39. </block>
  40. </view>
  41. </view>
  42. <template v-for="(item,index) in itemArr">
  43. <u-index-item v-if="item.child.length > 0">
  44. <u-index-anchor :text="indexList[index]"></u-index-anchor>
  45. <view class="list-cell" v-for="(cell, index2) in item.child" :key="index2">
  46. <view @click="selectCity(cell.title)">{{cell.title}}</view>
  47. </view>
  48. </u-index-item>
  49. </template>
  50. </u-index-list>
  51. </view>
  52. </template>
  53. <script>
  54. import $api from '@/static/js/api.js'
  55. const app = getApp().globalData
  56. var that = ''
  57. export default {
  58. data() {
  59. return {
  60. city: '',
  61. searchCity: '',
  62. history: [],
  63. hotList: [],
  64. indexList: ["A", "B", "C","D","E","F","G","H","I","J","K","L","M","N","O","P",'Q','R','S','T',"U","V",'W','X','Y','Z'],
  65. itemArr: [],
  66. show: false,
  67. result: {},
  68. select: true,
  69. }
  70. },
  71. onLoad(options) {
  72. that = this
  73. that.city = uni.getStorageSync('locationCity')
  74. if(uni.getStorageSync('city') == '全国') {
  75. that.select = false
  76. } else {
  77. that.select = true
  78. }
  79. that.history = uni.getStorageSync('historyCity')
  80. if(that.history == '') {
  81. that.history = []
  82. }
  83. if(!uni.getStorageSync('locationCity')) {
  84. this.getLocation()
  85. }
  86. this.getCity()
  87. },
  88. onPageScroll(e) {
  89. console.log(e);
  90. // this.scrollTop = e.scrollTop;
  91. },
  92. methods: {
  93. getLocation() {
  94. return new Promise((resolve, reject) => {
  95. app.getLoca().then(res=> {
  96. console.log('getLoaction',res);
  97. that.city = res[0].regeocodeData.addressComponent.city
  98. uni.setStorageSync('city',that.city)
  99. uni.setStorageSync('locationCity',that.city)
  100. })
  101. })
  102. },
  103. // 获取城市信息
  104. getCity() {
  105. $api.req({
  106. url: '/data/api.Area/sort',
  107. data: {
  108. level: 2,
  109. hot: 1,
  110. name: that.searchCity
  111. }
  112. }, function(res) {
  113. if(res.code == 1) {
  114. that.hotList = res.data.hot
  115. that.itemArr = res.data.areas
  116. }
  117. })
  118. },
  119. // 搜索城市
  120. search(e) {
  121. that.searchCity = e
  122. that.getCity()
  123. /* if(e == "") {
  124. that.show = false
  125. } else {
  126. that.show = true
  127. } */
  128. /* $api.req({
  129. url: '/data/api.Area/name',
  130. data: {
  131. name: that.searchCity
  132. }
  133. }, function(res) {
  134. if(res.code == 1) {
  135. if(res.data != null) {
  136. that.result = res.data
  137. // that.selectCity(res.data.name)
  138. } else {
  139. $api.info(res.info)
  140. }
  141. }
  142. }) */
  143. },
  144. // 选择索引
  145. selectList(e) {
  146. console.log("select",e);
  147. },
  148. // 选择城市
  149. selectCity(city) {
  150. console.log("City",city);
  151. var is_some = 0
  152. if (that.history.length > 0) {
  153. for(var i=0;i<that.history.length;i++) {
  154. if(that.history[i] == city) {
  155. is_some = 1;
  156. }
  157. }
  158. if(is_some == 0) {
  159. that.history.unshift(city)
  160. }
  161. } else {
  162. that.history.unshift(city)
  163. }
  164. if(that.history.length > 9) {
  165. that.history.pop()
  166. }
  167. uni.setStorageSync("historyCity",that.history)
  168. // that.search(city)
  169. var pages = getCurrentPages();// 获取所有的页面栈
  170. var prevPage = pages[pages.length - 2];
  171. prevPage.$vm.city = city;
  172. $api.jump(-1)
  173. uni.setStorageSync('city',city)
  174. },
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped>
  179. .content::v-deep {
  180. // padding: 0 30rpx;
  181. background-color: #f5f5f5;
  182. .top {
  183. width: 100%;
  184. background: #fff;
  185. padding: 36rpx 40rpx 0 30rpx;
  186. box-sizing: border-box;
  187. position: relative;
  188. .u-search {
  189. padding-bottom: 30rpx !important;
  190. }
  191. // margin-top: ;
  192. .result {
  193. position: absolute;
  194. left: 0;
  195. bottom: -181rpx;
  196. // border: 1rpx solid #ddd;
  197. width: 100%;
  198. min-height: 200rpx;
  199. margin: 0 auto;
  200. box-sizing: border-box;
  201. padding: 40rpx;
  202. background-color: #ffffff;
  203. z-index: 999;
  204. box-shadow: 0px 9px 8px 0px rgba(215,215,215,0.5);
  205. }
  206. }
  207. .title {
  208. width: 100%;
  209. background: #fff;
  210. padding: 32rpx 30rpx 32rpx;
  211. box-sizing: border-box;
  212. margin-top: 20rpx;
  213. font-size: 28rpx;
  214. color: #222;
  215. .title_text {
  216. margin-bottom: 20rpx;
  217. }
  218. .city_box {
  219. margin: 0 16rpx 20rpx 0;
  220. width: 212rpx;
  221. height: 72rpx;
  222. background: #f5f5f5;
  223. border-radius: 5rpx;
  224. font-size: 28rpx;
  225. color: #555;
  226. text-align: center;
  227. }
  228. .active {
  229. background: #506DFF;
  230. font-size: 28rpx;
  231. color: #fff
  232. }
  233. }
  234. .u-index-list {
  235. width: 100%;
  236. box-sizing: border-box;
  237. // padding: 32rpx 0;
  238. // background-color: #fff;
  239. }
  240. .u-input {
  241. background: #f5f5f5 !important;
  242. }
  243. .u-index-item {
  244. width: 100%;
  245. background: #fff;
  246. margin: 10rpx 0;
  247. padding: 32rpx 30rpx 0 0;
  248. .u-index-anchor {
  249. border: none !important;
  250. background-color: #fff !important;
  251. }
  252. }
  253. // .list-cell {
  254. // font-size: 30rpx;
  255. // color: #222;
  256. // padding-bottom: 32rpx;
  257. // }
  258. .list-cell {
  259. display: flex;
  260. box-sizing: border-box;
  261. width: 100%;
  262. padding: 10px 24rpx;
  263. overflow: hidden;
  264. color: #222;
  265. font-size: 30rpx;
  266. line-height: 24px;
  267. background-color: #fff;
  268. }
  269. }
  270. </style>