bookshelf.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <template>
  2. <view class="container">
  3. <!-- 搜索栏 -->
  4. <view class="search-bar">
  5. <input class="search-input" placeholder="搜索书架" />
  6. <text class="search-icon">🔍</text>
  7. </view>
  8. <scroll-view class="scroll-content" scroll-y>
  9. <!-- 我的书架 -->
  10. <view class="section" v-if="userInfo">
  11. <view class="section-header">
  12. <text class="section-title">我的书架</text>
  13. <text class="book-count">共{{ allItems.length }}本</text>
  14. </view>
  15. <view class="book-grid" v-if="allItems.length > 0">
  16. <view class="book-item" v-for="(item, index) in allItems" :key="item.id || index" @click="goToDetail(item)">
  17. <image class="book-cover" :src="item.image" mode="aspectFill" :lazy-load="true"></image>
  18. <text class="book-name">{{ item.title }}</text>
  19. <text class="book-progress" v-if="item.type === 'book'">{{ item.progress }}%</text>
  20. <text class="book-progress" v-else-if="item.type === 'audiobook'">{{ item.progressText || '继续收听' }}</text>
  21. <view class="type-badge" v-if="item.type === 'audiobook'">
  22. <text class="type-badge-text">听书</text>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="empty-state" v-else>
  27. <text class="empty-text">书架空空如也,快去添加书籍吧~</text>
  28. </view>
  29. </view>
  30. <!-- 最近阅读/收听 -->
  31. <view class="section" v-if="userInfo && recentItems.length > 0">
  32. <view class="section-header">
  33. <text class="section-title">最近阅读</text>
  34. </view>
  35. <view class="book-list">
  36. <view class="book-list-item" v-for="(item, index) in recentItems" :key="item.id || index" @click="goToDetail(item)">
  37. <image class="book-cover-small" :src="item.image" mode="aspectFill" :lazy-load="true"></image>
  38. <view class="book-info">
  39. <view class="book-title-row">
  40. <text class="book-title">{{ item.title }}</text>
  41. <text class="type-tag" v-if="item.type === 'audiobook'">听书</text>
  42. </view>
  43. <text class="book-author">{{ item.author }}</text>
  44. <text class="book-progress-text" v-if="item.type === 'book'">阅读进度:{{ item.progress }}%</text>
  45. <text class="book-progress-text" v-else-if="item.type === 'audiobook'">{{ item.progressText || '继续收听' }}</text>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 未登录提示 -->
  51. <view class="empty-state" v-if="!userInfo">
  52. <text class="empty-text">请先登录查看书架</text>
  53. <button class="login-btn" @click="goToLogin">去登录</button>
  54. </view>
  55. </scroll-view>
  56. </view>
  57. </template>
  58. <script>
  59. import { getBookshelfList, getAudiobookBookshelfList } from '../../utils/api.js'
  60. export default {
  61. data() {
  62. return {
  63. books: [],
  64. audiobooks: [],
  65. allItems: [],
  66. recentItems: [],
  67. userInfo: null,
  68. isLoading: false
  69. }
  70. },
  71. onLoad() {
  72. // 获取用户信息
  73. this.loadUserInfo()
  74. },
  75. onShow() {
  76. // 页面显示时重新加载数据
  77. this.loadUserInfo()
  78. },
  79. methods: {
  80. loadUserInfo() {
  81. try {
  82. const userInfo = uni.getStorageSync('userInfo')
  83. const isLogin = uni.getStorageSync('isLogin')
  84. if (userInfo && userInfo.id && isLogin) {
  85. this.userInfo = userInfo
  86. this.loadBookshelfList()
  87. } else {
  88. // 未登录,清空数据
  89. this.books = []
  90. this.audiobooks = []
  91. this.allItems = []
  92. this.recentItems = []
  93. this.userInfo = null
  94. }
  95. } catch (e) {
  96. console.error('获取用户信息失败', e)
  97. this.books = []
  98. this.audiobooks = []
  99. this.allItems = []
  100. this.recentItems = []
  101. this.userInfo = null
  102. }
  103. },
  104. async loadBookshelfList() {
  105. if (!this.userInfo || !this.userInfo.id) {
  106. return
  107. }
  108. this.isLoading = true
  109. uni.showLoading({
  110. title: '加载中...',
  111. mask: false
  112. })
  113. try {
  114. // 同时加载书籍书架和听书书架
  115. const [bookRes, audiobookRes] = await Promise.all([
  116. getBookshelfList(this.userInfo.id).catch(err => {
  117. console.error('获取书籍书架失败:', err)
  118. return { code: 200, data: [] }
  119. }),
  120. getAudiobookBookshelfList(this.userInfo.id).catch(err => {
  121. console.error('获取听书书架失败:', err)
  122. return { code: 200, data: [] }
  123. })
  124. ])
  125. uni.hideLoading()
  126. this.isLoading = false
  127. // 处理书籍数据
  128. if (bookRes.code === 200 && bookRes.data) {
  129. this.books = bookRes.data.map((item) => {
  130. const book = item.book || {}
  131. return {
  132. id: book.id || item.bookId,
  133. title: book.title || '未知书籍',
  134. author: book.author || '',
  135. image: book.image || book.cover || 'https://picsum.photos/seed/default/200/300',
  136. progress: item.readProgress || 0,
  137. type: 'book',
  138. lastReadTime: item.lastReadTime,
  139. addedAt: item.addedAt
  140. }
  141. })
  142. } else {
  143. this.books = []
  144. }
  145. // 处理听书数据
  146. if (audiobookRes.code === 200 && audiobookRes.data) {
  147. this.audiobooks = audiobookRes.data.map((item) => {
  148. const audiobook = item.audiobook || {}
  149. return {
  150. id: audiobook.id || item.audiobookId,
  151. title: audiobook.title || '未知听书',
  152. author: audiobook.author || '',
  153. narrator: audiobook.narrator || '',
  154. image: audiobook.image || audiobook.cover || 'https://picsum.photos/seed/default/200/300',
  155. progress: 0,
  156. progressText: item.lastListenedChapterId ? '继续收听' : '开始收听',
  157. type: 'audiobook',
  158. lastListenedChapterId: item.lastListenedChapterId,
  159. lastListenedPosition: item.lastListenedPosition,
  160. addedAt: item.addedAt,
  161. updatedAt: item.updatedAt
  162. }
  163. })
  164. } else {
  165. this.audiobooks = []
  166. }
  167. // 合并所有项目,按添加时间排序(最新的在前)
  168. this.allItems = [...this.books, ...this.audiobooks].sort((a, b) => {
  169. const timeA = a.addedAt ? new Date(a.addedAt).getTime() : 0
  170. const timeB = b.addedAt ? new Date(b.addedAt).getTime() : 0
  171. return timeB - timeA
  172. })
  173. // 最近阅读/收听(按最后阅读/收听时间排序,取前3本)
  174. this.recentItems = [...this.books, ...this.audiobooks]
  175. .filter((item) => {
  176. if (item.type === 'book') {
  177. return item.lastReadTime != null
  178. } else if (item.type === 'audiobook') {
  179. return item.lastListenedChapterId != null || item.updatedAt != null
  180. }
  181. return false
  182. })
  183. .sort((a, b) => {
  184. let timeA = 0
  185. let timeB = 0
  186. if (a.type === 'book' && a.lastReadTime) {
  187. timeA = new Date(a.lastReadTime).getTime()
  188. } else if (a.type === 'audiobook' && a.updatedAt) {
  189. timeA = new Date(a.updatedAt).getTime()
  190. }
  191. if (b.type === 'book' && b.lastReadTime) {
  192. timeB = new Date(b.lastReadTime).getTime()
  193. } else if (b.type === 'audiobook' && b.updatedAt) {
  194. timeB = new Date(b.updatedAt).getTime()
  195. }
  196. return timeB - timeA
  197. })
  198. .slice(0, 3)
  199. } catch (err) {
  200. uni.hideLoading()
  201. this.isLoading = false
  202. console.error('获取书架列表失败:', err)
  203. uni.showToast({
  204. title: '加载失败',
  205. icon: 'none'
  206. })
  207. this.books = []
  208. this.audiobooks = []
  209. this.allItems = []
  210. this.recentItems = []
  211. }
  212. },
  213. goToDetail(item) {
  214. if (!item || !item.id) {
  215. uni.showToast({
  216. title: '信息不完整',
  217. icon: 'none'
  218. })
  219. return
  220. }
  221. if (item.type === 'book') {
  222. // 跳转到书籍详情页
  223. if (!item.id) {
  224. uni.showToast({
  225. title: '书籍信息不完整',
  226. icon: 'none'
  227. })
  228. return
  229. }
  230. uni.navigateTo({
  231. url: `/pages/book-detail/book-detail?bookId=${item.id}`
  232. })
  233. } else if (item.type === 'audiobook') {
  234. // 跳转到听书详情页
  235. if (!item.id) {
  236. uni.showToast({
  237. title: '听书信息不完整',
  238. icon: 'none'
  239. })
  240. return
  241. }
  242. uni.navigateTo({
  243. url: `/pages/listen-detail/listen-detail?audiobookId=${item.id}`
  244. })
  245. }
  246. },
  247. goToLogin() {
  248. uni.navigateTo({
  249. url: '/pages/login/login'
  250. })
  251. }
  252. }
  253. }
  254. </script>
  255. <style scoped>
  256. .container {
  257. width: 100%;
  258. height: 100vh;
  259. background-color: #FFFFFF;
  260. display: flex;
  261. flex-direction: column;
  262. padding-top: 60px;
  263. box-sizing: border-box;
  264. }
  265. .search-bar {
  266. position: relative;
  267. padding: 20rpx 30rpx;
  268. background-color: #FFFFFF;
  269. }
  270. .search-input {
  271. width: 100%;
  272. height: 70rpx;
  273. background-color: #F5F5F5;
  274. border-radius: 35rpx;
  275. padding: 0 80rpx 0 30rpx;
  276. font-size: 28rpx;
  277. color: #333333;
  278. }
  279. .search-icon {
  280. position: absolute;
  281. right: 50rpx;
  282. top: 50%;
  283. transform: translateY(-50%);
  284. font-size: 32rpx;
  285. }
  286. .scroll-content {
  287. flex: 1;
  288. width: 100%;
  289. padding-bottom: 20rpx;
  290. }
  291. .section {
  292. padding: 40rpx 30rpx;
  293. background-color: #FFFFFF;
  294. }
  295. .section-header {
  296. display: flex;
  297. justify-content: space-between;
  298. align-items: center;
  299. margin-bottom: 30rpx;
  300. }
  301. .section-title {
  302. font-size: 36rpx;
  303. font-weight: bold;
  304. color: #333333;
  305. }
  306. .book-count {
  307. font-size: 28rpx;
  308. color: #999999;
  309. }
  310. .book-grid {
  311. display: flex;
  312. flex-wrap: wrap;
  313. justify-content: space-between;
  314. }
  315. .book-item {
  316. width: 160rpx;
  317. margin-bottom: 30rpx;
  318. }
  319. .book-cover {
  320. width: 160rpx;
  321. height: 220rpx;
  322. border-radius: 8rpx;
  323. margin-bottom: 15rpx;
  324. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
  325. background-color: #F5F5F5;
  326. }
  327. .book-name {
  328. font-size: 24rpx;
  329. color: #333333;
  330. display: block;
  331. overflow: hidden;
  332. text-overflow: ellipsis;
  333. white-space: nowrap;
  334. margin-bottom: 8rpx;
  335. }
  336. .book-progress {
  337. font-size: 22rpx;
  338. color: #4FC3F7;
  339. }
  340. .book-item {
  341. position: relative;
  342. }
  343. .type-badge {
  344. position: absolute;
  345. top: 8rpx;
  346. right: 8rpx;
  347. background-color: rgba(79, 195, 247, 0.9);
  348. border-radius: 8rpx;
  349. padding: 4rpx 12rpx;
  350. }
  351. .type-badge-text {
  352. font-size: 20rpx;
  353. color: #FFFFFF;
  354. }
  355. .book-list {
  356. display: flex;
  357. flex-direction: column;
  358. }
  359. .book-list-item {
  360. display: flex;
  361. margin-bottom: 30rpx;
  362. }
  363. .book-cover-small {
  364. width: 120rpx;
  365. height: 160rpx;
  366. border-radius: 8rpx;
  367. margin-right: 20rpx;
  368. flex-shrink: 0;
  369. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
  370. background-color: #F5F5F5;
  371. }
  372. .book-info {
  373. flex: 1;
  374. display: flex;
  375. flex-direction: column;
  376. justify-content: space-between;
  377. }
  378. .book-title-row {
  379. display: flex;
  380. align-items: center;
  381. margin-bottom: 15rpx;
  382. }
  383. .book-title {
  384. font-size: 32rpx;
  385. font-weight: bold;
  386. color: #333333;
  387. margin-right: 15rpx;
  388. flex: 1;
  389. }
  390. .type-tag {
  391. font-size: 22rpx;
  392. color: #4FC3F7;
  393. background-color: #E3F2FD;
  394. padding: 4rpx 12rpx;
  395. border-radius: 8rpx;
  396. }
  397. .book-author {
  398. font-size: 26rpx;
  399. color: #666666;
  400. margin-bottom: 15rpx;
  401. }
  402. .book-progress-text {
  403. font-size: 24rpx;
  404. color: #4FC3F7;
  405. }
  406. .empty-state {
  407. padding: 100rpx 30rpx;
  408. text-align: center;
  409. }
  410. .empty-text {
  411. font-size: 28rpx;
  412. color: #999999;
  413. display: block;
  414. margin-bottom: 40rpx;
  415. }
  416. .login-btn {
  417. width: 200rpx;
  418. height: 80rpx;
  419. background-color: #4FC3F7;
  420. color: #FFFFFF;
  421. font-size: 30rpx;
  422. border: none;
  423. border-radius: 40rpx;
  424. display: flex;
  425. align-items: center;
  426. justify-content: center;
  427. margin: 0 auto;
  428. }
  429. </style>