search.vue 7.0 KB

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