navbar-city-search.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="slot">
  3. <view class="navbar" :style="[{ minHeight: navBarHeight + 'px' }]">
  4. <view
  5. class="inner"
  6. :style="[
  7. { minHeight: menuHeight + 'px' },
  8. { lineHeight: menuHeight + 'px' },
  9. { paddingLeft: menuRight * 2 + 'px' },
  10. { paddingRight: menuRight * 2 + 'px' },
  11. { paddingTop: navBarHeight - menuHeight - menuTop + 'px' },
  12. { paddingBottom: '20rpx' }
  13. ]"
  14. >
  15. <view class="loaction-slot">
  16. <view class="loaction-title" v-if="locationStatus == 1"><text>正在获取地理位置...</text></view>
  17. <view class="loaction-title" v-if="locationStatus == 2" @click="goCitySelectPage">
  18. <text>{{ cityListSelected ? cityListSelected : locationCity }}</text>
  19. <u-icon style="margin-left: 8rpx;" :size="18" color="#171717" name="arrow-down-fill"></u-icon>
  20. </view>
  21. <view class="loaction-title" v-if="locationStatus == 3" @click="getLocation(true)"><text>地理位置获取失败,点击重试</text></view>
  22. </view>
  23. <view class="search-slot" :style="{ paddingTop: menuTop + 'px' }">
  24. <u-search @click="goSearchPage" disabled :placeholder="placeholder" :showAction="false" shape="square" borderRadius="0rpx" bg-color="#F4F5F8"></u-search>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="slot-height" :style="[{ height: navBarHeight + menuHeight + menuTop + 11 + 'px' }]"></view>
  29. </view>
  30. </template>
  31. <script>
  32. import { getLocation, getLocationAgain } from '@/utils/location.js';
  33. const app = getApp();
  34. export default {
  35. name: 'navbar-city-search',
  36. props: {
  37. // 标题
  38. title: {
  39. type: String,
  40. default: '标题'
  41. },
  42. // 占位内容
  43. placeholder: {
  44. type: String,
  45. default: '请输入'
  46. },
  47. // 已经选择的城市
  48. cityListSelected: {
  49. type: String,
  50. default: ''
  51. }
  52. },
  53. data() {
  54. return {
  55. // 导航栏高度
  56. menuTop: app.globalData.menuTop,
  57. navBarHeight: app.globalData.navBarHeight,
  58. menuRight: app.globalData.menuRight,
  59. menuBotton: app.globalData.menuBotton,
  60. menuHeight: app.globalData.menuHeight,
  61. statusBarHeight: app.globalData.statusBarHeight,
  62. // 定位数据
  63. locationData: {},
  64. // 当前城市
  65. locationCity: '',
  66. // 获取地理位置状态,1正在获取,2获取成功,3失败
  67. locationStatus: 1
  68. };
  69. },
  70. mounted() {
  71. if (this.cityListSelected) {
  72. this.locationStatus = 2;
  73. } else {
  74. this.getLocation();
  75. }
  76. },
  77. methods: {
  78. // 跳转城市选择页面
  79. goCitySelectPage() {
  80. uni.navigateTo({
  81. url: '/pages/home/city-list?nowCity=' + this.locationCity
  82. });
  83. },
  84. // 去搜索页面
  85. goSearchPage() {
  86. uni.navigateTo({
  87. url: '/pages/search'
  88. });
  89. },
  90. // 获取地理位置
  91. async getLocation(isAgain = false) {
  92. this.locationStatus = 1;
  93. if (!isAgain) {
  94. getLocation(res => {
  95. this.setLocationData(res);
  96. });
  97. } else {
  98. getLocationAgain(res => {
  99. this.setLocationData(res);
  100. });
  101. }
  102. },
  103. // 设置定位数据
  104. setLocationData(res) {
  105. if (res.status) {
  106. this.locationStatus = 2;
  107. this.locationData = res.data;
  108. this.locationCity = res.data.ad_info.city;
  109. this.$store.commit('user/COMMIT_LOACTION_INFO', res.data);
  110. } else {
  111. this.locationStatus = 3;
  112. uni.showToast({
  113. title: '获取地理位置失败',
  114. duration: 3000,
  115. icon: 'none'
  116. });
  117. }
  118. }
  119. }
  120. };
  121. </script>
  122. <style lang="scss" scoped>
  123. .slot {
  124. width: 100vw;
  125. }
  126. .navbar {
  127. width: 100%;
  128. position: fixed;
  129. top: 0;
  130. left: 0;
  131. z-index: 899;
  132. overflow: hidden;
  133. }
  134. .inner {
  135. width: 100%;
  136. height: 100%;
  137. background-color: $app-theme-bg-color;
  138. }
  139. .loaction-slot {
  140. display: flex;
  141. align-items: center;
  142. .loaction-title {
  143. display: flex;
  144. align-items: center;
  145. text {
  146. font-size: 32rpx;
  147. font-family: PingFang SC;
  148. font-weight: 400;
  149. color: $app-theme-text-black-color;
  150. }
  151. }
  152. }
  153. .search-slot {
  154. }
  155. </style>