Search.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <template>
  2. <view class="search-container">
  3. <!-- 搜索框 -->
  4. <view class="search-header">
  5. <view class="cancel-btn" @click="goBack">
  6. <uni-icons type="left" size="22" color="#1F1F1F"></uni-icons>
  7. </view>
  8. <view class="search-box">
  9. <!-- <uni-icons type="search" size="16" color="#999"></uni-icons> -->
  10. <input type="text" v-model="searchKeyword" placeholder="请输入关键词" confirm-type="search" @confirm="onSearch"
  11. @input="handleInput" />
  12. <uni-icons v-if="searchKeyword" type="clear" size="16" color="#999" @click="clearKeyword"></uni-icons>
  13. <view class="searchImgBox" @click="onSearch">
  14. <image class="image" src="@/static/home/sy_icon_sousuo.png"></image>
  15. </view>
  16. </view>
  17. </view>
  18. <view class="reserveASeat"></view>
  19. <!-- 搜索历史 -->
  20. <view class="search-history" v-if="searchStatus && historyList.length > 0">
  21. <view class="history-header">
  22. <text class="title">搜索历史</text>
  23. <image @click="clearHistory" style="width: 32rpx;height: 32rpx;" class="deleteAll"
  24. src="@/static/home/sy_icon_shanchu.png"></image>
  25. </view>
  26. <view class="history-list">
  27. <view class="history-item" v-for="(item, index) in displayedHistoryList" :key="index"
  28. @click="useHistoryKeyword(item)">
  29. <view>
  30. <uni-icons type="clock" size="14" color="#999"></uni-icons>
  31. <view class="history-text">
  32. <image src="@/static/home/sy_icon_lishijilu.png"></image> {{ item }}
  33. </view>
  34. </view>
  35. <image class="deleteBtn" @click.stop="deleteHistoryItem(item)" src="@/static/icon/close.png"></image>
  36. </view>
  37. <view class="expandBtn" @click="toggleHistory">
  38. <view v-if="!isExpanded">
  39. <template v-if="historyList.length > 5">
  40. 查看全部
  41. <image src="@/static/home/sy_icon_chakanquanbu.png"></image>
  42. </template>
  43. </view>
  44. <view v-else class="fold">
  45. 折叠历史记录
  46. <image src="@/static/home/sy_icon_chakanquanbu.png"></image>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. <!-- 搜索结果 -->
  52. <view class="search-result" v-if="!searchStatus">
  53. <block v-if="searchResult.length > 0">
  54. <view class="items-grid">
  55. <CrowdFundingItem v-for="item in searchResult" :key="item.id" :item="item" @click="goToDetail(item.id)" />
  56. </view>
  57. <view v-if="isLoading" class="loading-more">加载中...</view>
  58. <view v-else-if="!hasMore" class="no-more">没有更多了</view>
  59. </block>
  60. <view class="no-data" v-else-if="!isLoading">
  61. <text>暂无数据</text>
  62. </view>
  63. </view>
  64. <DialogBox ref="DialogBox"></DialogBox>
  65. </view>
  66. </template>
  67. <script>
  68. import CrowdFundingItem from './components/CrowdFundingItem/CrowdFundingItem.vue';
  69. const HISTORY_KEY = 'search_history';
  70. const MAX_HISTORY = 20;
  71. export default {
  72. components: { CrowdFundingItem },
  73. data() {
  74. return {
  75. searchKeyword: '', // 搜索关键词
  76. historyList: [], // 搜索历史
  77. searchResult: [],
  78. page: 1,
  79. pageSize: 20,
  80. hasMore: true,
  81. isLoading: false,
  82. isExpanded: false, // 添加展开/折叠状态变量
  83. searchStatus: true,//显示搜索历史 false显示搜索结果
  84. }
  85. },
  86. onLoad() {
  87. // 加载历史记录
  88. this.loadHistory();
  89. },
  90. onPullDownRefresh() {
  91. this.onSearch(true);
  92. uni.stopPullDownRefresh();
  93. },
  94. onReachBottom() {
  95. this.onSearch(false);
  96. },
  97. computed: {
  98. // 添加计算属性以控制显示的历史记录数量
  99. displayedHistoryList() {
  100. return this.isExpanded ? this.historyList : this.historyList.slice(0, 5);
  101. }
  102. },
  103. methods: {
  104. // 加载历史记录
  105. loadHistory() {
  106. try {
  107. const history = uni.getStorageSync(HISTORY_KEY);
  108. this.historyList = history ? JSON.parse(history) : [];
  109. } catch (e) {
  110. console.error('Failed to load search history:', e);
  111. this.historyList = [];
  112. }
  113. },
  114. // 保存历史记录
  115. saveHistory() {
  116. try {
  117. // 将当前搜索词添加到历史记录开头
  118. if (this.searchKeyword && !this.historyList.includes(this.searchKeyword)) {
  119. console.log(this.historyList, 11111);
  120. this.historyList.unshift(this.searchKeyword);
  121. console.log(this.historyList, 11111);
  122. // 限制历史记录数量
  123. if (this.historyList.length > MAX_HISTORY) {
  124. this.historyList = this.historyList.slice(0, MAX_HISTORY);
  125. }
  126. uni.setStorageSync(HISTORY_KEY, JSON.stringify(this.historyList));
  127. }
  128. } catch (e) {
  129. console.error('Failed to save search history:', e);
  130. }
  131. },
  132. // 清空历史记录
  133. clearHistory() {
  134. this.$refs['DialogBox'].confirm({
  135. title: '提示',
  136. content: '确定要清空搜索历史吗?',
  137. DialogType: 'inquiry',
  138. btn1: '否',
  139. btn2: '是',
  140. animation: 0
  141. }).then((res) => {
  142. if (res.isConfirm) {
  143. this.historyList = [];
  144. uni.setStorageSync(HISTORY_KEY, '[]');
  145. }
  146. })
  147. },
  148. // 清空单个历史记录
  149. deleteHistoryItem(item) {
  150. console.log(item, '删除历史记录');
  151. this.$refs['DialogBox'].confirm({
  152. title: '提示',
  153. content: '确定要删除搜索历史吗?',
  154. DialogType: 'inquiry',
  155. btn1: '否',
  156. btn2: '是',
  157. animation: 0
  158. }).then((res) => {
  159. if (res.isConfirm) {
  160. const index = this.historyList.indexOf(item);
  161. if (index !== -1) {
  162. this.historyList.splice(index, 1);
  163. }
  164. uni.setStorageSync(HISTORY_KEY, JSON.stringify(this.historyList));
  165. }
  166. })
  167. },
  168. // 使用历史记录中的关键词
  169. useHistoryKeyword(keyword) {
  170. this.searchKeyword = keyword;
  171. this.onSearch();
  172. },
  173. // 处理搜索
  174. async onSearch(isRefresh = true) {
  175. if (!this.searchKeyword.trim()) return;
  176. if (isRefresh) {
  177. this.page = 1;
  178. this.hasMore = true;
  179. this.searchResult = [];
  180. }
  181. if (!this.hasMore || this.isLoading) return;
  182. this.isLoading = true;
  183. try {
  184. const [err, res] = await uni.request({
  185. url: this.$apiHost + '/crowdfund/list',
  186. method: 'GET',
  187. data: {
  188. page: this.page,
  189. pageSize: this.pageSize,
  190. keyword: this.searchKeyword
  191. }
  192. });
  193. if (!err && res && res.data && res.data.data && res.data.data.list) {
  194. const list = res.data.data.list;
  195. if (isRefresh) {
  196. this.searchResult = list;
  197. } else {
  198. this.searchResult = [...this.searchResult, ...list];
  199. }
  200. this.hasMore = this.searchResult.length < (res.data.data.total || 0);
  201. this.page++;
  202. } else {
  203. this.hasMore = false;
  204. }
  205. } catch (e) {
  206. this.hasMore = false;
  207. }
  208. this.isLoading = false;
  209. // 保存历史记录
  210. this.saveHistory();
  211. this.searchStatus = false;
  212. },
  213. // 清空搜索框
  214. clearKeyword() {
  215. this.searchKeyword = '';
  216. },
  217. // 处理输入
  218. handleInput(e) {
  219. this.searchKeyword = e.detail.value;
  220. if (this.searchKeyword.trim() == '') {
  221. this.searchStatus = true;
  222. }
  223. console.log(this.historyList, e.detail.value);
  224. },
  225. // 返回上一页
  226. goBack() {
  227. uni.navigateBack();
  228. },
  229. // 添加展开/折叠方法
  230. toggleHistory() {
  231. this.isExpanded = !this.isExpanded;
  232. },
  233. goToDetail(id) {
  234. uni.navigateTo({ url: '/pages/crowdFunding/crowdfundingDetails?id=' + id });
  235. },
  236. handleSearch() {
  237. if (!this.searchKeyword.trim()) return;
  238. // 保存到历史记录
  239. this.saveHistory();
  240. this.searchStatus = false;
  241. // 加载搜索结果
  242. this.queryList();
  243. },
  244. }
  245. }
  246. </script>
  247. <style lang="scss">
  248. .search-container {
  249. background-color: #f2f6f2;
  250. min-height: 100vh;
  251. width: 100%;
  252. overflow-x: hidden;
  253. position: relative;
  254. .search-header {
  255. background-color: #ffffff;
  256. padding: 16rpx 30rpx;
  257. display: flex;
  258. align-items: center;
  259. padding-top: calc(16rpx + var(--status-bar-height));
  260. padding-left: 40rpx;
  261. padding-right: 28rpx;
  262. padding-bottom: 16rpx;
  263. position: fixed;
  264. left: 0;
  265. top: 0;
  266. z-index: 9;
  267. width: 100%;
  268. box-sizing: border-box;
  269. .search-box {
  270. flex: 1;
  271. height: 72rpx;
  272. background-color: #f1f1f1;
  273. border-radius: 36rpx;
  274. display: flex;
  275. align-items: center;
  276. padding: 0 24rpx;
  277. padding-left: 14rpx;
  278. width: 622rpx;
  279. height: 72rpx;
  280. background: #FFFFFF;
  281. border-radius: 36rpx;
  282. border: 6rpx solid #000000;
  283. position: relative;
  284. left: 0;
  285. top: 0;
  286. box-sizing: border-box;
  287. max-width: calc(100% - 60rpx);
  288. input {
  289. flex: 1;
  290. height: 100%;
  291. margin: 0 16rpx;
  292. font-size: 24rpx;
  293. font-family: 'PingFang SC-Medium';
  294. width: auto;
  295. min-width: 0;
  296. }
  297. .searchImgBox {
  298. width: 88rpx;
  299. height: 56rpx;
  300. background: #1F1F1F;
  301. border-radius: 32rpx;
  302. display: flex;
  303. align-items: center;
  304. justify-content: center;
  305. position: absolute;
  306. top: 50%;
  307. right: 6rpx;
  308. transform: translateY(-50%);
  309. flex-shrink: 0;
  310. .image {
  311. width: 36rpx;
  312. height: 36rpx;
  313. }
  314. }
  315. }
  316. .cancel-btn {
  317. color: #1f1f1f;
  318. width: 36rpx;
  319. height: 100%;
  320. margin-right: 24rpx;
  321. font-weight: 700;
  322. flex-shrink: 0;
  323. }
  324. }
  325. .reserveASeat {
  326. width: 100%;
  327. height: calc(108rpx + var(--status-bar-height));
  328. }
  329. .search-history {
  330. background-color: #ffffff;
  331. padding: 30rpx;
  332. padding-top: 15rpx;
  333. width: 100%;
  334. box-sizing: border-box;
  335. .history-header {
  336. display: flex;
  337. justify-content: space-between;
  338. align-items: center;
  339. margin-bottom: 15rpx;
  340. width: 100%;
  341. box-sizing: border-box;
  342. .title {
  343. font-size: 30rpx;
  344. color: #333;
  345. font-weight: bold;
  346. }
  347. }
  348. .history-list {
  349. transition: all 1s;
  350. overflow: hidden;
  351. width: 100%;
  352. box-sizing: border-box;
  353. .history-item {
  354. display: flex;
  355. align-items: center;
  356. padding: 18rpx 0;
  357. justify-content: space-between;
  358. width: 100%;
  359. box-sizing: border-box;
  360. .history-text {
  361. font-size: 28rpx;
  362. color: #666;
  363. display: flex;
  364. align-items: center;
  365. justify-content: flex-start;
  366. font-weight: 400;
  367. color: #1F1F1F;
  368. margin-left: 6rpx;
  369. font-family: 'PingFang SC-Bold';
  370. flex: 1;
  371. overflow: hidden;
  372. min-width: 0;
  373. image {
  374. width: 32rpx;
  375. height: 32rpx;
  376. margin-right: 16rpx;
  377. flex-shrink: 0;
  378. }
  379. }
  380. .deleteBtn {
  381. width: 30rpx;
  382. height: 30rpx;
  383. flex-shrink: 0;
  384. }
  385. }
  386. }
  387. }
  388. .search-result {
  389. // background-color: #ffffff;
  390. margin-top: 20rpx;
  391. min-height: 200rpx;
  392. width: 100%;
  393. box-sizing: border-box;
  394. }
  395. .no-data {
  396. display: flex;
  397. flex-direction: column;
  398. align-items: center;
  399. justify-content: center;
  400. padding: 60rpx 0;
  401. // background-color: #fff;
  402. width: 100%;
  403. box-sizing: border-box;
  404. text {
  405. color: #808080;
  406. font-size: 28rpx;
  407. }
  408. }
  409. }
  410. .expandBtn {
  411. view {
  412. font-family: 'PingFang SC-Bold';
  413. color: #999999;
  414. display: flex;
  415. align-items: center;
  416. justify-content: center;
  417. &.fold {
  418. image {
  419. transform: rotate(180deg) translateY(-2rpx);
  420. }
  421. }
  422. image {
  423. width: 28rpx;
  424. height: 28rpx;
  425. }
  426. }
  427. }
  428. .items-grid {
  429. display: grid;
  430. grid-template-columns: repeat(2, 1fr);
  431. gap: 24rpx 12rpx;
  432. padding: 16rpx 12rpx 0 12rpx;
  433. background: #f2f6f2;
  434. }
  435. .loading-more { text-align: center; color: #999; font-size: 26rpx; padding: 24rpx 0; }
  436. .no-more { text-align: center; color: #ccc; font-size: 24rpx; padding: 20rpx 0; }
  437. </style>