selectCity.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <view class="page">
  3. <scroll-view class="scrollList" scroll-y :scroll-into-view="scrollViewId" :style="{height:winHeight+'px'}">
  4. <view class="search-bar">
  5. <view class="search-bar-form">
  6. <view class="search-bar-box">
  7. <!-- <icon class="icon-search-in-box" type="search" size="16"></icon> -->
  8. <input confirm-type="search" class="search-bar-input" placeholder="输入城市名称或首字母查询"
  9. placeholder-class="phcolor" :value="inputVal" :focus="inputShowed" @input="inputTyping" />
  10. <view class="icon-clear" v-if="inputVal" @tap="clearInput">
  11. <!-- #ifdef APP-PLUS || MP -->
  12. <icon type="clear" :size="15"></icon>
  13. <!-- #endif -->
  14. <!-- #ifdef H5 -->
  15. <tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon>
  16. <!-- #endif -->
  17. </view>
  18. </view>
  19. <label class="search-bar-label" v-if="!inputShowed" @tap="showInput">
  20. <!-- <icon class="icon-search" type="search" size="16"></icon> -->
  21. <view class="search-bar-text">输入城市名称或首字母查询</view>
  22. </label>
  23. </view>
  24. </view>
  25. <view class="tui-list search-result" v-if="inputShowed">
  26. <view class="tui-list-cell" hover-class="tui-list-cell-hover" v-for="(item,index) in searchResult"
  27. :key="index" @tap="selectCity" :data-name="item" :hover-stay-time='150'>
  28. <view class="tui-list-cell-navigate">
  29. {{item}}
  30. </view>
  31. </view>
  32. </view>
  33. <view v-if="!inputVal">
  34. <view class="current-city">
  35. <view class="title">定位城市</view>
  36. <view class="city-name" @tap="selectCity" :data-name="cityName">
  37. <tui-icon name="position-fill" color="#5677fc" :size="18"></tui-icon>
  38. {{cityName}}
  39. </view>
  40. </view>
  41. <view class="hot-city">
  42. <view class="title">热门城市</view>
  43. <view class="city-names">
  44. <view class="city-name-item" v-for="(item,index) in hotCity" :key="index" hover-class="tap-city"
  45. :hover-stay-time="150" @tap="selectCity" :data-name="item">
  46. {{item}}
  47. </view>
  48. </view>
  49. </view>
  50. <view class="tui-list city-list">
  51. <block v-for="(list,index) in lists" :key="index" v-if="list.data[0]">
  52. <view class="tui-list-cell-divider" :id="index === 0 ? 'suoyin' : list.letter">
  53. {{list.letter}}
  54. </view>
  55. <view class="tui-list-cell" hover-class="tui-list-cell-hover" v-for="(item,index2) in list.data"
  56. :key="index2" @tap="selectCity" :data-name="item.cityName" :hover-stay-time='150'>
  57. <view class="tui-list-cell-navigate" :class="[list.data.length-1==index?'last':'']">
  58. {{item.cityName}}
  59. </view>
  60. </view>
  61. </block>
  62. </view>
  63. </view>
  64. </scroll-view>
  65. <view class="tui-indexed-list-bar" :style="{height:indexBarHeight+'px'}" @touchstart="touchStart"
  66. @touchmove.stop.prevent="touchMove" @touchend.stop="touchEnd" @touchcancel.stop="touchCancel"
  67. v-if="!inputVal">
  68. <text v-for="(items,index) in lists" :key="index" @tap="handleClick(index)" class="tui-indexed-list-text"
  69. :style="{height:indexBarItemHeight+'px'}">
  70. {{index==0?"索引":items.letter}}
  71. </text>
  72. </view>
  73. <view class="tui-indexed-list-alert" v-if="touchmove && lists[touchmoveIndex].letter">
  74. {{lists[touchmoveIndex].letter}}
  75. </view>
  76. </view>
  77. </template>
  78. <script>
  79. export default {
  80. data() {
  81. return {
  82. cityName: '',
  83. latitude: "",
  84. longitude: "",
  85. lists: [],
  86. touchmove: false, // 是否在索引表上滑动
  87. touchmoveIndex: -1,
  88. titleHeight: 0, // 索引二字距离窗口顶部的高度
  89. indexBarHeight: 0, // 索引表高度
  90. indexBarItemHeight: 0, // 索引表子项的高度
  91. scrollViewId: '', // scroll-view滚动到的子元素的id
  92. winHeight: 0,
  93. inputShowed: false, // 输入框是否显示
  94. inputVal: '', // 搜索框输入的内容
  95. hotCity: ['北京', '上海', '广州', '深圳', '杭州', '长沙', '武汉', '厦门', '西安', '昆明', '成都', '重庆'], // 热门城市
  96. searchResult: [], // 搜索城市的结果
  97. }
  98. },
  99. onLoad: function(options) {
  100. const that = this;
  101. const _this = this;
  102. wx.authorize({
  103. scope: 'scope.userFuzzyLocation',
  104. success: res => {
  105. //console.log(res)
  106. wx.getFuzzyLocation({
  107. type: 'wgs84',
  108. success (res) {
  109. //console.log(res);
  110. _this.$request.post('geocoder.locationscope', {
  111. latitude: res.latitude,
  112. longitude: res.longitude
  113. }).then(res => {
  114. _this.cityName = res.data.city;
  115. uni.setStorageSync('cityName', _this.cityName);
  116. });
  117. }
  118. });
  119. },
  120. fail: res => {
  121. //console.log('失败:', res);
  122. }
  123. });
  124. _this.$request.get('area.abc').then(res => {
  125. _this.lists = res.data;
  126. });
  127. setTimeout(() => {
  128. uni.getSystemInfo({
  129. success: function(res) {
  130. let winHeight = res.windowHeight
  131. let barHeight = winHeight - uni.upx2px(204);
  132. that.winHeight = winHeight;
  133. that.indexBarHeight = barHeight;
  134. that.indexBarItemHeight = barHeight / 25;
  135. that.titleHeight = uni.upx2px(132);
  136. }
  137. })
  138. }, 50)
  139. },
  140. methods: {
  141. showInput() {
  142. this.inputShowed = true
  143. },
  144. clearInput() {
  145. this.inputVal = "";
  146. this.inputShowed = false;
  147. this.searchResult = [];
  148. uni.hideKeyboard() //强行隐藏键盘
  149. },
  150. inputTyping(e) {
  151. this.inputVal = e.detail.value;
  152. this.searchCity()
  153. },
  154. // 搜索城市
  155. searchCity() {
  156. let result = []
  157. this.lists.forEach((item1, index1) => {
  158. item1.data.forEach((item2, index2) => {
  159. if (item2.keyword.indexOf(this.inputVal.toLocaleUpperCase()) !== -1) {
  160. result.push(item2.cityName)
  161. }
  162. })
  163. })
  164. this.searchResult = result
  165. },
  166. // 选择城市
  167. selectCity(e) {
  168. let cityName = e.currentTarget.dataset.name;
  169. //返回并刷新上一页面
  170. //uni.$emit('emit', cityName)
  171. uni.setStorageSync('cityName', cityName);
  172. uni.navigateBack({
  173. delta: 1
  174. })
  175. },
  176. touchStart(e) {
  177. this.touchmove = true
  178. let pageY = e.touches[0].pageY
  179. let index = Math.floor((pageY - this.titleHeight) / this.indexBarItemHeight)
  180. let item = this.lists[index === 0 ? 1 : index]
  181. if (item) {
  182. this.scrollViewId = item.letter;
  183. this.touchmoveIndex = index;
  184. }
  185. },
  186. touchMove(e) {
  187. let pageY = e.touches[0].pageY;
  188. let index = Math.floor((pageY - this.titleHeight) / this.indexBarItemHeight)
  189. let item = this.lists[index === 0 ? 1 : index]
  190. if (item) {
  191. this.scrollViewId = item.letter;
  192. this.touchmoveIndex = index
  193. }
  194. },
  195. touchEnd() {
  196. this.touchmove = false;
  197. this.touchmoveIndex = -1;
  198. },
  199. touchCancel() {
  200. this.touchmove = false;
  201. this.touchmoveIndex = -1;
  202. },
  203. handleClick(index) {
  204. if (index === undefined || this.touchmove) return;
  205. let item = this.lists[index]
  206. if (item) {
  207. this.scrollViewId = item.letter;
  208. this.touchmoveIndex = index;
  209. }
  210. },
  211. },
  212. /**
  213. * 页面相关事件处理函数--监听用户下拉动作
  214. */
  215. onPullDownRefresh: function() {
  216. setTimeout(() => {
  217. uni.stopPullDownRefresh()
  218. }, 200);
  219. },
  220. }
  221. </script>
  222. <style>
  223. page {
  224. height: 100%;
  225. overflow: hidden;
  226. }
  227. .page {
  228. height: 100%;
  229. overflow: hidden;
  230. }
  231. .scrollList {
  232. flex: 1;
  233. }
  234. .search-bar {
  235. display: flex;
  236. align-items: center;
  237. position: relative;
  238. padding: 27rpx 30rpx 35rpx;
  239. background-color: #fff;
  240. }
  241. .search-bar-form {
  242. flex: 1;
  243. position: relative;
  244. border-radius: 32rpx;
  245. background: #f2f5f7;
  246. }
  247. .search-bar-box {
  248. display: flex;
  249. align-items: center;
  250. position: relative;
  251. padding-left: 20rpx;
  252. padding-right: 20rpx;
  253. height: 64rpx;
  254. z-index: 1;
  255. }
  256. .search-bar-input {
  257. line-height: normal;
  258. width: 100%;
  259. padding-left: 20rpx;
  260. font-size: 30rpx;
  261. color: #333;
  262. }
  263. .phcolor {
  264. font-size: 30rpx;
  265. }
  266. .icon-clear {
  267. height: 38rpx;
  268. }
  269. .icon-clear .tui-icon-class {
  270. display: block
  271. }
  272. .search-bar-label {
  273. height: 64rpx;
  274. display: flex;
  275. justify-content: center;
  276. align-items: center;
  277. position: absolute;
  278. top: 0;
  279. right: 0;
  280. bottom: 0;
  281. left: 0;
  282. z-index: 2;
  283. border-radius: 32rpx;
  284. color: #ccc;
  285. background: #f2f5f7;
  286. }
  287. .icon-search {
  288. position: relative;
  289. height: 26rpx;
  290. margin-right: 20rpx;
  291. font-size: inherit;
  292. }
  293. .search-bar-text {
  294. font-size: 30rpx;
  295. line-height: 32rpx;
  296. }
  297. .cancel-btn {
  298. padding-left: 30rpx;
  299. }
  300. .search-result::before {
  301. display: none;
  302. }
  303. .search-result::after {
  304. display: none;
  305. }
  306. .tui-list-cell {
  307. display: flex;
  308. flex-direction: row;
  309. justify-content: space-between;
  310. align-items: center;
  311. width: 100%;
  312. }
  313. .tui-list-cell-hover {
  314. background-color: #eee !important;
  315. }
  316. .tui-list-cell-navigate {
  317. width: 100%;
  318. position: relative;
  319. padding: 30rpx 0 30rpx 30rpx;
  320. font-size: 28rpx;
  321. color: #333;
  322. }
  323. .tui-list-cell-navigate::after {
  324. content: '';
  325. position: absolute;
  326. border-bottom: 1rpx solid #eaeef1;
  327. -webkit-transform: scaleY(0.5);
  328. transform: scaleY(0.5);
  329. bottom: 0;
  330. right: 0;
  331. left: 30rpx;
  332. }
  333. .current-city {
  334. padding: 0 30rpx 30rpx;
  335. background: #fff;
  336. }
  337. .tui-icon-class {
  338. margin-right: 10rpx;
  339. }
  340. .current-city .title {
  341. font-size: 24rpx;
  342. line-height: 24rpx;
  343. color: #999;
  344. }
  345. .city-name {
  346. display: flex;
  347. align-items: center;
  348. margin-top: 17rpx;
  349. font-size: 30rpx;
  350. font-weight: bold;
  351. line-height: 30rpx;
  352. color: #333;
  353. }
  354. .hot-city .title {
  355. height: 48rpx !important;
  356. padding-left: 30rpx;
  357. font-size: 24rpx !important;
  358. line-height: 48rpx !important;
  359. color: #999;
  360. background: #f2f5f7 !important;
  361. }
  362. .city-names {
  363. display: flex;
  364. flex-wrap: wrap;
  365. justify-content: space-between;
  366. align-content: space-between;
  367. width: 630rpx;
  368. padding: 12rpx 90rpx 26rpx 30rpx;
  369. background: #fff;
  370. }
  371. .city-name-item {
  372. display: flex;
  373. justify-content: center;
  374. align-items: center;
  375. width: 140rpx;
  376. height: 56rpx;
  377. margin-top: 16rpx;
  378. /* border: solid 1rpx #ccc; */
  379. border-radius: 28rpx;
  380. font-size: 28rpx;
  381. color: #333;
  382. position: relative;
  383. }
  384. .city-name-item::before {
  385. content: "";
  386. position: absolute;
  387. width: 200%;
  388. height: 200%;
  389. -webkit-transform-origin: 0 0;
  390. transform-origin: 0 0;
  391. -webkit-transform: scale(0.5, 0.5);
  392. transform: scale(0.5, 0.5);
  393. -webkit-box-sizing: border-box;
  394. box-sizing: border-box;
  395. left: 0;
  396. top: 0;
  397. border-radius: 56rpx;
  398. border: 1px solid #ccc;
  399. }
  400. .tap-city {
  401. color: #fff;
  402. background: #5677fc;
  403. /* border: solid 1rpx #5677fc; */
  404. }
  405. .tui-list {
  406. background-color: #fff;
  407. position: relative;
  408. width: 100%;
  409. display: flex;
  410. flex-direction: column;
  411. padding-bottom: env(safe-area-inset-bottom);
  412. }
  413. .tui-list-cell-divider {
  414. height: 48rpx;
  415. padding-left: 30rpx;
  416. font-size: 24rpx;
  417. color: #999;
  418. background: #f2f5f7;
  419. padding: 0 30rpx;
  420. display: flex;
  421. align-items: center;
  422. }
  423. .tui-indexed-list-bar {
  424. display: flex;
  425. flex-direction: column;
  426. align-items: center;
  427. justify-content: flex-start;
  428. z-index: 9999;
  429. position: absolute;
  430. top: 132rpx;
  431. right: 0;
  432. padding-right: 10rpx;
  433. width: 44rpx;
  434. }
  435. .tui-indexed-list-text {
  436. font-size: 22rpx;
  437. white-space: nowrap;
  438. }
  439. .tui-indexed-list-bar.active {
  440. background-color: rgb(200, 200, 200);
  441. }
  442. .tui-indexed-list-alert {
  443. position: absolute;
  444. z-index: 20;
  445. width: 160rpx;
  446. height: 160rpx;
  447. left: 50%;
  448. top: 50%;
  449. margin-left: -80rpx;
  450. margin-top: -80rpx;
  451. border-radius: 80rpx;
  452. text-align: center;
  453. line-height: 160rpx;
  454. font-size: 70rpx;
  455. color: #fff;
  456. background-color: rgba(0, 0, 0, 0.5);
  457. }
  458. </style>