search.vue 6.6 KB

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