search.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <view class="">
  3. <navbar :config="config" backColor="#999999"></navbar>
  4. <view class="search-box" :style="{'height':topFixedHeight + 'rpx'}">
  5. <view class="search">
  6. <u-search border-color="transparent" bg-color="transparent" :show-action="false"
  7. search-icon-color="#000" placeholder-color="#999999" color="#333333"
  8. :placeholder="defaultKeyword ? defaultKeyword : '请输入检索关键字'" v-model="keyword"
  9. @search="searchData"></u-search>
  10. <text class="search-btn" @click.stop="searchData">搜索</text>
  11. </view>
  12. </view>
  13. <scroll-view class="search-content" :style="{'height':pageHeight+ 'px'}" scroll-y="true">
  14. <view class="" v-show="searchContent" style="min-height: 100%;">
  15. <template v-if="searchList.length > 0 || searchLoading">
  16. <view class="search-item" v-for="item in searchList" @click.stop="$openPage(item)">
  17. <image class="search-icon" :src="$getImgPath(item.icon)" mode="aspectFit"></image>
  18. <text class="search-text">{{item.thirdName}}</text>
  19. <text class="search-look iconfont_yige">&#xe65f;</text>
  20. </view>
  21. </template>
  22. <template v-else>
  23. <view class="empty-data">
  24. <EmptyDate />
  25. </view>
  26. </template>
  27. </view>
  28. <view class="" v-show="!searchContent">
  29. <view class="search-hint" v-if="keywordArr && keywordArr.length > 0">
  30. <view class="hint-title">
  31. <text>历史搜索</text>
  32. <text @click.stop="clearSearchRecord()">清除</text>
  33. </view>
  34. <view class="hint-content">
  35. <block v-for="item in keywordArr">
  36. <text v-if="item" @click.stop="defaultSearch(item)">{{ item }}</text>
  37. </block>
  38. </view>
  39. </view>
  40. <view class="search-hint" v-if="topSearch && topSearch.length > 0">
  41. <view class="hint-title">
  42. 热门搜索
  43. </view>
  44. <view class="hint-content">
  45. <block v-for="item in topSearch">
  46. <text v-if="item" @click.stop="defaultSearch(item)">{{ item }}</text>
  47. </block>
  48. </view>
  49. </view>
  50. </view>
  51. </scroll-view>
  52. </view>
  53. </template>
  54. <script>
  55. import {
  56. getThirdSearch
  57. } from "@/api/government.js"
  58. import Mixin from "./Mixin.js"
  59. import {
  60. getSearchInfo
  61. } from "@/api/government.js"
  62. export default {
  63. mixins: [Mixin],
  64. data() {
  65. return {
  66. topFixedHeight: 100,
  67. config: {
  68. back: true,
  69. closePath: '',
  70. title: '搜索',
  71. color: 'black',
  72. backgroundColor: [1, '#fff'],
  73. statusBarFontColor: 'black'
  74. },
  75. topSearch: [],
  76. keyword: "",
  77. defaultKeyword: '',
  78. keywordArr: [],
  79. searchContent: false,
  80. searchList: [],
  81. searchLoading: false
  82. }
  83. },
  84. onLoad() {
  85. uni.getStorage({
  86. key: this.$keys.search_keyword,
  87. success: res => {
  88. // this.keywordArr = ['社会白社会白社会白社会白社会白社会白社会白社会白社会白社会白社会白社会白社会白社会白', ...res.data] || []
  89. this.keywordArr = res.data || []
  90. },
  91. fail: err => {
  92. this.keywordArr = []
  93. }
  94. });
  95. getSearchInfo().then(res => {
  96. const data = res.data || {}
  97. this.defaultKeyword = data?.defaultKeyword;
  98. const arr = data?.keywords.split(",")
  99. this.topSearch = arr;
  100. })
  101. },
  102. onShow() {
  103. },
  104. methods: {
  105. clearSearchRecord() {
  106. uni.removeStorage({
  107. key: this.$keys.search_keyword,
  108. success: res => {
  109. this.keywordArr = []
  110. }
  111. });
  112. },
  113. defaultSearch(e) {
  114. this.keyword = e;
  115. this.searchData()
  116. },
  117. searchData() {
  118. if (!this.keyword && !this.defaultKeyword) return
  119. this.keyword = this.keyword || this.defaultKeyword
  120. const text = this.keyword;
  121. this.searchLoading = true;
  122. this.searchList = [];
  123. this.searchContent = true;
  124. getThirdSearch({
  125. keyword: text,
  126. status:true
  127. }).then(res => {
  128. this.searchList = res.rows
  129. }).finally(() => {
  130. this.searchLoading = false;
  131. this.setHotKeyword(text)
  132. })
  133. },
  134. setHotKeyword(text) {
  135. try {
  136. this.keywordArr.forEach((el, index) => {
  137. if (el === text) {
  138. this.keywordArr.splice(index, 1)
  139. throw new Error()
  140. }
  141. });
  142. } catch (e) {
  143. //TODO handle the exception
  144. }
  145. if (this.keywordArr.length >= 10) {
  146. this.keywordArr.splice(9, this.keywordArr.length - 9)
  147. }
  148. this.keywordArr.unshift(this.keyword)
  149. uni.setStorage({
  150. key: this.$keys.search_keyword,
  151. data: this.keywordArr,
  152. success: function() {
  153. console.log('success');
  154. }
  155. });
  156. }
  157. },
  158. watch: {
  159. keyword: {
  160. handler: function(newKw, oldKw) {
  161. if (!newKw && this.searchContent) this.searchContent = false
  162. }
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. .search-box {
  169. padding: 0 30rpx;
  170. display: flex;
  171. align-items: center;
  172. .search {
  173. width: 100%;
  174. border: 1rpx solid #999999;
  175. border-radius: 42rpx;
  176. height: 82rpx;
  177. display: flex;
  178. justify-content: space-between;
  179. align-items: center;
  180. padding-right: 38rpx;
  181. .search-btn {
  182. font-size: 30rpx;
  183. font-family: Microsoft YaHei, Microsoft YaHei-Bold;
  184. font-weight: 700;
  185. text-align: left;
  186. color: #5fcee5;
  187. }
  188. }
  189. }
  190. .search-content {
  191. width: 100%;
  192. padding: 0 30rpx;
  193. box-sizing: border-box;
  194. .search-item {
  195. width: 100%;
  196. height: 100rpx;
  197. display: flex;
  198. justify-content: space-between;
  199. align-items: center;
  200. border-bottom: 1rpx solid #eee;
  201. .search-icon {
  202. width: 50rpx;
  203. height: 50rpx;
  204. }
  205. .search-text {
  206. flex: 1;
  207. padding: 0 20rpx;
  208. font-size: 30rpx;
  209. font-family: Microsoft YaHei, Microsoft YaHei-Regular;
  210. font-weight: 400;
  211. color: #333333;
  212. }
  213. .search-look {
  214. font-size: 34rpx;
  215. color: #c2c2c2;
  216. }
  217. }
  218. .empty-data {
  219. padding-top: 100rpx;
  220. }
  221. }
  222. .search-hint {
  223. padding-top: 40rpx;
  224. .hint-title {
  225. font-size: 34rpx;
  226. font-weight: bold;
  227. padding-bottom: 10rpx;
  228. display: flex;
  229. justify-content: space-between;
  230. align-items: center;
  231. text+text {
  232. font-size: 28rpx;
  233. font-weight: 400;
  234. color: #b9b8b8;
  235. }
  236. }
  237. .hint-content {
  238. width: 100%;
  239. display: flex;
  240. align-items: stretch;
  241. flex-wrap: wrap;
  242. text {
  243. flex-shrink: 0;
  244. margin: 20rpx 20rpx 0 0;
  245. // border-radius: 5rpx;
  246. padding: 15rpx 30rpx;
  247. font-size: 24rpx;
  248. line-height: 1.3;
  249. background: #f6f8fa;
  250. border-radius: 20rpx;
  251. }
  252. }
  253. }
  254. // .search-page {
  255. // width: 100%;
  256. // padding: 0 20rpx;
  257. // }
  258. </style>