Explorar o código

阅读翻页 修改图书列表接口 只取国内

hjr hai 3 meses
pai
achega
6d85d55648
Modificáronse 4 ficheiros con 144 adicións e 53 borrados
  1. 27 4
      src/components/library/index.ux
  2. 1 2
      src/helper/ajax.js
  3. 4 2
      src/manifest.json
  4. 112 45
      src/pages/book-content/index.ux

+ 27 - 4
src/components/library/index.ux

@@ -42,6 +42,7 @@
 
 import fetch from '@system.fetch'
 import { bookListData } from '../../assets/data/book-list.js'
+import ad from '@service.ad'
 export default {
   props: [],
   data: {
@@ -58,6 +59,8 @@ export default {
     bannerList:[],
     hotList:[], // 热门图书
     bookList:[],// 图书列表
+    bannerAd: null,
+    adUnitId: "278eae7418b04abbb5926847ed42271e", // 这个id是vivo的com.quickapp.center创建的,需要修改manifest的package才能预览出效果
   },
   search(info) {
     let searchText = info.detail
@@ -67,19 +70,39 @@ export default {
     // $utils.route2theUrl('pages/search', { initValue: text })
     $utils.route2theUrl('pages/search-book', { initValue: text })
   },
+  initAd() {
+    try {
+      this.bannerAd = ad.createBannerAd({  // 使用require方式避免在不支持广告接口的厂商运行时报错
+        adUnitId: this.adUnitId
+      })
+      this.bannerAd.onLoad(() => {     // 监听广告加载
+        console.log('onLoad event emit')
+        this.bannerAd.show()
+      })
+      this.bannerAd.onError((err) => { // 监听广告出错
+        console.log('onError event emit', err)
+      })
+      this.bannerAd.onClose((res) => { // 监听广告关闭
+        console.log('onClose event emit', res)
+      })
+    } catch (e) {
+      console.log('initAd',e)
+    }
+  },
   async onInit() {
+    console.log(ad.getProvider())
+    this.initAd()
     this.queryLibraryList(1)
     const res = await $apis.common.novelBanner({bannerType: 8, pageNum: 1,pageSize: 10})
     this.bannerList = res.data.result.list
     const res1 = await $apis.common.novelModulePage({moduleType: 8, pageNum: 1,pageSize: 10,statu:1})
     // this.hotList = res1.data.result.list
     this.rankList = res1.data.result.list.slice(0, 3)
-    const res2 = await $apis.common.novelPage({pageNum: 1,pageSize: 10})
+    // 国内
+    const res2 = await $apis.common.novelModulePage({moduleType: 8, ageNum: 1,pageSize: 10})
     this.library.list = res2.data.result.list
     this.library.total = res2.data.result.total
     this.library.pageSize = res2.data.result.pageSize
-    console.log('url',this.bookList )
-    console.log('urhotListl',this.rankList )
   },
   /* -------------------SelfCustomEvent------------------ */
   queryLibraryList(pageNo) {
@@ -106,7 +129,7 @@ export default {
     let total = this.library.pageNo * this.library.pageSize
     let end = this.library.total < total ? this.library.total : total
     let start = total - this.library.pageSize
-    const res =  await $apis.common.novelPage({bannerType: 1, pageNum: this.library.pageNo,pageSize: 10}) //bookListData.slice(start, end)
+    const res =  await $apis.common.novelModulePage({moduleType: 8, pageNum: this.library.pageNo,pageSize: 10}) //bookListData.slice(start, end)
     let concatList = res.data.result.list
     // 更新列表
     this.library.list = this.library.list.concat(concatList)

+ 1 - 2
src/helper/ajax.js

@@ -3,7 +3,7 @@ import $utils from './utils'
 var requestUrl = 'https://ltnovel8.weishuitang.com/pro-api/'
 
 function requestHandle(params) {
-  console.log(`🤖 当前正在发起请求的 Url 是: ${params.url}`)
+  // console.log(`🤖 当前正在发起请求的 Url 是: ${params.url}`)
   return new Promise((resolve, reject) => {
     $fetch.fetch({
       url: requestUrl + params.url,
@@ -16,7 +16,6 @@ function requestHandle(params) {
         // Token:`eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTc1MzE2NjUwOCwiZXhwIjoxNzYxMTE1MzA4fQ.j6X0PPFH8za9puF_MOCwThQvfa4qb6fwCK2Kw7N0GtY`,
       },
       success: data => {
-        // console.log(`🐛 request success`,  JSON.parse(data.data).data.result)
         resolve(JSON.parse(data.data)) // .data.result
       },
       fail: (data, code) => {

+ 4 - 2
src/manifest.json

@@ -1,5 +1,5 @@
 {
-  "package": "com.example.demo",
+  "package": "com.quickapp.center",
   "name": "demo",
   "versionName": "1.0.0",
   "versionCode": 1,
@@ -53,7 +53,9 @@
     },
     {
       "name": "system.geolocation"
-    }
+    },
+    {"name":"service.ad"},
+    { "name": "service.health" }
   ],
   "permissions": [
     {

+ 112 - 45
src/pages/book-content/index.ux

@@ -11,9 +11,26 @@
             chapterTitle
           }}</text>
         </list-item>
-        <list-item type="content" onclick="showOrHidePop" show="{{!!content}}">
+        <!-- <list-item type="content" onclick="showOrHidePop" show="{{!!content}}">
           <text class="book-text" style="{{contentStyle}}">{{ content }}</text>
+        </list-item> -->
+        <list-item type="content" show="{{!!content}}"  onclick="showOrHidePop">
+          <div class="doc-page">
+            <richtext scene="book" if="showRich" style="{{contentStyle}}" @splitpage="pageEndFunction" id="book" @pagechanged="pageChangeFunction">
+              {{ content }}</richtext>
+            <text if="{{totalPage > 0}}" class="index-class">{{ currentPage }} / {{ totalPage }} </text>
+          </div>
+          <!-- <swiper class="swiper" loop="{{false}}" index="{{currentPage}}" 
+          onchange="pageChange" indicator="{{ false }}">
+            <text class="book-text swiper-item" for="(index,item) in  pages" 
+            style="{{contentStyle}}">{{item}}</text>
+          </swiper> -->
         </list-item>
+        <!-- <list-item type="content" onclick="showOrHidePop" show="{{!!content}}">
+          <div style="width: 100%;justify-content: flex-end;">
+             <text >{{ currentPage+1 }}/ {{ pages.length }}</text>
+          </div>
+        </list-item> -->
         <list-item type="btns">
           <div class="bottom-bar">
             <text
@@ -85,14 +102,14 @@
             <text
               class="btn"
               onclick="sizeChange('minus')"
-              disabled="{{userSetting.fontsize <= 40}}"
+              disabled="{{userSetting.fontsize <= 30}}"
               >A-</text
             >
             <text class="size">{{ userSetting.fontsize }}</text>
             <text
               class="btn"
               onclick="sizeChange('plus')"
-              disabled="{{userSetting.fontsize >= 70}}"
+              disabled="{{userSetting.fontsize >= 50}}"
               >A+</text
             >
           </div>
@@ -127,6 +144,8 @@ import texttoaudio from '@service.texttoaudio'
 import { getBookContent } from '../../assets/data/book-content.js'
 import { contentsData } from '../../assets/data/contents.js'
 import fetch from '@system.fetch'
+import device from '@system.device'
+
 export default {
   public: {
     bookId: 1,
@@ -135,15 +154,20 @@ export default {
     total: 120
   },
   private: {
-    // content: "这里是完整的小说文本内容...", // 完整小说内容
+    totalPage: 0,
+    index: 0,
+    showRich:false,
+    componentName: 'richtext',
+    screenHeight:1000,
+    screenWidth: 720,
     pages: [], // 分页后的内容数组
-    currentPage: 0, // 当前页码
-    content: '',
+    currentPage: 1, // 当前页码
+    content: '',// 完整小说内容
     chapterTitle: '',
     showPop: false,
     showCategory:false,
     userSetting: {
-      fontsize: 40,
+      fontsize: 30,
       bgColor: '#EAEAEF',
       nightMode: false,
       fontFamily: 'default'
@@ -183,11 +207,20 @@ export default {
       return {
         fontFamily: this.userSetting.fontFamily,
         color: this.userSetting.nightMode ? 'rgb(126,129,134)' : '#000000',
-        fontSize: this.userSetting.fontsize
+        fontSize: this.userSetting.fontsize + 'px',
+        height:this.screenHeight + 'px',
       }
     }
   },
   async onInit() {
+    var that = this
+    device.getInfo({
+     success: function(ret) {
+       that.screenHeight = (ret.screenHeight * 1 - 500) || 1100
+       that.screenWidth = ret.screenWidth * 1
+        console.log(`handling success, brand = ${ ret.screenHeight } ${ ret.screenWidth }`)
+      }
+    })
     //搜索优化
     this.$page.setMeta({
       title: this.bookTitle,
@@ -206,28 +239,9 @@ export default {
     const res = await $apis.common.novelChapterPage({chapter: this.chapterId, novelId: this.bookId})
     let infos = res.data.result.list[0]
     this.chapterTitle = infos.chapterName
-    this.content = this.getTxtContent(infos.content)
+    this.getTxtContent(infos.content)
     console.log('this.content',this.content)
-    // this.getTxtContent('https://ytnovels.oss-cn-shanghai.aliyuncs.com/novels/2025/01945026620890304513-1752828963340.txt')
   },
-   // 分页函数
-    paginateContent() {
-      const CONTENT_PER_PAGE = 300; // 每页字符数(根据实际调整)
-      let pages = [];
-      
-      // 简单分页逻辑
-      for (let i = 0; i < this.content.length; i += CONTENT_PER_PAGE) {
-        pages.push(this.content.substring(i, i + CONTENT_PER_PAGE));
-      }
-      
-      this.pages = pages;
-      console.log('pages',this.pages)
-    },
-    
-    // 翻页事件处理
-    pageChange(e) {
-      this.currentPage = e.index;
-    },
   // 解析TXT文件的主方法
   getTxtContent(url) {
     console.log('url',url)
@@ -238,8 +252,22 @@ export default {
         if (res.code === 200) {
           // 处理文本编码
           const text = this.decodeData(res.data);
-          this.content = text;
-          this.paginateContent();
+          this.content = formatText(text);
+          this.showRich = false
+          setTimeout(()=>{
+            this.showRich = true
+          },50)
+          // this.paginateContent()
+          // 格式化文本:为每行添加p标签
+        function formatText(text) {
+            const lines = text.split('\n');
+            return lines.map(line => {
+                // 跳过空行
+                if (line.trim() === '') return '';
+                return `<p>${line}</p>`;
+            }).join('');
+        }
+        console.log('this.content',this.content)
         } else {
           console.error('请求失败:', res.code);
         }
@@ -293,10 +321,9 @@ export default {
     const res = await $apis.common.novelChapterPage({chapter: this.chapterId, novelId: this.bookId})
     let infos = res.data.result.list[0]
     this.chapterTitle = infos.chapterName
-    this.content = this.getTxtContent(infos.content)
-    // this.content = await getBookContent(this.bookId, this.chapterId)
-    // this.chapterTitle = contentsData[this.bookId][this.chapterId - 1]
+    this.getTxtContent(infos.content)
     this.$element('list').scrollTo({ index: 0 })
+    this.currentPage = 1
   },
   goToContents() {
     $utils.route2theUrl('pages/contents', {
@@ -318,6 +345,14 @@ export default {
     $utils.routerBack()
   },
   sizeChange(type) {
+    if(type == 'minus' && this.userSetting.fontsize <=30  || type == 'plus' && this.userSetting.fontsize >=50){
+        return
+    }
+    // if(this.currentPage != 0){ // 
+    //   $utils.showToast('请回到第一页后再调整')
+    //   return
+    // }
+    // this.paginateContent()
     if (type === 'plus') this.userSetting.fontsize = this.userSetting.fontsize+ 2
     else this.userSetting.fontsize = this.userSetting.fontsize - 2
     this.saveUserSettings()
@@ -356,22 +391,53 @@ export default {
         }
       })
     }
+  },
+  pageChangeFunction(evt) {
+    // 获取当前页数
+    this.currentPage = evt.curpage
+  },
+  pageEndFunction(evt) {
+    this.totalPage = evt.totalpage
   }
 }
 </script>
 
 <style lang="less">
 @import '../../assets/styles/index.less';
-/* .swiper {
-    width: 100%;
-    height: 1500px;
-  }
-  
-  .swiper-item {
-    width: 100%;
-    height: 100%;
-    padding: 20px;
-  } */
+
+.doc-page {
+  flex-direction: column;
+  align-items: center;
+  /* background-color: #ebcda3; */
+}
+
+.index-class {
+  position: fixed;
+  font-size: 30px;
+  color: grey;
+  left: 50px;
+  bottom: 10px;
+}
+
+.rich-text {
+  font-size: 22px;
+  margin: 24px;
+}
+
+.index-wrapper {
+  width: 100px;
+  height: 100px;
+  /* background-color: red; */
+}
+.swiper {
+  width: 100%;
+  height: 1100px;
+}
+.swiper-item {
+  width: 100%;
+  height: 100%;
+  padding: 20px;
+}
 .stack-wrapper {
   width: 100%;
   height: 100%;
@@ -388,12 +454,13 @@ export default {
   .g22kjdgy{
     color: #ffffff;
   }
-  .book-text {
+  /* .book-text {
     margin: @gap-3 0;
-  }
+  } */
 }
 .btn-bottom {
   .btn-primary;
+  margin: 0 @gap-2;
   flex: 1;
   text-align: center;
 }