search.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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(true)" />
  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: false,
  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. LocationTimeout: null,
  52. SearchTimeout: null,
  53. }
  54. },
  55. created() {
  56. this.init()
  57. },
  58. mounted() {
  59. EventBus.$on('TianDiTuSearch', (callBack) => {
  60. this.handleSearch(callBack)
  61. }); // 确保在组件销毁前移除事件监听
  62. },
  63. beforeDestroy() {
  64. EventBus.$off("TianDiTuSearch")
  65. },
  66. methods: {
  67. init() {
  68. clearTimeout(this.LocationTimeout);
  69. this.LocationTimeout = null;
  70. this.LocationTimeout = setTimeout(() => {
  71. getLocation().then(res => {
  72. const { longitude, latitude } = res || {}
  73. if (longitude && latitude) {
  74. this.selfLatitude = latitude;
  75. this.selfLongitude = longitude;
  76. clearTimeout(this.LocationTimeout);
  77. this.LocationTimeout = null;
  78. this.onSearchFocus()
  79. } else {
  80. this.init()
  81. }
  82. }).catch(err => {
  83. this.init()
  84. })
  85. }, 1000)
  86. },
  87. /**
  88. * 获取定位
  89. * 有callBack,着是手动定位,地图会平移调用搜索,所以无需再次搜索
  90. * 没有callBack,则需要手动收索
  91. *
  92. */
  93. handleSearch(callBack) {
  94. try {
  95. uni.showLoading({
  96. mask: true
  97. })
  98. getLocation().then(res => {
  99. const { longitude, latitude } = res || {}
  100. if (longitude && latitude) {
  101. this.selfLatitude = latitude;
  102. this.selfLongitude = longitude;
  103. }
  104. callBack && callBack({ longitude: res.longitude, latitude: res.latitude })
  105. }).catch(err => {
  106. uni.showToast({
  107. title: '获取定位失败',
  108. icon: 'none'
  109. })
  110. }).finally(() => {
  111. uni.hideLoading()
  112. })
  113. } catch (error) {
  114. console.log(999977999, error)
  115. //TODO handle the exception
  116. }
  117. },
  118. onSearchFocus(search = false) {
  119. try {
  120. clearTimeout(this.SearchTimeout)
  121. this.SearchTimeout = null;
  122. this.SearchTimeout = setTimeout(() => {
  123. let pms = {};
  124. try {
  125. if (this.params.code) {
  126. pms.areaCode = this.params.code || undefined
  127. }
  128. pms.mapTypeId = this.params.mapTypeId;
  129. pms.selfLatitude = this.selfLatitude;
  130. pms.selfLongitude = this.selfLongitude;
  131. pms.locationName = this.locationName;
  132. pms.search = search
  133. } catch (error) {
  134. console.log("定位收索11 error", error)
  135. //TODO handle the exception
  136. }
  137. this.$emit("handleSearch", pms)
  138. }, 150)
  139. } catch (error) {
  140. console.log(999977999, error)
  141. //TODO handle the exception
  142. }
  143. },
  144. getVal() {
  145. return {
  146. }
  147. },
  148. getMapList() {
  149. }
  150. },
  151. watch: {
  152. params: {
  153. handler(newV, oldV) {
  154. this.onSearchFocus(true)
  155. },
  156. deep: true
  157. }
  158. }
  159. }
  160. </script>
  161. <style lang="scss" scoped>
  162. $radius_ : 20rpx;
  163. $heig_: 80rpx;
  164. .search-box {
  165. // padding: 20rpx 20rpx 0;
  166. position: fixed;
  167. left: 20rpx;
  168. top: 20rpx;
  169. right: 20rpx;
  170. z-index: 999;
  171. background-color: #fff;
  172. border-radius: $radius_ ;
  173. overflow: hidden;
  174. .search-input-box {
  175. width: 100%;
  176. height: $heig_;
  177. display: flex;
  178. align-items: center;
  179. padding: 0 10px;
  180. color: #020202;
  181. .search-icon {
  182. width: 45rpx;
  183. height: 45rpx;
  184. path {
  185. fill: #020202;
  186. }
  187. }
  188. .search-input {
  189. padding-left: 10px;
  190. }
  191. }
  192. .search-select {
  193. border-top: 1rpx solid #E8E8E8;
  194. width: 100%;
  195. height: $heig_;
  196. display: flex;
  197. justify-content: space-between;
  198. align-items: center;
  199. .select-item {
  200. width: 50%;
  201. display: flex;
  202. justify-content: center;
  203. align-items: center;
  204. font-size: 30rpx;
  205. padding: 0 10rpx;
  206. .item-text {
  207. display: inline-block;
  208. padding-right: 6rpx;
  209. overflow: hidden;
  210. }
  211. }
  212. }
  213. }
  214. </style>