city.vue 4.3 KB

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