| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { getNovelSearch } from '../../api/api'
- import { goToBookDetail } from '../../utils/util'
- Page({
- data: {
- keyword: '',
- bookList: [],
- loading: false,
- hasMore: true,
- page: 1,
- size: 10
- },
- onLoad: function(options) {
- // 获取传入的关键词
- const keyword = options.keyword || '';
- this.setData({ keyword });
-
- // 如果有关键词,立即搜索
- if (keyword) {
- this.search();
- }
- },
- // 输入框内容变化
- handleInput: function(e) {
- this.setData({
- keyword: e.detail.value
- });
- },
- // 清空搜索内容
- clearSearch: function() {
- this.setData({
- keyword: '',
- bookList: []
- });
- },
- // 开始搜索
- search: function() {
- // 如果关键词为空,不执行搜索
- if (!this.data.keyword.trim()) {
- return;
- }
- // 重置搜索状态
- this.setData({
- page: 1,
- bookList: [],
- hasMore: true
- });
- // 执行搜索
- this.loadSearchResult();
- },
- // 加载搜索结果
- async loadSearchResult() {
- if (this.data.loading || !this.data.hasMore) return;
- this.setData({ loading: true });
-
- try {
- const params = {
- keyword: this.data.keyword,
- page: this.data.page,
- size: this.data.size
- };
- const result = await getNovelSearch(params);
-
- if (result && result.records) {
- // 处理搜索结果,标记匹配关键字的部分
- const processedList = this.processBookList(result.records);
-
- const newList = this.data.page === 1 ? processedList : [...this.data.bookList, ...processedList];
- const hasMore = !result.last;
-
- this.setData({
- bookList: newList,
- loading: false,
- hasMore: hasMore,
- page: hasMore ? this.data.page + 1 : this.data.page
- });
- } else {
- this.setData({
- loading: false,
- hasMore: false
- });
- }
- } catch (error) {
- console.error('搜索失败:', error);
- this.setData({
- loading: false,
- hasMore: false
- });
- wx.showToast({
- title: '搜索失败,请重试',
- icon: 'none'
- });
- }
- },
- // 处理搜索结果,标记匹配关键字的部分
- processBookList(books) {
- if (!this.data.keyword || !books || !books.length) return books;
-
- const keyword = this.data.keyword.trim();
-
- return books.map(book => {
- if (book.title && keyword) {
- // 添加标记,表示这本书的标题包含关键字
- book.titleContainsKeyword = book.title.toLowerCase().includes(keyword.toLowerCase());
-
- // 添加处理后的标题,用于在WXML中展示
- // 由于小程序不支持直接用rich-text高亮,我们标记出匹配部分在后续WXML中用view嵌套处理
- const titleLower = book.title.toLowerCase();
- const keywordLower = keyword.toLowerCase();
-
- if (book.titleContainsKeyword) {
- const index = titleLower.indexOf(keywordLower);
- if (index !== -1) {
- book.titleBeforeKeyword = book.title.substring(0, index);
- book.titleKeyword = book.title.substring(index, index + keyword.length);
- book.titleAfterKeyword = book.title.substring(index + keyword.length);
- }
- }
- }
- return book;
- });
- },
- // 监听滚动到底部
- onScrollToLower: function() {
- if (this.data.hasMore) {
- this.loadSearchResult();
- }
- },
- // 跳转到书籍详情
- handleBookTap: function(e) {
- const { id, wxbookid } = e.currentTarget.dataset;
- goToBookDetail({
- bookId: id,
- wxBookId: wxbookid
- });
- },
- // 返回上一页
- goBack: function() {
- wx.navigateBack({
- delta: 1
- });
- }
- })
|