citySelect.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <template>
  2. <view>
  3. <!-- pages/jsCase/citySel/index.wxml -->
  4. <view :class="'head ' + (search && 'r_head')">
  5. <!-- 搜索框 -->
  6. <view class="flex-row search-box">
  7. <view class="head_input">
  8. <image src="https://mall.zhaijieshi.cc/file/wx-dy/icon-search.png" class="search_icon"></image>
  9. <input type="text" placeholder="搜索" placeholder-class="place_holder" @input="getValue" :value="search" />
  10. </view>
  11. <view class="sha_icon" @tap.stop.prevent="clear_input">取消</view>
  12. </view>
  13. <view class="flex-row head_curr" v-if="!search">
  14. <image src="https://mall.zhaijieshi.cc/file/wx-dy/address-map.png" class="h_c_icon" />
  15. <view>当前定位城市: {{ current_city }}</view>
  16. </view>
  17. </view>
  18. <scroll-view v-if="!search" :scroll-y="true" class="sy_container" :scroll-into-view="scrollViewId">
  19. <view class="hot_city">
  20. <view class="title">热门城市</view>
  21. <view class="flex-row flex-wrap box">
  22. <block v-for="(item, index) in hot_city" :key="index">
  23. <view class="name" hover-class="sel_city" hover-stay-time="150">{{ item.name }}</view>
  24. </block>
  25. </view>
  26. </view>
  27. <view class="all_city">
  28. <view v-if="item.data.length > 0" v-for="(item, index) in city_list" :key="index">
  29. <view class="letter_name" :id="item.letter">{{ item.letter }}</view>
  30. <view class="city">
  31. <block v-for="(item0, index0) in item.data" :key="index0">
  32. <view class="name flex-row" hover-class="city_hover" hover-stay-time="150">{{ item0.cityName }}</view>
  33. </block>
  34. </view>
  35. </view>
  36. </view>
  37. </scroll-view>
  38. <!-- 侧边选择索引 -->
  39. <view v-if="!search">
  40. <view
  41. class="fixed_bar"
  42. :style="'height: ' + barHeight + 'px;'"
  43. @touchstart.stop.prevent="touchStart"
  44. @touchmove.stop.prevent="touchMove"
  45. @touchend.stop.prevent="touchEnd"
  46. @touchcancel.stop.prevent="touchCancel"
  47. >
  48. <view :style="'height: ' + barHeight / 22 + 'px;'" v-for="(item, index) in city_list" :key="index">
  49. <view
  50. :class="'bar_item flex-column j_c ' + (curr == index && 'bar_item_active')"
  51. :style="'width: ' + (barHeight / 22) * 0.75 + 'px;height: ' + (barHeight / 22) * 0.75 + 'px;'"
  52. >
  53. {{ item.letter }}
  54. </view>
  55. </view>
  56. </view>
  57. <view v-if="showLetter && city_list[curr].letter" class="fixed_letter">{{ city_list[curr].letter }}</view>
  58. </view>
  59. <view v-if="search" class="result_list">
  60. <view v-if="result.length > 0">
  61. <block v-for="(item, index) in result" :key="index">
  62. <view class="r_item" hover-stay-time="150" hover-class="r_item_hover">{{ item.name }}</view>
  63. </block>
  64. </view>
  65. <view v-else class="flex-column no_data">
  66. <image src="https://i.postimg.cc/7P00ckMG/image.png" />
  67. <view>请输入正确的城市名称呢</view>
  68. </view>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. // pages/jsCase/citySel/index.js
  74. const { cityData } = require('./city.js');
  75. var map = require('../../../utils/map.js');
  76. var app = getApp();
  77. export default {
  78. data() {
  79. return {
  80. current_city: '',
  81. search: '',
  82. hot_city: [
  83. {
  84. name: '北京'
  85. },
  86. {
  87. name: '上海'
  88. },
  89. {
  90. name: '广州'
  91. },
  92. {
  93. name: '深圳'
  94. },
  95. {
  96. name: '成都'
  97. },
  98. {
  99. name: '苏州'
  100. },
  101. {
  102. name: '杭州'
  103. },
  104. {
  105. name: '南京'
  106. }
  107. ],
  108. city_list: [],
  109. barHeight: 0,
  110. curr: -1,
  111. scrollViewId: '',
  112. barTop: 0,
  113. showLetter: true,
  114. //搜索结果
  115. result: [],
  116. addressName: '',
  117. letter: ''
  118. };
  119. },
  120. onLoad: function (options) {
  121. this.getUserLocation();
  122. this.setData({
  123. // current_city: options.currentCity || "南京",
  124. city_list: cityData
  125. });
  126. uni.getSystemInfo({
  127. success: (res) => {
  128. console.log(res);
  129. let winHeight = res.windowHeight;
  130. let barHeight = winHeight - (res.windowWidth / 750) * 300;
  131. this.setData({
  132. barHeight: barHeight,
  133. barTop: (res.windowWidth / 750) * 180
  134. });
  135. }
  136. });
  137. },
  138. methods: {
  139. getUserLocation: function () {
  140. map.getUserLocation();
  141. let that = this;
  142. setTimeout(function () {
  143. that.setData({
  144. addressName: app.globalData.currentLocationName,
  145. current_city: app.globalData.city
  146. });
  147. }, 1000);
  148. },
  149. /**
  150. * 获取value值
  151. * @param {*} e
  152. */
  153. getValue(e) {
  154. this.setData(
  155. {
  156. search: e.detail.value
  157. },
  158. () => {
  159. this.searchFun(e.detail.value);
  160. }
  161. );
  162. },
  163. /**
  164. * 搜索成功
  165. */
  166. searchFun(e) {
  167. let result = [];
  168. let { city_list } = this;
  169. city_list.forEach((item1) => {
  170. item1.data.forEach((item2) => {
  171. if (item2.keyword.indexOf(e.toLocaleUpperCase()) !== -1) {
  172. result.push({
  173. name: item2.cityName
  174. });
  175. }
  176. });
  177. });
  178. this.setData({
  179. result
  180. });
  181. },
  182. /**
  183. * 清空验证码
  184. */
  185. clear_input() {
  186. this.setData({
  187. search: ''
  188. });
  189. },
  190. touch(e) {
  191. let pageY = e.touches[0].pageY;
  192. let index = Math.floor((pageY - this.barTop) / (this.barHeight / 22)); //向下取整
  193. let item = this.city_list[index];
  194. if (item) {
  195. this.setData({
  196. scrollViewId: item.letter,
  197. curr: index
  198. });
  199. }
  200. },
  201. touchStart(e) {
  202. this.setData({
  203. showLetter: true
  204. });
  205. this.touch(e);
  206. },
  207. touchMove(e) {
  208. this.touch(e);
  209. },
  210. touchEnd() {
  211. this.setData({
  212. showLetter: false
  213. });
  214. },
  215. touchCancel() {
  216. this.setData({
  217. showLetter: false
  218. });
  219. }
  220. }
  221. };
  222. </script>
  223. <style>
  224. /* pages/jsCase/citySel/index.wxss */
  225. page {
  226. height: 100%;
  227. background: #f2f5f7;
  228. }
  229. .head {
  230. position: fixed;
  231. top: 0;
  232. left: 0;
  233. width: 100%;
  234. height: 170rpx;
  235. box-sizing: border-box;
  236. padding: 20rpx 20rpx 0;
  237. background: #fff;
  238. z-index: 9999;
  239. }
  240. .search-box {
  241. display: flex;
  242. align-items: center;
  243. }
  244. .r_head {
  245. height: 90rpx;
  246. }
  247. .head_input {
  248. /* position: relative; */
  249. flex: 1;
  250. display: flex;
  251. align-items: center;
  252. height: 60rpx;
  253. padding-left: 16rpx;
  254. border-radius: 30rpx;
  255. background: #f2f5f7;
  256. }
  257. .search_icon {
  258. /* position : absolute;
  259. top : 50%;
  260. left : 0;
  261. margin-top : -15rpx; */
  262. width: 30rpx;
  263. height: 30rpx;
  264. margin-right: 12rpx;
  265. }
  266. .head input {
  267. height: 60rpx;
  268. /* padding-left : 75rpx; */
  269. font-size: 28rpx;
  270. }
  271. .place_holder {
  272. font-size: 28rpx;
  273. color: #999999;
  274. }
  275. .sha_icon {
  276. margin-left: 18rpx;
  277. font-size: 28rpx;
  278. color: #333333;
  279. }
  280. .head_curr {
  281. height: 80rpx;
  282. display: flex;
  283. align-items: center;
  284. color: #333;
  285. font-size: 28rpx;
  286. /* padding-top: 20rpx; */
  287. box-sizing: border-box;
  288. }
  289. .h_c_icon {
  290. flex-shrink: 0;
  291. margin-right: 15rpx;
  292. width: 32rpx;
  293. height: 32rpx;
  294. margin-left: 12rpx;
  295. }
  296. .sy_container {
  297. box-sizing: border-box;
  298. width: 100%;
  299. height: 100%;
  300. padding-top: 170rpx;
  301. }
  302. .hot_city .title {
  303. padding: 10rpx 30rpx;
  304. color: #999;
  305. font-size: 26rpx;
  306. }
  307. .hot_city .box {
  308. background-color: #fff;
  309. padding: 5rpx 30rpx 15rpx 40rpx;
  310. box-sizing: border-box;
  311. }
  312. .hot_city .box .name {
  313. vertical-align: top;
  314. display: inline-block;
  315. min-width: 140rpx;
  316. line-height: 56rpx;
  317. height: 56rpx;
  318. border-radius: 28rpx;
  319. font-size: 28rpx;
  320. color: #333;
  321. text-align: center;
  322. padding: 0 20rpx;
  323. box-sizing: border-box;
  324. margin-top: 10rpx;
  325. margin-right: 20rpx;
  326. position: relative;
  327. }
  328. .hot_city .box .name::after {
  329. content: '';
  330. position: absolute;
  331. width: 200%;
  332. height: 200%;
  333. -webkit-transform-origin: 0 0;
  334. transform-origin: 0 0;
  335. -webkit-transform: scale(0.5, 0.5);
  336. transform: scale(0.5, 0.5);
  337. -webkit-box-sizing: border-box;
  338. box-sizing: border-box;
  339. left: 0;
  340. top: 0;
  341. border-radius: 56rpx;
  342. border: 1rpx solid rgb(235, 225, 225);
  343. }
  344. .sel_city {
  345. color: #fff !important;
  346. background: pink;
  347. }
  348. .all_city .letter_name {
  349. height: 48rpx;
  350. font-size: 24rpx;
  351. color: #999;
  352. background: #f2f5f7;
  353. padding: 0 30rpx;
  354. line-height: 48rpx;
  355. }
  356. .all_city .city {
  357. background-color: #fff;
  358. }
  359. .all_city .city .name {
  360. width: 100%;
  361. padding: 30rpx;
  362. font-size: 28rpx;
  363. color: #333;
  364. position: relative;
  365. overflow: hidden;
  366. }
  367. .all_city .city .name::after {
  368. content: '';
  369. position: absolute;
  370. border-bottom: 1rpx solid #eaeef1;
  371. -webkit-transform: scaleY(0.5);
  372. transform: scaleY(0.5);
  373. bottom: 0;
  374. right: 0;
  375. left: 30rpx;
  376. }
  377. .city_hover {
  378. background-color: #eee !important;
  379. }
  380. .fixed_bar {
  381. position: fixed;
  382. z-index: 999;
  383. top: 180rpx;
  384. right: 0px;
  385. padding-right: 10rpx;
  386. width: 50rpx;
  387. font-size: 22rpx;
  388. text-align: center;
  389. }
  390. .bar_item {
  391. background-color: rgb(233, 228, 220);
  392. border-radius: 50%;
  393. }
  394. @media screen and (max-width: 320px) {
  395. .fixed_bar {
  396. font-size: 20rpx;
  397. }
  398. }
  399. .bar_item_active {
  400. background-color: #fff;
  401. /* box-shadow : 5rpx 5rpx 5rpx #f7c3ee; */
  402. box-shadow: 5rpx 5rpx 5rpx #09afff;
  403. }
  404. .fixed_letter {
  405. position: absolute;
  406. z-index: 20;
  407. width: 160rpx;
  408. height: 160rpx;
  409. left: 50%;
  410. top: 50%;
  411. margin-left: -80rpx;
  412. margin-top: -80rpx;
  413. border-radius: 80rpx;
  414. text-align: center;
  415. line-height: 160rpx;
  416. font-size: 70rpx;
  417. color: #fff;
  418. background-color: rgba(0, 0, 0, 0.5);
  419. /* box-shadow : 5rpx 5rpx 5rpx #f7c3ee; */
  420. box-shadow: 5rpx 5rpx 5rpx #09afff;
  421. }
  422. /* 搜索结果 */
  423. .result_list {
  424. padding-top: 90rpx;
  425. background: #fff;
  426. width: 100%;
  427. }
  428. .r_item {
  429. width: 100%;
  430. position: relative;
  431. padding: 30rpx 0 30rpx 30rpx;
  432. font-size: 28rpx;
  433. color: #333;
  434. box-sizing: border-box;
  435. }
  436. .r_item::after {
  437. content: '';
  438. position: absolute;
  439. border-bottom: 1rpx solid #eaeef1;
  440. -webkit-transform: scaleY(0.5);
  441. transform: scaleY(0.5);
  442. bottom: 0;
  443. right: 0;
  444. left: 30rpx;
  445. }
  446. .r_item_hover {
  447. background-color: #eee !important;
  448. }
  449. .no_data {
  450. height: 500rpx;
  451. align-items: center;
  452. font-size: 26rpx;
  453. color: #999;
  454. display: flex;
  455. flex-direction: column;
  456. }
  457. .no_data image {
  458. width: 250rpx;
  459. height: 162rpx;
  460. margin-bottom: 30rpx;
  461. }
  462. </style>