| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const utils_api = require("../../utils/api.js");
- const _sfc_main = {
- data() {
- return {
- searchKeyword: "",
- isFocus: false,
- showSearchResults: false,
- searchHistory: [],
- popularSearches: [],
- searchResults: [],
- isLoading: false,
- currentPage: 1,
- pageSize: 20,
- hasMore: true,
- mode: "book",
- // 'book' or 'audio'
- userInfo: null
- };
- },
- onLoad(options) {
- if (options.mode === "audio") {
- this.mode = "audio";
- }
- try {
- const userInfo = common_vendor.index.getStorageSync("userInfo");
- if (userInfo && userInfo.id) {
- this.userInfo = userInfo;
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:150", "获取用户信息失败", e);
- }
- this.loadSearchHistory();
- this.loadPopularSearches();
- if (options.keyword) {
- this.searchKeyword = decodeURIComponent(options.keyword);
- this.performSearch(this.searchKeyword);
- }
- },
- methods: {
- handleInput(e) {
- this.searchKeyword = e.detail.value;
- },
- handleFocus() {
- this.isFocus = true;
- },
- handleBlur() {
- this.isFocus = false;
- },
- handleSearch() {
- if (this.searchKeyword.trim()) {
- this.performSearch(this.searchKeyword.trim());
- }
- },
- handleCancel() {
- if (this.showSearchResults) {
- this.showSearchResults = false;
- this.searchKeyword = "";
- this.searchResults = [];
- } else {
- common_vendor.index.navigateBack({
- delta: 1
- });
- }
- },
- searchByHistory(keyword) {
- this.searchKeyword = keyword;
- this.performSearch(keyword);
- },
- searchByTag(keyword) {
- this.searchKeyword = keyword;
- this.performSearch(keyword);
- },
- performSearch(keyword) {
- if (!keyword || !keyword.trim()) {
- return;
- }
- this.saveToHistory(keyword);
- this.showSearchResults = true;
- this.currentPage = 1;
- this.hasMore = true;
- this.searchDispatcher(keyword, 1);
- },
- async loadPopularSearches() {
- try {
- const res = await utils_api.getPopularKeywords(10);
- if (res && res.code === 200 && res.data && Array.isArray(res.data)) {
- this.popularSearches = res.data;
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:226", "加载热门搜索失败:", e);
- this.popularSearches = ["西游记", "三体", "大侦探", "窗边的小豆豆"];
- }
- },
- async searchDispatcher(keyword, page = 1) {
- if (this.mode === "audio") {
- await this.searchAudio(keyword, page);
- } else {
- await this.searchBook(keyword, page);
- }
- },
- async searchBook(keyword, page = 1) {
- if (!keyword || !keyword.trim()) {
- this.searchResults = [];
- return;
- }
- try {
- this.isLoading = true;
- const res = await utils_api.searchBooks(keyword.trim(), page, this.pageSize);
- if (res && res.code === 200 && res.data) {
- const pageResult = res.data;
- const books = pageResult.list || pageResult.data || [];
- const processed = books.map((b) => ({
- id: b.id,
- title: b.title || "",
- author: b.author || "未知作者",
- desc: b.desc || b.brief || b.introduction || "",
- cover: b.cover || b.image || "",
- image: b.image || b.cover || ""
- }));
- if (page === 1)
- this.searchResults = processed;
- else
- this.searchResults = this.searchResults.concat(processed);
- const total = pageResult.total || 0;
- this.hasMore = this.searchResults.length < total;
- this.currentPage = page;
- } else {
- if (page === 1)
- this.searchResults = [];
- common_vendor.index.showToast({ title: res.message || "搜索失败", icon: "none" });
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:270", "搜索书籍失败:", e);
- if (page === 1)
- this.searchResults = [];
- common_vendor.index.showToast({ title: e.message || "搜索失败,请重试", icon: "none" });
- } finally {
- this.isLoading = false;
- }
- },
- async searchAudio(keyword, page = 1) {
- if (!keyword || !keyword.trim()) {
- this.searchResults = [];
- return;
- }
- try {
- this.isLoading = true;
- const res = await utils_api.searchAudiobooks({ keyword: keyword.trim(), page, size: this.pageSize });
- if (res && res.code === 200 && res.data) {
- const pageResult = res.data;
- const list = pageResult.list || [];
- const processed = list.map((a) => ({
- id: a.id,
- title: a.title || "",
- author: a.narrator || a.author || "未知主播",
- desc: a.brief || a.desc || "",
- cover: a.image || a.cover || "",
- image: a.image || a.cover || "",
- isAudio: true
- }));
- if (page === 1)
- this.searchResults = processed;
- else
- this.searchResults = this.searchResults.concat(processed);
- const total = pageResult.total || 0;
- this.hasMore = this.searchResults.length < total;
- this.currentPage = page;
- } else {
- if (page === 1)
- this.searchResults = [];
- common_vendor.index.showToast({ title: res.message || "搜索失败", icon: "none" });
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:306", "搜索听书失败:", e);
- if (page === 1)
- this.searchResults = [];
- common_vendor.index.showToast({ title: e.message || "搜索失败,请重试", icon: "none" });
- } finally {
- this.isLoading = false;
- }
- },
- async saveToHistory(keyword) {
- if (!this.userInfo || !this.userInfo.id) {
- const index = this.searchHistory.findIndex(
- (item) => (typeof item === "string" ? item : item.keyword) === keyword
- );
- if (index > -1) {
- this.searchHistory.splice(index, 1);
- }
- this.searchHistory.unshift(keyword);
- if (this.searchHistory.length > 10) {
- this.searchHistory = this.searchHistory.slice(0, 10);
- }
- try {
- common_vendor.index.setStorageSync("searchHistory", this.searchHistory);
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:329", "保存搜索历史失败", e);
- }
- return;
- }
- try {
- await utils_api.recordSearchHistory(this.userInfo.id, keyword);
- await this.loadSearchHistory();
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:340", "保存搜索历史失败", e);
- }
- },
- async loadSearchHistory() {
- if (!this.userInfo || !this.userInfo.id) {
- try {
- const history = common_vendor.index.getStorageSync("searchHistory");
- if (history && Array.isArray(history)) {
- this.searchHistory = history.map(
- (item) => typeof item === "string" ? item : item.keyword
- );
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:354", "加载搜索历史失败", e);
- }
- return;
- }
- try {
- const res = await utils_api.getSearchHistory(this.userInfo.id, 10);
- if (res && res.code === 200 && res.data) {
- this.searchHistory = res.data.map((item) => item.keyword);
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:366", "加载搜索历史失败", e);
- try {
- const history = common_vendor.index.getStorageSync("searchHistory");
- if (history && Array.isArray(history)) {
- this.searchHistory = history.map(
- (item) => typeof item === "string" ? item : item.keyword
- );
- }
- } catch (err) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:376", "从本地存储加载搜索历史失败", err);
- }
- }
- },
- async clearHistory() {
- common_vendor.index.showModal({
- title: "提示",
- content: "确定要清空搜索历史吗?",
- success: async (res) => {
- if (res.confirm) {
- if (this.userInfo && this.userInfo.id) {
- try {
- common_vendor.index.showLoading({
- title: "清空中...",
- mask: true
- });
- const result = await utils_api.clearSearchHistory(this.userInfo.id);
- common_vendor.index.hideLoading();
- if (result && result.code === 200) {
- this.searchHistory = [];
- common_vendor.index.showToast({
- title: "已清空",
- icon: "success"
- });
- } else {
- common_vendor.index.showToast({
- title: result.message || "清空失败",
- icon: "none"
- });
- }
- } catch (e) {
- common_vendor.index.hideLoading();
- common_vendor.index.__f__("error", "at pages/search/search.vue:411", "清空搜索历史失败", e);
- common_vendor.index.showToast({
- title: "清空失败,请重试",
- icon: "none"
- });
- }
- } else {
- this.searchHistory = [];
- try {
- common_vendor.index.removeStorageSync("searchHistory");
- common_vendor.index.showToast({
- title: "已清空",
- icon: "success"
- });
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/search/search.vue:427", "清空搜索历史失败", e);
- }
- }
- }
- }
- });
- },
- loadMore() {
- if (!this.isLoading && this.hasMore && this.searchKeyword.trim()) {
- this.searchDispatcher(this.searchKeyword.trim(), this.currentPage + 1);
- }
- },
- goToBookDetail(book) {
- if (!book || !book.id) {
- common_vendor.index.showToast({ title: "信息不完整", icon: "none" });
- return;
- }
- if (this.mode === "audio" || book.isAudio) {
- common_vendor.index.navigateTo({ url: `/pages/listen-detail/listen-detail?audiobookId=${book.id}` });
- } else {
- common_vendor.index.navigateTo({ url: `/pages/book-detail/book-detail?bookId=${book.id}` });
- }
- },
- handleImageError(index) {
- if (this.searchResults[index]) {
- this.searchResults[index].cover = "https://via.placeholder.com/200x300?text=No+Image";
- this.searchResults[index].image = "https://via.placeholder.com/200x300?text=No+Image";
- }
- }
- }
- };
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: $data.isFocus,
- b: common_vendor.o([($event) => $data.searchKeyword = $event.detail.value, (...args) => $options.handleInput && $options.handleInput(...args)]),
- c: common_vendor.o((...args) => $options.handleSearch && $options.handleSearch(...args)),
- d: common_vendor.o((...args) => $options.handleFocus && $options.handleFocus(...args)),
- e: common_vendor.o((...args) => $options.handleBlur && $options.handleBlur(...args)),
- f: $data.searchKeyword,
- g: common_vendor.o((...args) => $options.handleCancel && $options.handleCancel(...args)),
- h: !$data.showSearchResults
- }, !$data.showSearchResults ? common_vendor.e({
- i: $data.searchHistory.length > 0
- }, $data.searchHistory.length > 0 ? {
- j: common_vendor.o((...args) => $options.clearHistory && $options.clearHistory(...args)),
- k: common_vendor.f($data.searchHistory, (item, index, i0) => {
- return {
- a: common_vendor.t(item),
- b: index,
- c: common_vendor.o(($event) => $options.searchByHistory(item), index)
- };
- })
- } : {}, {
- l: common_vendor.f($data.popularSearches, (item, index, i0) => {
- return {
- a: common_vendor.t(item),
- b: index,
- c: common_vendor.o(($event) => $options.searchByTag(item), index)
- };
- })
- }) : {}, {
- m: $data.showSearchResults
- }, $data.showSearchResults ? common_vendor.e({
- n: $data.searchResults.length > 0
- }, $data.searchResults.length > 0 ? {
- o: common_vendor.t($data.searchResults.length)
- } : {}, {
- p: $data.isLoading && $data.searchResults.length === 0
- }, $data.isLoading && $data.searchResults.length === 0 ? {} : $data.searchResults.length > 0 ? {
- r: common_vendor.f($data.searchResults, (book, index, i0) => {
- return common_vendor.e({
- a: book.cover || book.image,
- b: common_vendor.o(($event) => $options.handleImageError(index), book.id || index),
- c: common_vendor.t(book.title),
- d: book.desc
- }, book.desc ? {
- e: common_vendor.t(book.desc)
- } : {}, {
- f: book.author
- }, book.author ? {
- g: common_vendor.t(book.author)
- } : {}, {
- h: book.id || index,
- i: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
- });
- })
- } : !$data.isLoading ? {} : {}, {
- q: $data.searchResults.length > 0,
- s: !$data.isLoading,
- t: $data.isLoading && $data.searchResults.length > 0
- }, $data.isLoading && $data.searchResults.length > 0 ? {} : !$data.hasMore && $data.searchResults.length > 0 ? {} : {}, {
- v: !$data.hasMore && $data.searchResults.length > 0,
- w: common_vendor.o((...args) => $options.loadMore && $options.loadMore(...args))
- }) : {});
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-c10c040c"]]);
- wx.createPage(MiniProgramPage);
- //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/search/search.js.map
|