u-city-locate.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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">{{ t("up.cityLocate.locateCity") }}</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. import { t } from '../../libs/i18n'
  43. export default{
  44. name: 'u-city-locate',
  45. props:{
  46. indexList: {
  47. type: Array,
  48. default: ['🔥']
  49. },
  50. cityList:{
  51. type: Array,
  52. default: () => {
  53. return [
  54. [{
  55. name: '北京',
  56. value: 'beijing'
  57. },
  58. {
  59. name: '上海',
  60. value: 'shanghai'
  61. },
  62. {
  63. name: '广州',
  64. value: 'guangzhou'
  65. },
  66. {
  67. name: '深圳',
  68. value: 'shenzhen'
  69. },
  70. {
  71. name: '杭州',
  72. value: 'hangzhou'
  73. }]
  74. ]
  75. }
  76. },
  77. locationType: {
  78. type: String,
  79. default: 'wgs84'
  80. },
  81. currentCity: {
  82. type: String,
  83. default: ''
  84. },
  85. nameKey: {
  86. type: String,
  87. default: 'name'
  88. }
  89. },
  90. computed:{
  91. },
  92. watch:{
  93. currentCity(val) {
  94. this.locationCity = val;
  95. }
  96. },
  97. data(){
  98. return{
  99. locationCity: t("up.cityLocate.locating") + '....'
  100. }
  101. },
  102. emits: ['location-success', 'select-city'],
  103. methods:{
  104. t,
  105. // 获取城市
  106. selectedCity(city){
  107. this.locationCity = city[this.nameKey];
  108. this.$emit('select-city', {
  109. locationCity: this.locationCity
  110. });
  111. },
  112. // 定位操作
  113. location(){
  114. let That = this;
  115. uni.getLocation({
  116. type: this.locationType,
  117. geocode:true,
  118. success(res){
  119. console.log(res);
  120. That.locationCity = res.address && res.address.city;
  121. That.$emit('location-success', {
  122. ...res,
  123. locationCity: That.locationCity
  124. });
  125. },
  126. fail(){
  127. That.locationCity = t("up.cityLocate.fail");
  128. }
  129. });
  130. },
  131. },
  132. // 页面挂载后进行异步操作
  133. created(){
  134. },
  135. mounted(){
  136. this.location();
  137. }
  138. }
  139. </script>
  140. <style lang="scss">
  141. .list__item {
  142. padding: 8px 1px;
  143. }
  144. .u-current-city-title {
  145. color: grey;
  146. margin-bottom: 5px;
  147. }
  148. .u-current-city-item {
  149. height: 30px;
  150. }
  151. .hot-city-list {
  152. display: flex !important;
  153. flex-direction: row !important;
  154. padding: 12px 0;
  155. .hot-city-item {
  156. padding: 6px 12px;
  157. margin: 5px;
  158. border: 1px solid #ededed;
  159. }
  160. }
  161. </style>