index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. bannerList: [],
  8. categories: [
  9. { name: "全部分类", icon: "📄", color: "#81C784" },
  10. { name: "排行榜", icon: "👑", color: "#FFD54F" },
  11. { name: "热门书籍", icon: "🔥", color: "#E57373" },
  12. { name: "新书榜", icon: "📖", color: "#81C784" },
  13. { name: "VIP书籍", icon: "📚", color: "#4FC3F7" }
  14. ],
  15. todayRecommend: [],
  16. bestsellers: [],
  17. featuredList: [],
  18. moreRecommend: [],
  19. isLoading: false,
  20. featuredLoading: false,
  21. moreLoading: false
  22. };
  23. },
  24. onLoad() {
  25. this.loadBanners();
  26. this.loadTodayRecommend();
  27. this.loadBestsellers();
  28. this.loadFeaturedList();
  29. this.loadMoreRecommend();
  30. },
  31. onShow() {
  32. this.loadTodayRecommend();
  33. this.loadBestsellers();
  34. this.loadFeaturedList();
  35. this.loadMoreRecommend();
  36. },
  37. methods: {
  38. async loadBanners() {
  39. try {
  40. const res = await utils_api.getBannersByCode("home_banner");
  41. if (res && res.code === 200 && Array.isArray(res.data)) {
  42. this.bannerList = res.data.map((b) => ({
  43. image: b.image,
  44. title: b.title || "",
  45. targetType: b.targetType,
  46. targetId: b.targetId,
  47. link: b.link
  48. }));
  49. }
  50. } catch (e) {
  51. common_vendor.index.__f__("error", "at pages/index/index.vue:209", "加载轮播失败", e);
  52. }
  53. },
  54. // 加载今日推荐
  55. loadTodayRecommend() {
  56. common_vendor.index.__f__("log", "at pages/index/index.vue:214", "开始加载今日推荐...");
  57. this.isLoading = true;
  58. utils_api.getTodayRecommend(8).then((res) => {
  59. common_vendor.index.__f__("log", "at pages/index/index.vue:218", "今日推荐API响应:", res);
  60. this.isLoading = false;
  61. if (res && res.code === 200) {
  62. if (res.data && Array.isArray(res.data) && res.data.length > 0) {
  63. this.todayRecommend = res.data.map((book) => {
  64. return {
  65. id: book.id,
  66. title: book.title || "未知书名",
  67. image: book.image || book.cover || "https://picsum.photos/seed/default/200/300",
  68. author: book.author || ""
  69. };
  70. });
  71. common_vendor.index.__f__("log", "at pages/index/index.vue:234", "今日推荐数据加载成功,共", this.todayRecommend.length, "本");
  72. } else {
  73. common_vendor.index.__f__("warn", "at pages/index/index.vue:236", "今日推荐数据为空或格式不正确:", res.data);
  74. this.todayRecommend = [];
  75. }
  76. } else {
  77. common_vendor.index.__f__("warn", "at pages/index/index.vue:240", "今日推荐API返回错误:", res);
  78. this.todayRecommend = [];
  79. common_vendor.index.showToast({
  80. title: res.message || "获取推荐失败",
  81. icon: "none",
  82. duration: 2e3
  83. });
  84. }
  85. }).catch((err) => {
  86. this.isLoading = false;
  87. common_vendor.index.__f__("error", "at pages/index/index.vue:252", "获取今日推荐失败:", err);
  88. this.todayRecommend = [];
  89. common_vendor.index.showToast({
  90. title: err.message || "网络请求失败,请检查后端服务",
  91. icon: "none",
  92. duration: 3e3
  93. });
  94. });
  95. },
  96. // 加载畅销书籍
  97. loadBestsellers() {
  98. common_vendor.index.__f__("log", "at pages/index/index.vue:264", "开始加载畅销书籍...");
  99. this.isLoading = true;
  100. utils_api.getBestsellers(10).then((res) => {
  101. common_vendor.index.__f__("log", "at pages/index/index.vue:268", "畅销书籍API响应:", res);
  102. this.isLoading = false;
  103. if (res && res.code === 200) {
  104. if (res.data && Array.isArray(res.data) && res.data.length > 0) {
  105. this.bestsellers = res.data.map((book) => {
  106. return {
  107. id: book.id,
  108. title: book.title || "未知书名",
  109. desc: book.desc || book.brief || book.introduction || "",
  110. author: book.author || "",
  111. image: book.image || book.cover || "https://picsum.photos/seed/default/200/300"
  112. };
  113. });
  114. common_vendor.index.__f__("log", "at pages/index/index.vue:285", "畅销书籍数据加载成功,共", this.bestsellers.length, "本");
  115. } else {
  116. common_vendor.index.__f__("warn", "at pages/index/index.vue:287", "畅销书籍数据为空或格式不正确:", res.data);
  117. this.bestsellers = [];
  118. }
  119. } else {
  120. common_vendor.index.__f__("warn", "at pages/index/index.vue:291", "畅销书籍API返回错误:", res);
  121. this.bestsellers = [];
  122. common_vendor.index.showToast({
  123. title: res.message || "获取畅销书籍失败",
  124. icon: "none",
  125. duration: 2e3
  126. });
  127. }
  128. }).catch((err) => {
  129. this.isLoading = false;
  130. common_vendor.index.__f__("error", "at pages/index/index.vue:303", "获取畅销书籍失败:", err);
  131. this.bestsellers = [];
  132. common_vendor.index.showToast({
  133. title: err.message || "网络请求失败,请检查后端服务",
  134. icon: "none",
  135. duration: 3e3
  136. });
  137. });
  138. },
  139. // 加载精品书单
  140. loadFeaturedList() {
  141. common_vendor.index.__f__("log", "at pages/index/index.vue:315", "开始加载精品书单...");
  142. this.featuredLoading = true;
  143. utils_api.getFeaturedList(4).then((res) => {
  144. common_vendor.index.__f__("log", "at pages/index/index.vue:319", "精品书单API响应:", res);
  145. this.featuredLoading = false;
  146. if (res && res.code === 200) {
  147. if (res.data && Array.isArray(res.data) && res.data.length > 0) {
  148. this.featuredList = res.data.map((book) => {
  149. return {
  150. id: book.id,
  151. title: book.title || "未知书名",
  152. image: book.image || book.cover || "https://picsum.photos/seed/featured/200/300"
  153. };
  154. });
  155. common_vendor.index.__f__("log", "at pages/index/index.vue:330", "精品书单加载成功,共", this.featuredList.length, "本");
  156. } else {
  157. common_vendor.index.__f__("warn", "at pages/index/index.vue:332", "精品书单数据为空或格式不正确:", res.data);
  158. this.featuredList = [];
  159. }
  160. } else {
  161. common_vendor.index.__f__("warn", "at pages/index/index.vue:336", "精品书单API返回错误:", res);
  162. this.featuredList = [];
  163. common_vendor.index.showToast({
  164. title: res.message || "获取精品书单失败",
  165. icon: "none",
  166. duration: 2e3
  167. });
  168. }
  169. }).catch((err) => {
  170. this.featuredLoading = false;
  171. common_vendor.index.__f__("error", "at pages/index/index.vue:347", "获取精品书单失败:", err);
  172. this.featuredList = [];
  173. common_vendor.index.showToast({
  174. title: err.message || "网络请求失败,请检查后端服务",
  175. icon: "none",
  176. duration: 3e3
  177. });
  178. });
  179. },
  180. // 加载更多推荐书籍
  181. loadMoreRecommend() {
  182. common_vendor.index.__f__("log", "at pages/index/index.vue:358", "开始加载更多推荐书籍...");
  183. this.moreLoading = true;
  184. utils_api.getMoreRecommend(6).then((res) => {
  185. common_vendor.index.__f__("log", "at pages/index/index.vue:362", "更多推荐API响应:", res);
  186. this.moreLoading = false;
  187. if (res && res.code === 200) {
  188. if (res.data && Array.isArray(res.data) && res.data.length > 0) {
  189. this.moreRecommend = res.data.map((book) => {
  190. return {
  191. id: book.id,
  192. title: book.title || "未知书名",
  193. author: book.author || "",
  194. image: book.image || book.cover || "https://picsum.photos/seed/more/200/300"
  195. };
  196. });
  197. common_vendor.index.__f__("log", "at pages/index/index.vue:374", "更多推荐加载成功,共", this.moreRecommend.length, "本");
  198. } else {
  199. common_vendor.index.__f__("warn", "at pages/index/index.vue:376", "更多推荐数据为空或格式不正确:", res.data);
  200. this.moreRecommend = [];
  201. }
  202. } else {
  203. common_vendor.index.__f__("warn", "at pages/index/index.vue:380", "更多推荐API返回错误:", res);
  204. this.moreRecommend = [];
  205. common_vendor.index.showToast({
  206. title: res.message || "获取更多推荐失败",
  207. icon: "none",
  208. duration: 2e3
  209. });
  210. }
  211. }).catch((err) => {
  212. this.moreLoading = false;
  213. common_vendor.index.__f__("error", "at pages/index/index.vue:391", "获取更多推荐失败:", err);
  214. this.moreRecommend = [];
  215. common_vendor.index.showToast({
  216. title: err.message || "网络请求失败,请检查后端服务",
  217. icon: "none",
  218. duration: 3e3
  219. });
  220. });
  221. },
  222. handleCategoryClick(item, index) {
  223. if (index === 0) {
  224. common_vendor.index.navigateTo({
  225. url: "/pages/category/category"
  226. });
  227. } else if (index === 1) {
  228. common_vendor.index.navigateTo({
  229. url: "/pages/ranking/ranking"
  230. });
  231. } else if (index === 2) {
  232. common_vendor.index.navigateTo({
  233. url: "/pages/hot-books/hot-books"
  234. });
  235. } else if (index === 3) {
  236. common_vendor.index.navigateTo({
  237. url: "/pages/new-books/new-books"
  238. });
  239. } else if (index === 4) {
  240. common_vendor.index.navigateTo({
  241. url: "/pages/vip-books/vip-books"
  242. });
  243. }
  244. },
  245. goToBookDetail(book) {
  246. if (!book || !book.id) {
  247. common_vendor.index.showToast({
  248. title: "书籍信息不完整",
  249. icon: "none"
  250. });
  251. return;
  252. }
  253. common_vendor.index.navigateTo({
  254. url: `/pages/book-detail/book-detail?bookId=${book.id}`
  255. });
  256. },
  257. goToMoreBooks(type) {
  258. common_vendor.index.navigateTo({
  259. url: `/pages/more-books/more-books?type=${type}`
  260. });
  261. },
  262. goToSearch() {
  263. common_vendor.index.navigateTo({
  264. url: "/pages/search/search"
  265. });
  266. },
  267. handleBannerClick(item) {
  268. if (!item)
  269. return;
  270. common_vendor.index.__f__("log", "at pages/index/index.vue:447", "轮播图点击:", item);
  271. if (item.targetType === "book" && item.targetId) {
  272. this.goToBookDetail({ id: item.targetId });
  273. return;
  274. }
  275. if (item.targetType === "audiobook" && item.targetId) {
  276. common_vendor.index.navigateTo({ url: `/pages/listen-detail/listen-detail?audiobookId=${item.targetId}` });
  277. return;
  278. }
  279. if (item.targetType === "url" && item.link) {
  280. common_vendor.index.showModal({
  281. title: "提示",
  282. content: "此链接需要复制后在浏览器打开",
  283. confirmText: "复制链接",
  284. success: (res) => {
  285. if (res.confirm) {
  286. common_vendor.index.setClipboardData({
  287. data: item.link,
  288. success: () => {
  289. common_vendor.index.showToast({
  290. title: "链接已复制",
  291. icon: "success"
  292. });
  293. }
  294. });
  295. }
  296. }
  297. });
  298. return;
  299. }
  300. if (item.link && !item.targetType)
  301. ;
  302. },
  303. handleImageError(index) {
  304. const fallbackImages = [
  305. "https://picsum.photos/seed/library1/1080/600",
  306. "https://picsum.photos/seed/library2/1080/600",
  307. "https://picsum.photos/seed/library3/1080/600",
  308. "https://picsum.photos/seed/library4/1080/600",
  309. "https://picsum.photos/seed/library5/1080/600"
  310. ];
  311. if (this.bannerList[index]) {
  312. this.bannerList[index].image = fallbackImages[index] || fallbackImages[0];
  313. }
  314. },
  315. handleBookImageError(book, listType) {
  316. book.image = "https://picsum.photos/seed/default/200/300";
  317. }
  318. }
  319. };
  320. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  321. return common_vendor.e({
  322. a: common_vendor.o((...args) => $options.goToSearch && $options.goToSearch(...args)),
  323. b: common_vendor.f($data.bannerList, (item, index, i0) => {
  324. return {
  325. a: item.image,
  326. b: common_vendor.o(($event) => $options.handleImageError(index), index),
  327. c: common_vendor.t(item.title),
  328. d: index,
  329. e: common_vendor.o(($event) => $options.handleBannerClick(item), index)
  330. };
  331. }),
  332. c: common_vendor.f($data.categories, (item, index, i0) => {
  333. return {
  334. a: common_vendor.t(item.icon),
  335. b: item.color,
  336. c: common_vendor.t(item.name),
  337. d: index,
  338. e: common_vendor.o(($event) => $options.handleCategoryClick(item, index), index)
  339. };
  340. }),
  341. d: common_vendor.o(($event) => $options.goToMoreBooks("today")),
  342. e: $data.isLoading && $data.todayRecommend.length === 0
  343. }, $data.isLoading && $data.todayRecommend.length === 0 ? {} : $data.todayRecommend.length > 0 ? {
  344. g: common_vendor.f($data.todayRecommend, (book, index, i0) => {
  345. return {
  346. a: book.image,
  347. b: common_vendor.o(($event) => $options.handleBookImageError(book, "todayRecommend"), book.id || index),
  348. c: common_vendor.t(book.title),
  349. d: book.id || index,
  350. e: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
  351. };
  352. })
  353. } : {}, {
  354. f: $data.todayRecommend.length > 0,
  355. h: common_vendor.o(($event) => $options.goToMoreBooks("bestseller")),
  356. i: $data.isLoading && $data.bestsellers.length === 0
  357. }, $data.isLoading && $data.bestsellers.length === 0 ? {} : $data.bestsellers.length > 0 ? {
  358. k: common_vendor.f($data.bestsellers, (book, index, i0) => {
  359. return {
  360. a: book.image,
  361. b: common_vendor.o(($event) => $options.handleBookImageError(book, "bestsellers"), book.id || index),
  362. c: common_vendor.t(book.title),
  363. d: common_vendor.t(book.desc),
  364. e: common_vendor.t(book.author),
  365. f: book.id || index,
  366. g: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
  367. };
  368. })
  369. } : {}, {
  370. j: $data.bestsellers.length > 0,
  371. l: common_vendor.o(($event) => $options.goToMoreBooks("featured")),
  372. m: $data.featuredLoading
  373. }, $data.featuredLoading ? {} : $data.featuredList.length > 0 ? {
  374. o: common_vendor.f($data.featuredList, (book, index, i0) => {
  375. return {
  376. a: book.image,
  377. b: common_vendor.o(($event) => $options.handleBookImageError(book, "featuredList"), book.id || index),
  378. c: common_vendor.t(book.title),
  379. d: book.id || index,
  380. e: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
  381. };
  382. })
  383. } : {}, {
  384. n: $data.featuredList.length > 0,
  385. p: $data.moreLoading
  386. }, $data.moreLoading ? {} : $data.moreRecommend.length > 0 ? {
  387. r: common_vendor.f($data.moreRecommend, (book, index, i0) => {
  388. return {
  389. a: book.image,
  390. b: common_vendor.o(($event) => $options.handleBookImageError(book, "moreRecommend"), book.id || index),
  391. c: common_vendor.t(book.title),
  392. d: common_vendor.t(book.author),
  393. e: book.id || index,
  394. f: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
  395. };
  396. })
  397. } : {}, {
  398. q: $data.moreRecommend.length > 0
  399. });
  400. }
  401. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-1cf27b2a"]]);
  402. wx.createPage(MiniProgramPage);
  403. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/index/index.js.map