read.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <template>
  2. <scroll-view class="scroll-container" :scroll-y="true" bounces="false" :show-scrollbar="false" :refresher-enabled="false" :style="{'background-color': read_setting_data.readMode==config.read_config.readMode.Bright?config.read_config.colorList[read_setting_data.colorBgIndex]:DarkBg,'height':'100%' }">
  3. <readPage ref="readPages" @onTouchstart="onStartClickView"
  4. @clickCatalog="onClickCatalog"
  5. @clickPreChapter="onClickPreChapter"
  6. @clickNextChapter="onClickNextChapter"
  7. @onTouchend="onEndClickView" @onTouchmove="hideSetting" v-for="(book_read_data_item,index) in book_text_list_view" :key="book_read_data_item.book_chapter_id"
  8. :text_content="book_read_data_item.book_content" :book_title="book_read_data_item.book_title">
  9. </readPage>
  10. </scroll-view>
  11. <settingMenu ref="Menu" v-if="menuShow" :book_id="book_data.book_id" @clickCatalog="onClickCatalog"
  12. @clickPreChapter="onClickPreChapter" @clickNextChapter="onClickNextChapter"
  13. @clickMode="onClickMode" @clickOpenSetting="onClickOpenSetting"
  14. @Close="closeMenu" @changeFontSize="onChangeFontSize" @changeBgColor="onChangeBgColor"
  15. @selectAutoBuy="onSelectAutoBuy" @clickKeep="onClickKeep">
  16. </settingMenu>
  17. <chapterCatalog v-if="showChapterList" :items="directoryList" :size="40" :remain="16"
  18. :scrollHeight="windowHeight*0.6" @clickChar="goToChapter" @Close="showChapterList=false">
  19. </chapterCatalog>
  20. <recharge v-if="showRecharge" :coin="getChapterNeedCoin()" @Close="showRecharge=false" @onClickBuy="onClickBuy" @BuyChapter="onBuyChapter"></recharge>
  21. </template>
  22. <script setup lang="ts">
  23. import { onUnload } from '@dcloudio/uni-app'
  24. import { nextTick, onMounted, ref, watch } from 'vue';
  25. import { book_item_data, book_read_data, chapter_item_data, order_data, order_info_data } from '../../data/data';
  26. import { tools } from '../../framework/tools';
  27. import { UserStatus } from '../../stores/userStatusManager';
  28. import { log } from '../../framework/log';
  29. import readPage from '../../components/read/readPage.vue'
  30. import settingMenu from '../../components/read/settingMenu.vue'
  31. import chapterCatalog from '../../components/read/chapterCatalog.vue'
  32. import {onPullDownRefresh,onReachBottom} from '@dcloudio/uni-app'
  33. import { ReadSetting } from '../../stores/readSetting';
  34. import { config } from '../../config/config';
  35. import { ReadRecord } from '../../stores/readRecordManager';
  36. import recharge from '../../components/read/recharge.vue';
  37. import { http } from '../../framework/http';
  38. import { UserData } from '../../stores/userDataManager';
  39. const {windowHeight,windowWidth} = uni.getSystemInfoSync()
  40. const readPages = ref(null)
  41. const Menu = ref(null)
  42. let book_data:book_item_data = UserStatus().getUserSelectBook()
  43. let directoryList:chapter_item_data[] = []
  44. let cur_read_chapter_index =ref(0)
  45. let start_read_chapter_id = book_data.start_read_chapter_id
  46. //设置属性
  47. let read_setting_data = ReadSetting().getReadSetting()
  48. let fontSize = config.read_config.fontSizeList[read_setting_data.fontSizeIndex]
  49. let bgColor = config.read_config.colorList[read_setting_data.colorBgIndex]
  50. // 页面销毁
  51. onUnload(()=>{
  52. tools.exitRead()
  53. })
  54. onMounted(()=>{
  55. })
  56. //设置属性end
  57. updateNavigation()
  58. if(book_data!=null&&start_read_chapter_id!=-1){
  59. initView()
  60. }
  61. let book_text_list_view = ref<Array<book_read_data>>([])
  62. let book_text_list_cache = ref<Map<number,book_read_data>>(new Map<number,book_read_data>())
  63. function initView(){
  64. tools.getChapterList(book_data.chapter_path,(chapter_ls)=>{
  65. directoryList = chapter_ls
  66. cur_read_chapter_index.value = tools.getCurChapterIndex(start_read_chapter_id,directoryList)
  67. draw(null)
  68. })
  69. }
  70. function draw(cb:Function,dir_is_down:boolean=true){
  71. let loading_view = (d:book_read_data)=>{
  72. book_data.start_read_chapter_id = d.book_chapter_id //记录开始阅读章节id
  73. ReadRecord().addData(book_data,d)
  74. cb && cb()
  75. nextTick(()=>{
  76. if(d!=null){
  77. if(dir_is_down){
  78. book_text_list_view.value.push(d)
  79. }else{
  80. book_text_list_view.value.unshift(d)
  81. }
  82. check_buy_status()
  83. }
  84. })
  85. }
  86. if(cur_read_chapter_index.value<=directoryList.length&&cur_read_chapter_index.value>=0){
  87. let data = tools.getCurChapterData(cur_read_chapter_index.value,directoryList)
  88. if(data!=null){
  89. let cache_data = book_text_list_cache.value.get(data.id)
  90. if(cache_data!=null){
  91. loading_view(cache_data)
  92. }else{
  93. tools.getCurChapterTxt(book_data.base_path,data.id,fontSize*2,(text:string)=>{
  94. cache_data = new book_read_data()
  95. cache_data.book_content = text
  96. cache_data.book_chapter_id = data.id
  97. cache_data.book_chapter_name = data.name
  98. cache_data.book_title = tools.autoParagraphTitle(data.name,fontSize*4)
  99. book_text_list_cache.value.set(data.id,cache_data)
  100. loading_view(cache_data)
  101. })
  102. }
  103. }
  104. }else{
  105. log.Error("暂无可读取内容!")
  106. }
  107. }
  108. function check_buy_status(){
  109. let chapter = tools.getCurChapterData(cur_read_chapter_index.value,directoryList);
  110. if(chapter.pay_type == config.chapter_pay_type.SHOU_FEI){
  111. if(!tools.getUserIsVIP()){ //判断是否是vip
  112. if(ReadSetting().getReadSetting().autoBuyNextChpater){ //判断是否是自动购买
  113. if(UserData().getData().coin>=chapter.coin){ //用户书币足够支付
  114. check_book_chapter_is_buy(chapter.id,(isBuy)=>{
  115. if(!isBuy){
  116. BuyBookChapter(chapter.id,chapter.coin)
  117. }
  118. })
  119. }else{
  120. check_book_chapter_is_buy(chapter.id)
  121. }
  122. }else{
  123. check_book_chapter_is_buy(chapter.id)
  124. }
  125. }else{
  126. UnLockChapter()
  127. }
  128. }
  129. }
  130. let showCode = ref(false)
  131. function check_book_chapter_is_buy(chapter_id:number,cb:Function=null){
  132. tools.check_book_chapter_is_buy(book_data.book_id,chapter_id,(isBuy)=>{
  133. if(!isBuy){
  134. console.log("check_book_chapter_is_buy1",isBuy)
  135. showRecharge.value = true
  136. showCode.value = true
  137. LockChapter()
  138. }else{
  139. UnLockChapter()
  140. }
  141. if(cb!=null){
  142. cb(isBuy)
  143. }
  144. })
  145. }
  146. function down_dir_load(){
  147. cur_read_chapter_index.value+=1;
  148. draw(null)
  149. }
  150. function up_dir_load(){
  151. cur_read_chapter_index.value-=1;
  152. draw(null,false)
  153. }
  154. // onPullDownRefresh( async () => {
  155. // showTopLoadingStatus()
  156. // uni.stopPullDownRefresh();
  157. // })
  158. // onReachBottom(async ()=>{
  159. // showBottomLoadingStatus()
  160. // })
  161. function LockChapter(){
  162. readPages.value.find((child,index)=>{
  163. child.LockChapter()
  164. })
  165. }
  166. function UnLockChapter(){
  167. readPages.value.find((child,index)=>{
  168. child.UnLockChapter()
  169. })
  170. }
  171. function showBottomLoadingStatus(){
  172. readPages.value.find((child,index)=>{
  173. if( index == (book_text_list_view.value.length-1) ){
  174. child.showBottomLoading()
  175. down_dir_load()
  176. }
  177. })
  178. }
  179. function showTopLoadingStatus(){
  180. readPages.value.find((child,index)=>{
  181. if( index == 0 ){
  182. child.showTopLoading()
  183. up_dir_load()
  184. }
  185. })
  186. }
  187. //设置相关
  188. let menuShow = ref(false)
  189. let startTouchY = ref(-1)
  190. let endTouchY = ref(-1)
  191. function onClickCatalog(){
  192. closeMenu()
  193. showChapterList.value = true
  194. }
  195. function hideChapterList(){
  196. showChapterList.value = false
  197. }
  198. function onClickOpenSetting(){
  199. console.log("点了设置")
  200. showSetting()
  201. }
  202. function onClickPreChapter(){
  203. goToChapter(tools.getPreChapterData(cur_read_chapter_index.value,directoryList))
  204. hideSetting()
  205. }
  206. function onClickNextChapter(){
  207. goToChapter(tools.getNextChapterData(cur_read_chapter_index.value,directoryList))
  208. hideSetting()
  209. }
  210. function onStartClickView(e){
  211. startTouchY.value = e.changedTouches[0].pageY
  212. }
  213. function onEndClickView(e){
  214. endTouchY.value = e.changedTouches[0].pageY
  215. if(Math.abs(endTouchY.value-startTouchY.value)<=5){
  216. startTouchY.value = -1
  217. showMenuView()
  218. }
  219. }
  220. function showMenuView(){
  221. menuShow.value = true
  222. }
  223. function hideAllTop(){
  224. }
  225. function showSetting(){
  226. Menu.value.showSettingFont()
  227. }
  228. function hideSetting(){
  229. startTouchY.value = -1
  230. menuShow.value = false
  231. hideChapterList()
  232. }
  233. function closeMenu(){
  234. hideSetting()
  235. }
  236. function onChangeFontSize(index:number){
  237. let new_index = -1;
  238. if(index>0){
  239. new_index = ReadSetting().data.fontSizeIndex + 1
  240. if(new_index>=config.read_config.fontSizeList.length){
  241. }else{
  242. ReadSetting().changeFontSize(new_index)
  243. }
  244. }else{
  245. new_index = ReadSetting().data.fontSizeIndex - 1
  246. if(new_index<0){
  247. }else{
  248. ReadSetting().changeFontSize(new_index)
  249. }
  250. }
  251. }
  252. function onChangeBgColor(index:number){
  253. ReadSetting().changeReadMode(config.read_config.readMode.Bright)
  254. ReadSetting().changeBgColor(index)
  255. }
  256. function onSelectAutoBuy(isAuto:boolean){
  257. ReadSetting().changeAutoBuyNextChapter(isAuto)
  258. }
  259. function onClickKeep(book_id:number){
  260. tools.checkBookOnBookshelf(book_id,(is_on)=>{
  261. if(is_on){
  262. uni.showToast({
  263. title:'当前书已在书架中了!',
  264. icon:'none'
  265. })
  266. }else{
  267. tools.addBookshelf(book_data,()=>{
  268. Menu.value.updateBookshelfStatus()
  269. uni.showToast({
  270. title:'成功添加书架!',
  271. icon:'none'
  272. })
  273. })
  274. }
  275. })
  276. log.Debug("收藏",book_data.book_name)
  277. }
  278. let DarkBg = ref(tools.getDbColorByMode(read_setting_data.readMode))
  279. function onClickMode(mode:number){
  280. ReadSetting().changeReadMode(mode)
  281. }
  282. watch(async () => ReadSetting().data.readMode, // 监听的数据源
  283. (newVal, oldVal) => {
  284. DarkBg.value = tools.getDbColorByMode(read_setting_data.readMode)
  285. updateNavigation()
  286. }
  287. );
  288. //设置end
  289. //章节列表 start
  290. let showChapterList = ref(false)
  291. function goToChapter(chapter_list_item_data:chapter_item_data){
  292. // console.log("想看",chapter_list_item_data)
  293. if(chapter_list_item_data!=null){
  294. cur_read_chapter_index.value = tools.getCurChapterIndex(chapter_list_item_data.id,directoryList)
  295. draw(()=>{
  296. book_text_list_view.value = []
  297. })
  298. hideChapterList()
  299. }else{
  300. uni.showToast({
  301. title:'没有此章节哦!',
  302. icon:'none'
  303. })
  304. log.Error('goToChapter error!',cur_read_chapter_index.value)
  305. }
  306. }
  307. watch(async () => ReadSetting().data.colorBgIndex, // 监听的数据源
  308. (newVal, oldVal) => {
  309. bgColor = config.read_config.colorList[read_setting_data.colorBgIndex]
  310. updateNavigation()
  311. }
  312. );
  313. function updateNavigation(){
  314. let showbgColor = bgColor
  315. if(read_setting_data.readMode==config.read_config.readMode.Dark){
  316. showbgColor = tools.getDbColorByMode(read_setting_data.readMode)
  317. }
  318. uni.setNavigationBarColor({
  319. frontColor: '#ffffff',
  320. backgroundColor: showbgColor,
  321. animation: {
  322. duration: 0,
  323. timingFunc: 'easeIn'
  324. }
  325. })
  326. }
  327. //充值start
  328. let showRecharge = ref(false)
  329. // function getCurChapterId(){
  330. // return tools.getCurChapterData(cur_read_chapter_index.value,directoryList).id
  331. // }
  332. function getChapterNeedCoin(){
  333. let chapter = tools.getCurChapterData(cur_read_chapter_index.value,directoryList)
  334. if(chapter!=null){
  335. return chapter.coin
  336. }
  337. return 0
  338. }
  339. function onBuyChapter(){
  340. let chapter = tools.getCurChapterData(cur_read_chapter_index.value,directoryList)
  341. BuyBookChapter(chapter.id,chapter.coin)
  342. }
  343. function onClickBuy(goods_id:number,isVip:boolean = true){
  344. uni.showLoading({
  345. title:'跳转支付...',
  346. mask:true
  347. })
  348. http.DynamicRequest(config.url_confg.Dynamic.recharge.order_buy,{'pay_type':tools.getCurBuyType(),'goods_id':goods_id},(err,d)=>{
  349. if(!err){
  350. if(d.code == config.url_confg.StatesCode.SUCCESS){
  351. let data:order_data = d.content
  352. SchedulingOrder(data.order_id,()=>{
  353. UserData().update_user_info(()=>{
  354. uni.hideLoading()
  355. if(isVip){
  356. UnLockChapter()
  357. showRecharge.value = false
  358. }
  359. })
  360. })
  361. }
  362. }
  363. })
  364. }
  365. function BuyBookChapter(chapter_id:number,coin:number){
  366. if(tools.getUserIsVIP()){
  367. UnLockChapter()
  368. return
  369. }
  370. if(UserData().getData().coin>=coin){
  371. uni.showLoading({
  372. title:'正在购买...',
  373. mask:true
  374. })
  375. http.DynamicRequest(config.url_confg.Dynamic.buy_chapter,{'book_id':book_data.book_id,'chapter_id':chapter_id},(err,d)=>{
  376. if(!err){
  377. log.Debug(d)
  378. if(d.code==config.url_confg.StatesCode.SUCCESS){
  379. // d.content.includes(chapter_id)
  380. UserData().update_user_info(()=>{
  381. uni.hideLoading()
  382. uni.showToast({
  383. title:'购买成功!'
  384. })
  385. UnLockChapter()
  386. showRecharge.value = false
  387. })
  388. }else{
  389. }
  390. }else{
  391. log.Error(err)
  392. }
  393. })
  394. }else{
  395. uni.showToast({
  396. title:'书币不足!'
  397. })
  398. }
  399. }
  400. function SchedulingOrder(order_id:string,cb:Function){
  401. let temp = setInterval(()=>{
  402. http.DynamicRequest(config.url_confg.Dynamic.recharge.order_info,{'order_id':order_id},(err,d)=>{
  403. if(!err){
  404. if(d.code == config.url_confg.StatesCode.SUCCESS){
  405. let data:order_info_data = d.content
  406. if(data.status==config.order_status.YI_JING_ZHI_FU){
  407. clearInterval(temp)
  408. cb()
  409. }
  410. }
  411. }
  412. })
  413. },500)
  414. }
  415. </script>
  416. <style scoped lang="scss">
  417. .color-bg{
  418. // position: relative; /* 或者使用 fixed/absolute,根据需要调整 */
  419. width: 100vw;
  420. // height: 100vh; /* 视口高度 */
  421. // overflow: hidden; /* 隐藏超出容器的内容,如果需要的话 */
  422. // height: 100%;
  423. overflow-y: auto;
  424. }
  425. .content {
  426. /* 内容组件的样式 */
  427. position: relative; /* 相对于 page-container 定位 */
  428. z-index: 1; /* 确保内容在背景之上 */
  429. /* 其他样式... */
  430. }
  431. .scroll-container {
  432. // position: relative; /* 或者使用 fixed/absolute,根据需要调整 */
  433. width: 100vw;
  434. // height: 100vh; /* 视口高度 */
  435. // overflow: hidden; /* 隐藏超出容器的内容,如果需要的话 */
  436. // height: 100%;
  437. overflow-y: auto;
  438. scrollbar-width: none; /* Firefox */
  439. -ms-overflow-style: none; /* IE 10+ */
  440. }
  441. /* 针对Chrome, Safari和Opera等Webkit浏览器 */
  442. .scroll-container::-webkit-scrollbar {
  443. width: 0;
  444. height: 0;
  445. }
  446. /* 隐藏水平滚动条 */
  447. .scroll-container::-webkit-scrollbar {
  448. width: 0;
  449. height: 0;
  450. }
  451. /* 隐藏垂直滚动条 */
  452. .scroll-container::-webkit-scrollbar {
  453. width: 0;
  454. height: 0;
  455. }
  456. /* 同时隐藏水平和垂直滚动条 */
  457. .scroll-container::-webkit-scrollbar {
  458. width: 0;
  459. height: 0;
  460. }
  461. </style>