You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

170 lines
3.6 KiB

  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: var(--up-tips-color, #909399);
  146. margin-bottom: 5px;
  147. }
  148. .u-location-city,
  149. .list__item__city-name {
  150. color: var(--up-main-color, #303133);
  151. }
  152. .u-current-city-item {
  153. height: 30px;
  154. }
  155. .hot-city-list {
  156. display: flex !important;
  157. flex-direction: row !important;
  158. padding: 12px 0;
  159. .hot-city-item {
  160. padding: 6px 12px;
  161. margin: 5px;
  162. color: var(--up-main-color, #303133);
  163. border: 1px solid var(--up-border-color, #ededed);
  164. background-color: var(--up-card-bg-color, #ffffff);
  165. }
  166. }
  167. </style>