search.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="search-content">
  3. <view class="search-box">
  4. <view class="search-input-box">
  5. <svg t="1733904658291" class="search-icon" viewBox="0 0 1024 1024" version="1.1"
  6. xmlns="http://www.w3.org/2000/svg" p-id="5981" xmlns:xlink="http://www.w3.org/1999/xlink">
  7. <path
  8. d="M174.545 465.455c0-18.619 16.291-34.91 34.91-34.91s34.909 16.291 34.909 34.91c0 121.018 100.072 221.09 221.09 221.09 18.619 0 34.91 16.291 34.91 34.91s-16.291 34.909-34.91 34.909c-160.581 0-290.909-130.328-290.909-290.91z m290.91 360.727c200.145 0 360.727-160.582 360.727-360.727S665.6 104.727 465.455 104.727 104.727 265.31 104.727 465.455 265.31 826.182 465.455 826.182z m323.49-76.8L961.164 921.6c13.963 13.964 13.963 34.91 0 48.873-13.964 13.963-34.91 13.963-48.873 0L737.745 795.927c-74.472 60.51-169.89 97.746-272.29 97.746C228.073 896 34.909 702.836 34.909 465.455S228.073 34.909 465.455 34.909 896 228.073 896 465.455c0 109.381-39.564 209.454-107.055 283.927z"
  9. p-id="5982">
  10. </path>
  11. </svg>
  12. <input confirm-type="search" ref="searchInputRef" class="search-input" :auto-blur="true" type="text"
  13. placeholder="请输入关键字" v-model="locationName" @blur="onSearchFocus()" />
  14. </view>
  15. <view class="search-select">
  16. <view class="select-item" @click.stop="$refs.cityRef.open()">
  17. <text class="item-text one-row">{{params.codeLabel || '办理区域'}}</text>
  18. <u-icon name="arrow-down-fill" color="#101010" size="16"></u-icon>
  19. </view>
  20. <view class="select-item" @click.stop="$refs.mapTypeRef.open()">
  21. <text class="item-text one-row">{{ params.typeName || '位置类型'}}</text>
  22. <u-icon name="arrow-down-fill" color="#101010" size="16"></u-icon>
  23. </view>
  24. </view>
  25. </view>
  26. <!-- 选择地址 -->
  27. <city ref="cityRef" :code.sync="params.code" @cityName="e => params.codeLabel = e" />
  28. <!-- 选择位置类型 -->
  29. <mapType ref="mapTypeRef" :mapTypeId.sync="params.mapTypeId" @typeName="e => params.typeName = e" />
  30. </view>
  31. </template>
  32. <script>
  33. import { getMapList_Api } from "@/api/map.js"
  34. import { EventBus } from "@/utils/vueBus.js"
  35. import { getLocation } from "@/utils/tool.js"
  36. export default {
  37. data() {
  38. return {
  39. locationPower: true,
  40. locationName: undefined,
  41. selfLatitude: undefined,
  42. selfLongitude: undefined,
  43. // selfLatitude: 30.482926,
  44. // selfLongitude: 114.414431,
  45. params: {
  46. code: undefined,
  47. codeLabel: undefined,
  48. mapTypeId: undefined,
  49. typeName: undefined,
  50. },
  51. SearchTimeout: null,
  52. }
  53. },
  54. mounted() {
  55. EventBus.$on('TianDiTuSearch', (callBack) => {
  56. this.handleSearch(callBack)
  57. }); // 确保在组件销毁前移除事件监听
  58. },
  59. beforeDestroy() {
  60. EventBus.$off("TianDiTuSearch")
  61. },
  62. methods: {
  63. init() {
  64. },
  65. /**
  66. * 获取定位
  67. * 有callBack,着是手动定位,地图会平移调用搜索,所以无需再次搜索
  68. * 没有callBack,则需要手动收索
  69. *
  70. */
  71. handleSearch(callBack) {
  72. try {
  73. clearTimeout(this.SearchTimeout)
  74. this.SearchTimeout = null;
  75. this.SearchTimeout = setTimeout(() => {
  76. if (!callBack && !this.locationPower) {
  77. this.onSearchFocus()
  78. return
  79. }
  80. uni.showLoading({
  81. mask: true
  82. })
  83. this.locationPower = true;
  84. getLocation().then(res => {
  85. const { longitude, latitude } = res || {}
  86. if (longitude && latitude) {
  87. this.selfLatitude = latitude;
  88. this.selfLongitude = longitude;
  89. }
  90. callBack && callBack({ longitude: res.longitude, latitude: res.latitude })
  91. }).catch(err => {
  92. this.locationPower = false
  93. uni.showToast({
  94. title: '获取定位失败',
  95. icon: 'none'
  96. })
  97. }).finally(() => {
  98. if (!callBack) this.onSearchFocus()
  99. uni.hideLoading()
  100. })
  101. }, 150)
  102. } catch (error) {
  103. console.log(999977999, error)
  104. //TODO handle the exception
  105. }
  106. },
  107. onSearchFocus() {
  108. let pms = {};
  109. try {
  110. if (this.params.code) {
  111. pms.areaCode = this.params.code || undefined
  112. }
  113. pms.mapTypeId = this.params.mapTypeId;
  114. pms.selfLatitude = this.selfLatitude;
  115. pms.selfLongitude = this.selfLongitude;
  116. pms.locationName = this.locationName;
  117. } catch (error) {
  118. console.log("定位收索11 error", error)
  119. //TODO handle the exception
  120. }
  121. this.$emit("handleSearch", pms)
  122. },
  123. getVal() {
  124. return {
  125. }
  126. },
  127. getMapList() {
  128. }
  129. },
  130. watch: {
  131. params: {
  132. handler(newV, oldV) {
  133. this.handleSearch()
  134. },
  135. deep: true
  136. }
  137. }
  138. }
  139. </script>
  140. <style lang="scss" scoped>
  141. $radius_ : 20rpx;
  142. $heig_: 80rpx;
  143. .search-box {
  144. // padding: 20rpx 20rpx 0;
  145. position: fixed;
  146. left: 20rpx;
  147. top: 20rpx;
  148. right: 20rpx;
  149. z-index: 999;
  150. background-color: #fff;
  151. border-radius: $radius_ ;
  152. overflow: hidden;
  153. .search-input-box {
  154. width: 100%;
  155. height: $heig_;
  156. display: flex;
  157. align-items: center;
  158. padding: 0 10px;
  159. color: #020202;
  160. .search-icon {
  161. width: 45rpx;
  162. height: 45rpx;
  163. path {
  164. fill: #020202;
  165. }
  166. }
  167. .search-input {
  168. padding-left: 10px;
  169. }
  170. }
  171. .search-select {
  172. border-top: 1rpx solid #E8E8E8;
  173. width: 100%;
  174. height: $heig_;
  175. display: flex;
  176. justify-content: space-between;
  177. align-items: center;
  178. .select-item {
  179. width: 50%;
  180. display: flex;
  181. justify-content: center;
  182. align-items: center;
  183. font-size: 30rpx;
  184. padding: 0 10rpx;
  185. .item-text {
  186. display: inline-block;
  187. padding-right: 6rpx;
  188. overflow: hidden;
  189. }
  190. }
  191. }
  192. }
  193. </style>