city.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view>
  3. <u-index-list :index-list="indexList" activeColor="#4581fb">
  4. <view class="list">
  5. <view class="search">
  6. <u-search placeholder="搜索城市名/拼音" v-model="title" bgColor="white" :showAction="false" @clear="show = false"></u-search>
  7. </view>
  8. <!-- #ifdef APP-PLUS-->
  9. <view class="bbv animated fadeInDown" v-if="item.title">
  10. <view class="title">定位城市</view>
  11. <view class="item" @click="getLocation()">
  12. <view class="tags" @click="select(item)">
  13. <view class="out">
  14. <view class="int">{{ item.title }}</view>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. <!-- #endif -->
  20. <view class="bbv">
  21. <view class="title">热门城市</view>
  22. <view class="item">
  23. <view class="tags" v-for="(item, index) in hot_city" :key="item.id" @click="select(item)">
  24. <view class="out">
  25. <view class="int omit">{{ item.title }}</view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. <view v-for="(item, index) in list" :key="index" class="item">
  31. <u-index-item>
  32. <u-index-anchor :text="indexList[index]"></u-index-anchor>
  33. <view class="tags" v-for="(child, i) in item" :key="child.title" @click="select(child)">
  34. <view class="out">
  35. <view class="int omit">{{ child.title }}</view>
  36. </view>
  37. </view>
  38. </u-index-item>
  39. </view>
  40. <view class="loading" v-if="loading"><u-loadmore :status="loading ? 'loading' : 'nomore'" /></view>
  41. </view>
  42. </u-index-list>
  43. <view class="search_view" v-if="show">
  44. <view class="list">
  45. <view class="item" v-for="(item, index) in search_list" :key="item.id" @click="select(item)">
  46. <view class="title">{{ item.title }}</view>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. export default {
  54. data() {
  55. return {
  56. loading: true,
  57. item: {},
  58. eventName: 'select_city',
  59. search_list: [], //搜索列表
  60. show: false,
  61. title: '',
  62. indexList: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z'],
  63. hot_city: [{ title: '不限' }, { id: '3288', title: '南宁市' }, { id: '1215', title: '北京市' }, { id: '2003', title: '上海市' }, { id: '3169', title: '深圳市' }, { id: '2131', title: '杭州市' }, { id: '3146', title: '广州市' }],
  64. list: [],
  65. all: []
  66. };
  67. },
  68. watch: {
  69. title(val) {
  70. if (val) {
  71. this.show = true;
  72. this.search_list = this.all.filter((item) => item.level == 2 && (item.title.indexOf(val) != -1 || item.pinyin.indexOf(val) != -1));
  73. } else {
  74. this.show = false;
  75. this.search_list = [];
  76. }
  77. }
  78. },
  79. onLoad(e) {
  80. if (e.eventName) {
  81. this.eventName = e.eventName;
  82. }
  83. setTimeout(() => {
  84. this.getData();
  85. }, 500);
  86. },
  87. methods: {
  88. getData() {
  89. this.all = uni.getStorageSync('city_all');
  90. this.indexList.forEach((i) => {
  91. this.list.push(this.all.filter((item) => item.level == 2 && i == item.pin));
  92. });
  93. this.loading = false;
  94. },
  95. //定位城市
  96. getLocation() {
  97. uni.getLocation({
  98. type: 'wgs84',
  99. geocode: true,
  100. success: (res) => {
  101. // 创建地图坐标对象
  102. let point = new plus.maps.Point(res.longitude, res.latitude);
  103. //静态方法,反向地理编码
  104. plus.maps.Map.reverseGeocode(point, {}, (event) => {
  105. let reg = /.+?(省|市|自治区|自治州|县|区)/g;
  106. let addressList = event.address.match(reg).toString().split(',');
  107. //注意区分直辖市;
  108. let city = addressList.length >= 3 ? addressList[1] : addressList[0];
  109. this.item = this.all.filter((item) => item.level == 2 && item.title == city)[0];
  110. this.$forceUpdate();
  111. });
  112. },
  113. fail: (err) => {
  114. uni.showModal({ content: '定位失败,请检查是否允许地位', showCancel: false });
  115. }
  116. });
  117. },
  118. //选择
  119. select(item) {
  120. uni.$emit(this.eventName, item);
  121. uni.navigateBack();
  122. }
  123. }
  124. };
  125. </script>
  126. <style lang="scss">
  127. .list {
  128. padding: 10px 25px 10px 25px;
  129. .search {
  130. padding: 0px 10px 15px 10px;
  131. }
  132. .title {
  133. padding-left: 7px;
  134. font-weight: bold;
  135. margin-bottom: 7px;
  136. }
  137. .bbv {
  138. margin-top: 15px;
  139. }
  140. .item {
  141. overflow: hidden;
  142. margin-top: 10px;
  143. }
  144. .tags {
  145. width: 33.3333%;
  146. .int {
  147. background-color: white;
  148. }
  149. }
  150. }
  151. .search_view {
  152. /* #ifdef H5 */
  153. top: 100px;
  154. height: 88%;
  155. /* #endif */
  156. /* #ifdef APP-PLUS||MP-WEIXIN */
  157. top: 60px;
  158. height: 92%;
  159. /* #endif */
  160. }
  161. .u-index-item {
  162. display: block !important;
  163. }
  164. .u-index-anchor {
  165. margin-left: 5px;
  166. margin-right: 5px;
  167. border-radius: 3px;
  168. margin-bottom: 10px;
  169. }
  170. </style>