小说小程序前端代码仓库(小程序)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

591 lines
13 KiB

  1. <template>
  2. <!-- 书架页面 -->
  3. <view class="page">
  4. <!-- 头部标签切换 -->
  5. <view class="header">
  6. <view class="header-content">
  7. <view class="tab-container">
  8. <view class="tab" :class="{'active': activeTab === 'read'}" @click="switchTab('read')">阅读</view>
  9. <view class="tab" :class="{'active': activeTab === 'work'}" @click="switchTab('work')">作品</view>
  10. </view>
  11. </view>
  12. </view>
  13. <!-- 书籍列表 - 阅读模式 -->
  14. <view class="novel-grid" v-if="activeTab === 'read' && !isEditMode">
  15. <view class="novel-row" v-for="(row, rowIndex) in novelRows" :key="rowIndex">
  16. <view class="novel-item"
  17. v-for="(novel, index) in list"
  18. :key="novel.id"
  19. @click="toNovelDetail(novel.id)"
  20. @longpress="enterEditMode">
  21. <novel-item
  22. :book="novel"
  23. horizontal="true"
  24. :style="{ width: '220rpx' }">
  25. </novel-item>
  26. <view class="novel-tag" v-if="novel.tag">{{novel.tag}}</view>
  27. <view class="novel-original" v-if="novel.isOriginal">
  28. <text>原创</text>
  29. </view>
  30. </view>
  31. </view>
  32. <!-- 空状态提示 -->
  33. <view class="empty-works" v-if="list.length === 0">
  34. <text class="empty-text">你书架还没有书籍呢</text>
  35. <text class="empty-tips">去首页看看吧</text>
  36. </view>
  37. </view>
  38. <!-- 作品列表 - 作品模式 -->
  39. <view class="works-container" v-if="activeTab === 'work' && !isEditMode">
  40. <!-- 顶部创建区域 -->
  41. <view class="works-header">
  42. <new-work-item @click="createNewWork" @settings="toReaderSettings" />
  43. </view>
  44. <!-- 作品列表 -->
  45. <view class="works-content">
  46. <work-item
  47. v-for="work in list"
  48. :key="work.id"
  49. :work="work"
  50. @click="toWorkDetail(work.id)"
  51. @longpress="enterEditMode"
  52. />
  53. <!-- 空状态提示 -->
  54. <view class="empty-works" v-if="list.length === 0">
  55. <text class="empty-text">你还没有创建作品</text>
  56. <text class="empty-tips">点击左上角"+"创建你的第一部作品吧</text>
  57. </view>
  58. </view>
  59. </view>
  60. <!-- 编辑模式 - 阅读 -->
  61. <view class="novel-grid edit-mode" v-if="activeTab === 'read' && isEditMode">
  62. <view class="novel-row" v-for="(row, rowIndex) in novelRows" :key="rowIndex">
  63. <view class="novel-item"
  64. v-for="(novel, index) in list"
  65. :key="novel.id"
  66. @click="toggleSelect(novel, 'novel')">
  67. <view class="item-checkbox" v-if="selectedItems.includes(novel.id)">
  68. <view class="checkbox-inner">
  69. <uv-icon name="checkmark" size="28" color="#ffffff"></uv-icon>
  70. </view>
  71. </view>
  72. <view class="item-checkbox" v-else>
  73. <view class="checkbox-inner-no">
  74. </view>
  75. </view>
  76. <novel-item
  77. :book="novel"
  78. horizontal="true"
  79. :style="{ width: '220rpx', opacity: selectedItems.includes(novel.id) ? '0.8' : '1' }">
  80. </novel-item>
  81. <view class="novel-tag" v-if="novel.tag">{{novel.tag}}</view>
  82. <view class="novel-original" v-if="novel.isOriginal">
  83. <text>原创</text>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. <!-- 编辑模式 - 作品 -->
  89. <view class="works-container edit-mode" v-if="activeTab === 'work' && isEditMode">
  90. <view class="works-content">
  91. <view
  92. class="work-item-wrapper"
  93. v-for="work in list"
  94. :key="work.id"
  95. @click="toggleSelect(work, 'work')"
  96. >
  97. <work-item
  98. :work="work"
  99. :style="{ opacity: selectedItems.includes(work.id) ? '0.8' : '1' }"
  100. />
  101. </view>
  102. </view>
  103. </view>
  104. <!-- 底部操作栏 -->
  105. <view class="bottom-action-bar" v-if="isEditMode">
  106. <view class="action-item" @click="exitEditMode">
  107. <view class="action-icon">
  108. <uv-icon name="reload" size="40" color="#666"></uv-icon>
  109. </view>
  110. <text>取消</text>
  111. </view>
  112. <view class="action-item" @click="selectAll">
  113. <view class="action-icon">
  114. <uv-icon name="grid-fill" size="40" color="#ff9900"></uv-icon>
  115. </view>
  116. <text>全选</text>
  117. </view>
  118. <view class="action-item" @click="removeSelected">
  119. <view class="action-icon">
  120. <uv-icon name="trash-fill" size="40" color="#f56c6c"></uv-icon>
  121. </view>
  122. <text>{{activeTab === 'read' ? '移出书架' : '删除'}}</text>
  123. </view>
  124. </view>
  125. <tabber select="bookshelf" v-if="!isEditMode"/>
  126. </view>
  127. </template>
  128. <script>
  129. import tabber from '@/components/base/tabbar.vue'
  130. import novelItem from '@/components/novel/novelItem.vue'
  131. import workItem from '@/components/novel/workItem.vue'
  132. import newWorkItem from '@/components/novel/newWorkItem.vue'
  133. import mixinsList from '@/mixins/list.js'
  134. export default {
  135. mixins: [mixinsList],
  136. components : {
  137. tabber,
  138. novelItem,
  139. workItem,
  140. newWorkItem
  141. },
  142. computed : {
  143. // 将小说列表分成每行3个的二维数组
  144. novelRows() {
  145. const rows = [];
  146. const itemsPerRow = 3;
  147. for (let i = 0; i < this.list.length; i += itemsPerRow) {
  148. rows.push(this.list.slice(i, i + itemsPerRow));
  149. }
  150. return rows;
  151. }
  152. },
  153. data() {
  154. return {
  155. statusBarHeight: 0, // 状态栏高度
  156. navBarHeight: 0, // 导航栏高度
  157. activeTab: 'read',
  158. isEditMode: false,
  159. selectedItems: [], // 统一选中项
  160. mixinsListApi : '',
  161. apiMap : {
  162. read : 'getReadBookPage',
  163. work : 'getMyBookPage',
  164. // work : 'getMyBookPage',
  165. },
  166. }
  167. },
  168. onLoad() {
  169. // 检查是否需要切换到作品标签
  170. const activeTab = uni.getStorageSync('activeBookshelfTab')
  171. // if (activeTab === 'work') {
  172. // this.activeTab = 'work'
  173. // this.mixinsListApi = this.apiMap[tab]
  174. // uni.removeStorageSync('activeBookshelfTab')
  175. // }
  176. // 监听切换到作品标签的事件
  177. // uni.$on('switchToWork', () => {
  178. // this.activeTab = 'work'
  179. // })
  180. },
  181. onShow() {
  182. // this.isEditMode = false;
  183. // this.selectedItems = [];
  184. if(this.isLogin){
  185. this.mixinsListApi = this.apiMap[tab]
  186. }
  187. },
  188. onUnload() {
  189. // 移除事件监听
  190. uni.$off('switchToWork')
  191. },
  192. methods: {
  193. // 切换标签
  194. switchTab(tab) {
  195. this.activeTab = tab;
  196. if(this.isLogin){
  197. this.mixinsListApi = this.apiMap[tab]
  198. }
  199. this.list = []
  200. this.getData()
  201. // 退出编辑模式
  202. this.exitEditMode();
  203. },
  204. // 跳转到小说阅读页
  205. toNovelDetail(id) {
  206. uni.navigateTo({
  207. url: '/pages_order/novel/readnovels?id=' + id
  208. })
  209. },
  210. // 跳转到作品详情页
  211. toWorkDetail(id) {
  212. console.log(id);
  213. uni.navigateTo({
  214. url: '/pages/work/detail?id=' + id
  215. })
  216. },
  217. // 创建新作品
  218. createNewWork() {
  219. uni.navigateTo({
  220. url: '/pages_order/novel/createNovel'
  221. })
  222. },
  223. // 跳转到读者成就设置
  224. toReaderSettings() {
  225. uni.navigateTo({
  226. url: '/pages_order/novel/ReaderAchievement'
  227. })
  228. },
  229. // 进入编辑模式
  230. enterEditMode() {
  231. this.isEditMode = true;
  232. this.selectedItems = [];
  233. },
  234. // 退出编辑模式
  235. exitEditMode() {
  236. this.isEditMode = false;
  237. this.selectedItems = [];
  238. },
  239. // 切换选择状态
  240. toggleSelect(item, type) {
  241. const index = this.selectedItems.indexOf(item.id);
  242. if (index === -1) {
  243. this.selectedItems.push(item.id);
  244. } else {
  245. this.selectedItems.splice(index, 1);
  246. }
  247. },
  248. // 全选
  249. selectAll() {
  250. if (this.activeTab === 'read') {
  251. // 已经全选,则取消全选
  252. if (this.selectedItems.length === this.novels.length) {
  253. this.selectedItems = [];
  254. } else {
  255. // 全选所有小说
  256. this.selectedItems = this.novels.map(novel => novel.id);
  257. }
  258. } else {
  259. // 已经全选,则取消全选
  260. if (this.selectedItems.length === this.list.length) {
  261. this.selectedItems = [];
  262. } else {
  263. // 全选所有作品
  264. this.selectedItems = this.list.map(work => work.id);
  265. }
  266. }
  267. },
  268. // 移除选中的项目
  269. removeSelected() {
  270. if (this.selectedItems.length === 0) {
  271. uni.showToast({
  272. title: '请先选择项目',
  273. icon: 'none'
  274. });
  275. return;
  276. }
  277. const title = this.activeTab === 'read' ? '移出书架' : '删除作品';
  278. const content = this.activeTab === 'read'
  279. ? `确定要将选中的${this.selectedItems.length}本小说移出书架吗?`
  280. : `确定要删除选中的${this.selectedItems.length}部作品吗?`;
  281. uni.showModal({
  282. title: '提示',
  283. content: content,
  284. success: async (res) => {
  285. if (res.confirm) {
  286. if (this.activeTab === 'read') {
  287. // 移除选中的小说
  288. // this.novels = this.novels.filter(novel => !this.selectedItems.includes(novel.id));
  289. await this.$fetch('batchRemoveReadBook', {
  290. bookIds : this.selectedItems.join(',')
  291. })
  292. uni.showToast({
  293. title: '移除成功',
  294. icon: 'success'
  295. });
  296. this.getData()
  297. } else {
  298. // 删除选中的作品
  299. this.list = this.list.filter(work => !this.selectedItems.includes(work.id));
  300. // 保存更新后的作品列表
  301. uni.setStorageSync('list', this.list)
  302. uni.showToast({
  303. title: '删除成功',
  304. icon: 'success'
  305. });
  306. }
  307. this.selectedItems = [];
  308. // 如果没有数据了,退出编辑模式
  309. if ((this.activeTab === 'read' && this.list.length === 0) ||
  310. (this.activeTab === 'work' && this.list.length === 0)) {
  311. this.exitEditMode();
  312. }
  313. }
  314. }
  315. });
  316. },
  317. }
  318. }
  319. </script>
  320. <style scoped lang="scss">
  321. .page {
  322. background-color: #ffffff;
  323. min-height: 100vh;
  324. position: relative;
  325. padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
  326. box-sizing: border-box;
  327. }
  328. .header {
  329. display: flex;
  330. flex-direction: column;
  331. justify-content: flex-end;
  332. position: sticky;
  333. top: 0;
  334. z-index: 100;
  335. background-color: #ffffff;
  336. box-sizing: border-box;
  337. width: 100%;
  338. border-bottom: 1rpx solid #f5f5f5;
  339. padding-top: calc(var(--status-bar-height) + 20rpx);
  340. .header-content {
  341. display: flex;
  342. justify-content: center;
  343. align-items: center;
  344. padding: 20rpx 30rpx;
  345. padding-bottom: 24rpx;
  346. width: 100%;
  347. }
  348. .tab-container {
  349. display: flex;
  350. align-items: center;
  351. font-size: 34rpx;
  352. .tab {
  353. margin-right: 40rpx;
  354. color: #999;
  355. position: relative;
  356. padding: 10rpx 0;
  357. &.active {
  358. color: #000;
  359. font-weight: bold;
  360. font-size: 36rpx;
  361. &::after {
  362. content: '';
  363. position: absolute;
  364. bottom: 0;
  365. left: 50%;
  366. transform: translateX(-50%);
  367. width: 40rpx;
  368. height: 6rpx;
  369. background-color: #000;
  370. border-radius: 3rpx;
  371. }
  372. }
  373. }
  374. }
  375. .header-right {
  376. display: flex;
  377. align-items: center;
  378. .header-icon {
  379. margin-left: 30rpx;
  380. height: 80rpx;
  381. display: flex;
  382. align-items: center;
  383. justify-content: center;
  384. text {
  385. font-size: 28rpx;
  386. color: #333;
  387. }
  388. }
  389. }
  390. }
  391. .empty-works {
  392. width: 100%;
  393. padding: 100rpx 0;
  394. display: flex;
  395. flex-direction: column;
  396. align-items: center;
  397. justify-content: center;
  398. .empty-text {
  399. font-size: 32rpx;
  400. color: #666;
  401. margin-bottom: 20rpx;
  402. }
  403. .empty-tips {
  404. font-size: 28rpx;
  405. color: #999;
  406. }
  407. }
  408. .novel-grid {
  409. padding: 20rpx;
  410. padding-top: 30rpx;
  411. padding-bottom: env(safe-area-inset-bottom);
  412. box-sizing: border-box;
  413. .novel-row {
  414. display: flex;
  415. margin-bottom: 40rpx;
  416. .novel-item {
  417. width: 31%;
  418. position: relative;
  419. .novel-tag {
  420. position: absolute;
  421. top: 10rpx;
  422. right: 10rpx;
  423. background-color: rgba(0, 0, 0, 0.6);
  424. color: #fff;
  425. font-size: 20rpx;
  426. padding: 4rpx 10rpx;
  427. border-radius: 6rpx;
  428. z-index: 1;
  429. }
  430. .novel-original {
  431. position: absolute;
  432. top: 10rpx;
  433. right: 10rpx;
  434. background-color: #ff9900;
  435. color: #fff;
  436. font-size: 20rpx;
  437. padding: 4rpx 10rpx;
  438. border-radius: 6rpx;
  439. z-index: 1;
  440. }
  441. }
  442. }
  443. }
  444. /* 作品列表相关样式 */
  445. .works-container {
  446. padding: 30rpx;
  447. .works-header {
  448. margin-bottom: 30rpx;
  449. }
  450. .works-content {
  451. display: flex;
  452. flex-direction: column;
  453. .work-item-wrapper {
  454. position: relative;
  455. margin-bottom: 20rpx;
  456. }
  457. }
  458. }
  459. .bottom-action-bar {
  460. position: fixed;
  461. bottom: 0;
  462. left: 0;
  463. width: 100%;
  464. height: calc(120rpx + env(safe-area-inset-bottom));
  465. background-color: #fff;
  466. border-top: 1rpx solid #eee;
  467. display: flex;
  468. justify-content: space-around;
  469. align-items: center;
  470. z-index: 99;
  471. padding-bottom: env(safe-area-inset-bottom);
  472. .action-item {
  473. display: flex;
  474. flex-direction: column;
  475. align-items: center;
  476. justify-content: center;
  477. .action-icon {
  478. width: 80rpx;
  479. height: 80rpx;
  480. display: flex;
  481. align-items: center;
  482. justify-content: center;
  483. }
  484. text {
  485. font-size: 24rpx;
  486. color: #666;
  487. margin-top: 8rpx;
  488. }
  489. }
  490. }
  491. // 编辑模式公共样式
  492. .edit-mode {
  493. .item-checkbox {
  494. position: absolute;
  495. top: 10rpx;
  496. left: 10rpx;
  497. z-index: 10;
  498. background-color: rgba(255,255,255,0.8);
  499. border-radius: 50%;
  500. width: 40rpx;
  501. height: 40rpx;
  502. display: flex;
  503. align-items: center;
  504. justify-content: center;
  505. .checkbox-inner {
  506. width: 40rpx;
  507. height: 40rpx;
  508. background-color: #1989fa;
  509. border-radius: 50%;
  510. display: flex;
  511. align-items: center;
  512. justify-content: center;
  513. }
  514. .checkbox-inner-no {
  515. width: 40rpx;
  516. height: 40rpx;
  517. background-color: #fff;
  518. border-radius: 50%;
  519. display: flex;
  520. align-items: center;
  521. justify-content: center;
  522. }
  523. }
  524. }
  525. </style>