u-city-locate.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view class="u-city-locate">
  3. <up-index-list :indexList="indexList">
  4. <template #header>
  5. <view class="u-current-city-wrap">
  6. <view class="u-current-city-title">定位城市</view>
  7. <view class="u-current-city-item" @tap="location">
  8. <view class="u-location-city">{{locationCity}}</view>
  9. </view>
  10. </view>
  11. </template>
  12. <template :key="index" v-for="(item, index) in cityList">
  13. <!-- #ifdef APP-NVUE -->
  14. <up-index-anchor :text="indexList[index]"></up-index-anchor>
  15. <!-- #endif -->
  16. <up-index-item>
  17. <!-- #ifndef APP-NVUE -->
  18. <up-index-anchor :text="indexList[index]"></up-index-anchor>
  19. <!-- #endif -->
  20. <view class="hot-city-list" v-if="index == 0">
  21. <view class="" v-for="(item1, index1) in item" @tap="selectedCity(item1)">
  22. <view class="hot-city-item">{{ item1[nameKey] }}</view>
  23. </view>
  24. </view>
  25. <view v-else class="item-list" v-for="(item1, index1) in item" :key="index1">
  26. <view class="list__item" @tap="selectedCity(item1)">
  27. <text class="list__item__city-name">{{item1[nameKey]}}</text>
  28. </view>
  29. <up-line></up-line>
  30. </view>
  31. </up-index-item>
  32. </template>
  33. <template #footer>
  34. <view class="u-safe-area-inset--bottom">
  35. <text class="list__footer"></text>
  36. </view>
  37. </template>
  38. </up-index-list>
  39. </view>
  40. </template>
  41. <script>
  42. export default{
  43. name: 'u-city-locate',
  44. props:{
  45. indexList: {
  46. type: Array,
  47. default: ['🔥']
  48. },
  49. cityList:{
  50. type: Array,
  51. default: () => {
  52. return [
  53. [{
  54. name: '北京',
  55. value: 'beijing'
  56. },
  57. {
  58. name: '上海',
  59. value: 'shanghai'
  60. },
  61. {
  62. name: '广州',
  63. value: 'guangzhou'
  64. },
  65. {
  66. name: '深圳',
  67. value: 'shenzhen'
  68. },
  69. {
  70. name: '杭州',
  71. value: 'hangzhou'
  72. }]
  73. ]
  74. }
  75. },
  76. locationType: {
  77. type: String,
  78. default: 'wgs84'
  79. },
  80. currentCity: {
  81. type: String,
  82. default: ''
  83. },
  84. nameKey: {
  85. type: String,
  86. default: 'name'
  87. }
  88. },
  89. computed:{
  90. },
  91. watch:{
  92. currentCity(val) {
  93. this.locationCity = val;
  94. }
  95. },
  96. data(){
  97. return{
  98. locationCity: '定位中....'
  99. }
  100. },
  101. emits: ['location-success', 'select-city'],
  102. methods:{
  103. // 获取城市
  104. selectedCity(city){
  105. this.locationCity = city[this.nameKey];
  106. this.$emit('select-city', {
  107. locationCity: this.locationCity
  108. });
  109. },
  110. // 定位操作
  111. location(){
  112. let That = this;
  113. uni.getLocation({
  114. type: this.locationType,
  115. geocode:true,
  116. success(res){
  117. console.log(res);
  118. That.locationCity = res.address && res.address.city;
  119. That.$emit('location-success', {
  120. ...res,
  121. locationCity: That.locationCity
  122. });
  123. },
  124. fail(){
  125. That.locationCity = "定位失败,请点击重试"
  126. }
  127. });
  128. },
  129. },
  130. // 页面挂载后进行异步操作
  131. created(){
  132. },
  133. mounted(){
  134. this.location();
  135. }
  136. }
  137. </script>
  138. <style lang="scss">
  139. .list__item {
  140. padding: 8px 1px;
  141. }
  142. .u-current-city-title {
  143. color: grey;
  144. margin-bottom: 5px;
  145. }
  146. .u-current-city-item {
  147. height: 30px;
  148. }
  149. .hot-city-list {
  150. display: flex !important;
  151. flex-direction: row !important;
  152. padding: 12px 0;
  153. .hot-city-item {
  154. padding: 6px 12px;
  155. margin: 5px;
  156. border: 1px solid #ededed;
  157. }
  158. }
  159. </style>