小说小程序前端代码仓库(小程序)
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.

681 lines
17 KiB

  1. <template>
  2. <!-- 书架页面 -->
  3. <view class="page">
  4. <!-- 头部标签切换 -->
  5. <view class="header" :style="{ paddingTop: `${statusBarHeight}px` }">
  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 row"
  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. </view>
  33. <!-- 作品列表 - 作品模式 -->
  34. <view class="works-container" v-if="activeTab === 'work' && !isEditMode">
  35. <!-- 顶部创建区域 -->
  36. <view class="works-header">
  37. <new-work-item @click="createNewWork" @settings="toReaderSettings" />
  38. </view>
  39. <!-- 作品列表 -->
  40. <view class="works-content">
  41. <work-item
  42. v-for="work in worksList"
  43. :key="work.id"
  44. :work="work"
  45. @click="toWorkDetail(work.id)"
  46. @longpress="enterEditMode"
  47. />
  48. <!-- 空状态提示 -->
  49. <view class="empty-works" v-if="worksList.length === 0">
  50. <text class="empty-text">你还没有创建作品</text>
  51. <text class="empty-tips">点击左上角"+"创建你的第一部作品吧</text>
  52. </view>
  53. </view>
  54. </view>
  55. <!-- 编辑模式 - 阅读 -->
  56. <view class="novel-grid edit-mode" v-if="activeTab === 'read' && isEditMode">
  57. <view class="novel-row" v-for="(row, rowIndex) in novelRows" :key="rowIndex">
  58. <view class="novel-item"
  59. v-for="(novel, index) in row"
  60. :key="novel.id"
  61. @click="toggleSelect(novel, 'novel')">
  62. <view class="item-checkbox" v-if="selectedItems.includes(novel.id)">
  63. <view class="checkbox-inner">
  64. <uv-icon name="checkmark" size="28" color="#ffffff"></uv-icon>
  65. </view>
  66. </view>
  67. <view class="item-checkbox" v-else>
  68. <view class="checkbox-inner-no">
  69. </view>
  70. </view>
  71. <novel-item
  72. :book="novel"
  73. horizontal="true"
  74. :style="{ width: '220rpx', opacity: selectedItems.includes(novel.id) ? '0.8' : '1' }">
  75. </novel-item>
  76. <view class="novel-tag" v-if="novel.tag">{{novel.tag}}</view>
  77. <view class="novel-original" v-if="novel.isOriginal">
  78. <text>原创</text>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. <!-- 编辑模式 - 作品 -->
  84. <view class="works-container edit-mode" v-if="activeTab === 'work' && isEditMode">
  85. <view class="works-content">
  86. <view
  87. class="work-item-wrapper"
  88. v-for="work in worksList"
  89. :key="work.id"
  90. @click="toggleSelect(work, 'work')"
  91. >
  92. <work-item
  93. :work="work"
  94. :style="{ opacity: selectedItems.includes(work.id) ? '0.8' : '1' }"
  95. />
  96. </view>
  97. </view>
  98. </view>
  99. <!-- 底部操作栏 -->
  100. <view class="bottom-action-bar" v-if="isEditMode">
  101. <view class="action-item" @click="exitEditMode">
  102. <view class="action-icon">
  103. <uv-icon name="reload" size="40" color="#666"></uv-icon>
  104. </view>
  105. <text>取消</text>
  106. </view>
  107. <view class="action-item" @click="selectAll">
  108. <view class="action-icon">
  109. <uv-icon name="grid-fill" size="40" color="#ff9900"></uv-icon>
  110. </view>
  111. <text>全选</text>
  112. </view>
  113. <view class="action-item" @click="removeSelected">
  114. <view class="action-icon">
  115. <uv-icon name="trash-fill" size="40" color="#f56c6c"></uv-icon>
  116. </view>
  117. <text>{{activeTab === 'read' ? '移出书架' : '删除'}}</text>
  118. </view>
  119. </view>
  120. <tabber select="bookshelf" v-if="!isEditMode"/>
  121. </view>
  122. </template>
  123. <script>
  124. import tabber from '@/components/base/tabbar.vue'
  125. import novelItem from '@/components/novel/novelItem.vue'
  126. import workItem from '@/components/novel/workItem.vue'
  127. import newWorkItem from '@/components/novel/newWorkItem.vue'
  128. import { mapGetters } from 'vuex'
  129. export default {
  130. components : {
  131. tabber,
  132. novelItem,
  133. workItem,
  134. newWorkItem
  135. },
  136. computed : {
  137. ...mapGetters(['userShop']),
  138. // 将小说列表分成每行3个的二维数组
  139. novelRows() {
  140. const rows = [];
  141. const itemsPerRow = 3;
  142. for (let i = 0; i < this.novels.length; i += itemsPerRow) {
  143. rows.push(this.novels.slice(i, i + itemsPerRow));
  144. }
  145. return rows;
  146. }
  147. },
  148. data() {
  149. return {
  150. statusBarHeight: 0, // 状态栏高度
  151. navBarHeight: 0, // 导航栏高度
  152. activeTab: 'read',
  153. isEditMode: false,
  154. selectedItems: [], // 统一选中项
  155. novels: [
  156. {
  157. id: '1',
  158. title: '我是半妖',
  159. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  160. author: '炎兰',
  161. desc: '都市玄幻小说,主角获得半妖化能力,通过吸收妖气不断变强...',
  162. tags: ['玄幻', '都市', '热血'],
  163. status: '连载中'
  164. },
  165. {
  166. id: '2',
  167. title: '兽王进化:从被小萝莉召唤开始',
  168. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  169. author: '九灵',
  170. desc: '一场意外让主角获得兽王血脉,开始了进化之路...',
  171. tags: ['奇幻', '冒险'],
  172. isOriginal: true,
  173. status: '连载中'
  174. },
  175. {
  176. id: '3',
  177. title: '魔法少女纯爷们',
  178. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  179. author: '烟火',
  180. desc: '一个普通男孩意外获得魔法少女的力量,开始了奇妙冒险...',
  181. tags: ['搞笑', '奇幻'],
  182. status: '已完结'
  183. },
  184. {
  185. id: '4',
  186. title: '我是一条小青龙',
  187. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  188. author: '东升',
  189. desc: '重生为一条小青龙,主角在修仙世界中成长的故事...',
  190. tags: ['仙侠', '修真'],
  191. tag: '独家',
  192. status: '连载中'
  193. },
  194. {
  195. id: '5',
  196. title: '女帝:别闹,朕怀孕了!',
  197. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  198. author: '君临',
  199. desc: '一代女帝意外穿越成了皇帝,却发现自己怀孕了...',
  200. tags: ['宫廷', '穿越'],
  201. isOriginal: true,
  202. status: '连载中'
  203. },
  204. {
  205. id: '6',
  206. title: '中国式应酬——应酬是门技术活',
  207. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  208. author: '商业顾问',
  209. desc: '一本教你如何在商业场合应对各种应酬的实用指南...',
  210. tags: ['商业', '实用'],
  211. status: '已完结'
  212. },
  213. {
  214. id: '7',
  215. title: '苏世民:我的经验与教训',
  216. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  217. author: '苏世民',
  218. desc: '黑石集团创始人苏世民的商业回忆录...',
  219. tags: ['传记', '商业'],
  220. status: '已完结'
  221. },
  222. {
  223. id: '8',
  224. title: '认知觉醒:开启自我改变的原动力',
  225. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  226. author: '周岭',
  227. desc: '帮助你打破思维局限,重塑认知结构的心理学著作...',
  228. tags: ['心理', '自助'],
  229. status: '已完结'
  230. },
  231. {
  232. id: '9',
  233. title: '纳瓦尔宝典',
  234. cover: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.iUyxJ_fxLjjX3kEBjteXWwAAAA?rs=1&pid=ImgDetMain',
  235. author: 'Naval',
  236. desc: '硅谷天使投资人纳瓦尔·拉维坎特的人生智慧...',
  237. tags: ['哲学', '投资'],
  238. status: '已完结'
  239. }
  240. ],
  241. // 作品列表数据
  242. worksList: [] // 清空初始数据,改为动态加载
  243. }
  244. },
  245. onLoad() {
  246. // 获取系统信息
  247. const systemInfo = uni.getSystemInfoSync();
  248. this.statusBarHeight = systemInfo.statusBarHeight;
  249. // 加载保存的作品列表
  250. this.loadWorksList()
  251. // 检查是否需要切换到作品标签
  252. const activeTab = uni.getStorageSync('activeBookshelfTab')
  253. if (activeTab === 'work') {
  254. this.activeTab = 'work'
  255. uni.removeStorageSync('activeBookshelfTab')
  256. }
  257. // 监听切换到作品标签的事件
  258. uni.$on('switchToWork', () => {
  259. this.activeTab = 'work'
  260. })
  261. // #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO
  262. const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  263. const navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
  264. this.navBarHeight = navBarHeight;
  265. // #endif
  266. },
  267. onShow() {
  268. // 重新加载作品列表
  269. this.loadWorksList()
  270. // 检查是否需要弹窗
  271. const pages = getCurrentPages();
  272. const current = pages[pages.length - 1];
  273. if (current.options && current.options.fromPublish === '1') {
  274. this.activeTab = 'work';
  275. uni.showToast({
  276. title: '发布成功',
  277. icon: 'success'
  278. });
  279. // 移除参数,防止返回时重复弹窗
  280. delete current.options.fromPublish;
  281. }
  282. this.isEditMode = false;
  283. this.selectedItems = [];
  284. },
  285. onUnload() {
  286. // 移除事件监听
  287. uni.$off('switchToWork')
  288. },
  289. methods: {
  290. // 切换标签
  291. switchTab(tab) {
  292. this.activeTab = tab;
  293. // 退出编辑模式
  294. this.exitEditMode();
  295. },
  296. // 跳转到小说详情页
  297. toNovelDetail(id) {
  298. uni.navigateTo({
  299. url: '/pages_order/book/bookDetail?id=' + id
  300. })
  301. },
  302. // 跳转到作品详情页
  303. toWorkDetail(id) {
  304. uni.navigateTo({
  305. url: '/pages/work/detail?id=' + id
  306. })
  307. },
  308. // 创建新作品
  309. createNewWork() {
  310. uni.navigateTo({
  311. url: '/pages_order/novel/createNovel'
  312. })
  313. },
  314. // 跳转到读者成就设置
  315. toReaderSettings() {
  316. uni.navigateTo({
  317. url: '/pages_order/novel/ReaderAchievement'
  318. })
  319. },
  320. // 进入编辑模式
  321. enterEditMode() {
  322. this.isEditMode = true;
  323. this.selectedItems = [];
  324. },
  325. // 退出编辑模式
  326. exitEditMode() {
  327. this.isEditMode = false;
  328. this.selectedItems = [];
  329. },
  330. // 切换选择状态
  331. toggleSelect(item, type) {
  332. const index = this.selectedItems.indexOf(item.id);
  333. if (index === -1) {
  334. this.selectedItems.push(item.id);
  335. } else {
  336. this.selectedItems.splice(index, 1);
  337. }
  338. },
  339. // 全选
  340. selectAll() {
  341. if (this.activeTab === 'read') {
  342. // 已经全选,则取消全选
  343. if (this.selectedItems.length === this.novels.length) {
  344. this.selectedItems = [];
  345. } else {
  346. // 全选所有小说
  347. this.selectedItems = this.novels.map(novel => novel.id);
  348. }
  349. } else {
  350. // 已经全选,则取消全选
  351. if (this.selectedItems.length === this.worksList.length) {
  352. this.selectedItems = [];
  353. } else {
  354. // 全选所有作品
  355. this.selectedItems = this.worksList.map(work => work.id);
  356. }
  357. }
  358. },
  359. // 移除选中的项目
  360. removeSelected() {
  361. if (this.selectedItems.length === 0) {
  362. uni.showToast({
  363. title: '请先选择项目',
  364. icon: 'none'
  365. });
  366. return;
  367. }
  368. const title = this.activeTab === 'read' ? '移出书架' : '删除作品';
  369. const content = this.activeTab === 'read'
  370. ? `确定要将选中的${this.selectedItems.length}本小说移出书架吗?`
  371. : `确定要删除选中的${this.selectedItems.length}部作品吗?`;
  372. uni.showModal({
  373. title: '提示',
  374. content: content,
  375. success: (res) => {
  376. if (res.confirm) {
  377. if (this.activeTab === 'read') {
  378. // 移除选中的小说
  379. this.novels = this.novels.filter(novel => !this.selectedItems.includes(novel.id));
  380. uni.showToast({
  381. title: '移除成功',
  382. icon: 'success'
  383. });
  384. } else {
  385. // 删除选中的作品
  386. this.worksList = this.worksList.filter(work => !this.selectedItems.includes(work.id));
  387. // 保存更新后的作品列表
  388. uni.setStorageSync('worksList', this.worksList)
  389. uni.showToast({
  390. title: '删除成功',
  391. icon: 'success'
  392. });
  393. }
  394. this.selectedItems = [];
  395. // 如果没有数据了,退出编辑模式
  396. if ((this.activeTab === 'read' && this.novels.length === 0) ||
  397. (this.activeTab === 'work' && this.worksList.length === 0)) {
  398. this.exitEditMode();
  399. }
  400. }
  401. }
  402. });
  403. },
  404. // 加载作品列表
  405. loadWorksList() {
  406. const savedWorks = uni.getStorageSync('worksList') || []
  407. this.worksList = savedWorks
  408. }
  409. }
  410. }
  411. </script>
  412. <style scoped lang="scss">
  413. .page {
  414. background-color: #ffffff;
  415. min-height: 100vh;
  416. position: relative;
  417. padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
  418. box-sizing: border-box;
  419. }
  420. .header {
  421. display: flex;
  422. flex-direction: column;
  423. justify-content: flex-end;
  424. position: sticky;
  425. top: 0;
  426. z-index: 100;
  427. background-color: #ffffff;
  428. box-sizing: border-box;
  429. width: 100%;
  430. border-bottom: 1rpx solid #f5f5f5;
  431. padding-top: constant(safe-area-inset-top); /* iOS 11.0 */
  432. padding-top: env(safe-area-inset-top); /* iOS 11.2+ */
  433. .header-content {
  434. display: flex;
  435. justify-content: center;
  436. align-items: center;
  437. padding: 20rpx 30rpx;
  438. padding-bottom: 24rpx;
  439. width: 100%;
  440. }
  441. .tab-container {
  442. display: flex;
  443. align-items: center;
  444. font-size: 34rpx;
  445. .tab {
  446. margin-right: 40rpx;
  447. color: #999;
  448. position: relative;
  449. padding: 10rpx 0;
  450. &.active {
  451. color: #000;
  452. font-weight: bold;
  453. font-size: 36rpx;
  454. &::after {
  455. content: '';
  456. position: absolute;
  457. bottom: 0;
  458. left: 50%;
  459. transform: translateX(-50%);
  460. width: 40rpx;
  461. height: 6rpx;
  462. background-color: #000;
  463. border-radius: 3rpx;
  464. }
  465. }
  466. }
  467. }
  468. .header-right {
  469. display: flex;
  470. align-items: center;
  471. .header-icon {
  472. margin-left: 30rpx;
  473. height: 80rpx;
  474. display: flex;
  475. align-items: center;
  476. justify-content: center;
  477. text {
  478. font-size: 28rpx;
  479. color: #333;
  480. }
  481. }
  482. }
  483. }
  484. .novel-grid {
  485. padding: 20rpx;
  486. padding-top: 30rpx;
  487. padding-bottom: env(safe-area-inset-bottom);
  488. box-sizing: border-box;
  489. .novel-row {
  490. display: flex;
  491. justify-content: space-between;
  492. margin-bottom: 40rpx;
  493. .novel-item {
  494. width: 31%;
  495. position: relative;
  496. .novel-tag {
  497. position: absolute;
  498. top: 10rpx;
  499. right: 10rpx;
  500. background-color: rgba(0, 0, 0, 0.6);
  501. color: #fff;
  502. font-size: 20rpx;
  503. padding: 4rpx 10rpx;
  504. border-radius: 6rpx;
  505. z-index: 1;
  506. }
  507. .novel-original {
  508. position: absolute;
  509. top: 10rpx;
  510. right: 10rpx;
  511. background-color: #ff9900;
  512. color: #fff;
  513. font-size: 20rpx;
  514. padding: 4rpx 10rpx;
  515. border-radius: 6rpx;
  516. z-index: 1;
  517. }
  518. }
  519. }
  520. }
  521. /* 作品列表相关样式 */
  522. .works-container {
  523. padding: 30rpx;
  524. .works-header {
  525. margin-bottom: 30rpx;
  526. }
  527. .works-content {
  528. display: flex;
  529. flex-direction: column;
  530. .work-item-wrapper {
  531. position: relative;
  532. margin-bottom: 20rpx;
  533. }
  534. .empty-works {
  535. width: 100%;
  536. padding: 100rpx 0;
  537. display: flex;
  538. flex-direction: column;
  539. align-items: center;
  540. justify-content: center;
  541. .empty-text {
  542. font-size: 32rpx;
  543. color: #666;
  544. margin-bottom: 20rpx;
  545. }
  546. .empty-tips {
  547. font-size: 28rpx;
  548. color: #999;
  549. }
  550. }
  551. }
  552. }
  553. .bottom-action-bar {
  554. position: fixed;
  555. bottom: 0;
  556. left: 0;
  557. width: 100%;
  558. height: calc(120rpx + env(safe-area-inset-bottom));
  559. background-color: #fff;
  560. border-top: 1rpx solid #eee;
  561. display: flex;
  562. justify-content: space-around;
  563. align-items: center;
  564. z-index: 99;
  565. padding-bottom: env(safe-area-inset-bottom);
  566. .action-item {
  567. display: flex;
  568. flex-direction: column;
  569. align-items: center;
  570. justify-content: center;
  571. .action-icon {
  572. width: 80rpx;
  573. height: 80rpx;
  574. display: flex;
  575. align-items: center;
  576. justify-content: center;
  577. }
  578. text {
  579. font-size: 24rpx;
  580. color: #666;
  581. margin-top: 8rpx;
  582. }
  583. }
  584. }
  585. // 编辑模式公共样式
  586. .edit-mode {
  587. .item-checkbox {
  588. position: absolute;
  589. top: 10rpx;
  590. left: 10rpx;
  591. z-index: 10;
  592. background-color: rgba(255,255,255,0.8);
  593. border-radius: 50%;
  594. width: 40rpx;
  595. height: 40rpx;
  596. display: flex;
  597. align-items: center;
  598. justify-content: center;
  599. .checkbox-inner {
  600. width: 40rpx;
  601. height: 40rpx;
  602. background-color: #1989fa;
  603. border-radius: 50%;
  604. display: flex;
  605. align-items: center;
  606. justify-content: center;
  607. }
  608. .checkbox-inner-no {
  609. width: 40rpx;
  610. height: 40rpx;
  611. background-color: #fff;
  612. border-radius: 50%;
  613. display: flex;
  614. align-items: center;
  615. justify-content: center;
  616. }
  617. }
  618. }
  619. </style>