read.vue 15 KB

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