read.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. readPages.value.find((child,index)=>{
  177. child.UnLockChapter()
  178. })
  179. }
  180. function showBottomLoadingStatus(){
  181. readPages.value.find((child,index)=>{
  182. if( index == (book_text_list_view.value.length-1) ){
  183. child.showBottomLoading()
  184. down_dir_load()
  185. }
  186. })
  187. }
  188. function showTopLoadingStatus(){
  189. readPages.value.find((child,index)=>{
  190. if( index == 0 ){
  191. child.showTopLoading()
  192. up_dir_load()
  193. }
  194. })
  195. }
  196. //设置相关
  197. let menuShow = ref(false)
  198. let startTouchY = ref(-1)
  199. let endTouchY = ref(-1)
  200. function onClickCatalog(){
  201. closeMenu()
  202. onShowChapterList()
  203. }
  204. function hideChapterList(){
  205. showChapterList.value = false
  206. }
  207. function onClickOpenSetting(){
  208. console.log("点了设置")
  209. showSetting()
  210. }
  211. function onClickPreChapter(){
  212. goToChapter(tools.getPreChapterData(cur_read_chapter_index.value,directoryList))
  213. hideSetting()
  214. }
  215. function onClickNextChapter(){
  216. goToChapter(tools.getNextChapterData(cur_read_chapter_index.value,directoryList))
  217. hideSetting()
  218. }
  219. function onStartClickView(e){
  220. startTouchY.value = e.changedTouches[0].pageY
  221. }
  222. function onEndClickView(e){
  223. endTouchY.value = e.changedTouches[0].pageY
  224. if(Math.abs(endTouchY.value-startTouchY.value)<=5){
  225. startTouchY.value = -1
  226. showMenuView()
  227. }
  228. }
  229. function showMenuView(){
  230. menuShow.value = true
  231. }
  232. function hideAllTop(){
  233. }
  234. function showSetting(){
  235. if(Menu.value.getSetttingStatus()){
  236. Menu.value.showSettingOther()
  237. }else{
  238. Menu.value.showSettingFont()
  239. }
  240. }
  241. function hideSetting(){
  242. startTouchY.value = -1
  243. menuShow.value = false
  244. hideChapterList()
  245. }
  246. function closeMenu(){
  247. hideSetting()
  248. }
  249. function onChangeFontSize(index:number){
  250. let new_index = -1;
  251. if(index>0){
  252. new_index = ReadSetting().data.fontSizeIndex + 1
  253. if(new_index>=config.read_config.fontSizeList.length){
  254. }else{
  255. ReadSetting().changeFontSize(new_index)
  256. }
  257. }else{
  258. new_index = ReadSetting().data.fontSizeIndex - 1
  259. if(new_index<0){
  260. }else{
  261. ReadSetting().changeFontSize(new_index)
  262. }
  263. }
  264. }
  265. function onChangeBgColor(index:number){
  266. ReadSetting().changeReadMode(config.read_config.readMode.Bright)
  267. ReadSetting().changeBgColor(index)
  268. }
  269. function onSelectAutoBuy(isAuto:boolean){
  270. ReadSetting().changeAutoBuyNextChapter(isAuto)
  271. }
  272. function onClickKeep(book_id:number){
  273. tools.checkBookOnBookshelf(book_id,(is_on)=>{
  274. if(is_on){
  275. // uni.showToast({
  276. // title:'当前书已在书架中了!',
  277. // icon:'none'
  278. // })
  279. tools.deleteBookshelf([book_data.book_id],()=>{
  280. Menu.value.updateBookshelfStatus()
  281. uni.showToast({
  282. title:'已移除书架!',
  283. icon:'none'
  284. })
  285. })
  286. }else{
  287. tools.addBookshelf(book_data,()=>{
  288. Menu.value.updateBookshelfStatus()
  289. uni.showToast({
  290. title:'成功添加书架!',
  291. icon:'none'
  292. })
  293. })
  294. }
  295. })
  296. log.Debug("收藏",book_data.book_name)
  297. }
  298. let DarkBg = ref(tools.getDbColorByMode(read_setting_data.readMode))
  299. function onClickMode(mode:number){
  300. ReadSetting().changeReadMode(mode)
  301. }
  302. watch(async () => ReadSetting().data.readMode, // 监听的数据源
  303. (newVal, oldVal) => {
  304. DarkBg.value = tools.getDbColorByMode(read_setting_data.readMode)
  305. updateNavigation()
  306. }
  307. );
  308. //设置end
  309. //章节列表 start
  310. let showChapterList = ref(false)
  311. function goToChapter(chapter_list_item_data:chapter_item_data){
  312. // console.log("想看",chapter_list_item_data)
  313. if(chapter_list_item_data!=null){
  314. cur_read_chapter_index.value = tools.getCurChapterIndex(chapter_list_item_data.id,directoryList)
  315. draw(()=>{
  316. book_text_list_view.value = []
  317. })
  318. hideChapterList()
  319. }else{
  320. uni.showToast({
  321. title:'没有此章节哦!',
  322. icon:'none'
  323. })
  324. log.Error('goToChapter error!',cur_read_chapter_index.value)
  325. }
  326. }
  327. watch(async () => ReadSetting().data.colorBgIndex, // 监听的数据源
  328. (newVal, oldVal) => {
  329. bgColor = config.read_config.colorList[read_setting_data.colorBgIndex]
  330. updateNavigation()
  331. }
  332. );
  333. function updateNavigation(){
  334. let showbgColor = bgColor
  335. if(read_setting_data.readMode==config.read_config.readMode.Dark){
  336. showbgColor = tools.getDbColorByMode(read_setting_data.readMode)
  337. }
  338. uni.setNavigationBarColor({
  339. frontColor: '#ffffff',
  340. backgroundColor: showbgColor,
  341. animation: {
  342. duration: 0,
  343. timingFunc: 'easeIn'
  344. }
  345. })
  346. }
  347. //充值start
  348. let showRecharge = ref(false)
  349. // function getCurChapterId(){
  350. // return tools.getCurChapterData(cur_read_chapter_index.value,directoryList).id
  351. // }
  352. function getChapterNeedCoin(){
  353. let chapter = tools.getCurChapterData(cur_read_chapter_index.value,directoryList)
  354. if(chapter!=null){
  355. return chapter.coin
  356. }
  357. return 0
  358. }
  359. function onBuyChapter(){
  360. let chapter = tools.getCurChapterData(cur_read_chapter_index.value,directoryList)
  361. BuyBookChapter(chapter.id,chapter.coin)
  362. }
  363. function onClickBuy(goods_id:number,isVip:boolean = true){
  364. uni.showLoading({
  365. title:'跳转支付...',
  366. mask:true
  367. })
  368. http.DynamicRequest(config.url_confg.Dynamic.recharge.order_buy,{'pay_type':tools.getCurBuyType(),'goods_id':goods_id},(err,d)=>{
  369. if(!err){
  370. if(d.code == config.url_confg.StatesCode.SUCCESS){
  371. let data:order_data = d.content
  372. SchedulingOrder(data.order_id,()=>{
  373. UserData().update_user_info(()=>{
  374. uni.hideLoading()
  375. if(isVip){
  376. UnLockChapter()
  377. showRecharge.value = false
  378. }
  379. uni.showToast({
  380. title:'购买成功'
  381. })
  382. })
  383. })
  384. }
  385. }
  386. })
  387. }
  388. function BuyBookChapter(chapter_id:number,coin:number){
  389. if(tools.getUserIsVIP()){
  390. UnLockChapter()
  391. return
  392. }
  393. if(UserData().getData().coin>=coin){
  394. uni.showLoading({
  395. title:'正在购买...',
  396. mask:true
  397. })
  398. http.DynamicRequest(config.url_confg.Dynamic.buy_chapter,{'book_id':book_data.book_id,'chapter_id':chapter_id},(err,d)=>{
  399. if(!err){
  400. log.Debug(d)
  401. if(d.code==config.url_confg.StatesCode.SUCCESS){
  402. // d.content.includes(chapter_id)
  403. UserData().update_user_info(()=>{
  404. uni.hideLoading()
  405. uni.showToast({
  406. title:'购买成功!'
  407. })
  408. UnLockChapter()
  409. showRecharge.value = false
  410. })
  411. }else{
  412. }
  413. }else{
  414. log.Error(err)
  415. }
  416. })
  417. }else{
  418. uni.showToast({
  419. title:'书币不足!'
  420. })
  421. }
  422. }
  423. function SchedulingOrder(order_id:string,cb:Function){
  424. let temp = setInterval(()=>{
  425. http.DynamicRequest(config.url_confg.Dynamic.recharge.order_info,{'order_id':order_id},(err,d)=>{
  426. if(!err){
  427. if(d.code == config.url_confg.StatesCode.SUCCESS){
  428. let data:order_info_data = d.content
  429. if(data.status==config.order_status.YI_JING_ZHI_FU){
  430. clearInterval(temp)
  431. cb()
  432. }
  433. }
  434. }
  435. })
  436. },500)
  437. }
  438. let unLockChapterList = ref(null)
  439. function onShowChapterList(){
  440. uni.showLoading({
  441. title:'...',
  442. })
  443. unLockChapterList.value =[]
  444. tools.get_book_un_lock_list(book_data.book_id,(list)=>{
  445. if(list!=null){
  446. unLockChapterList.value = list
  447. showChapterList.value = true
  448. }else{
  449. uni.showToast({
  450. title:'加载错误',
  451. icon:'none'
  452. })
  453. }
  454. uni.hideLoading()
  455. })
  456. }
  457. </script>
  458. <style scoped lang="scss">
  459. .color-bg{
  460. // position: relative; /* 或者使用 fixed/absolute,根据需要调整 */
  461. width: 100vw;
  462. // height: 100vh; /* 视口高度 */
  463. // overflow: hidden; /* 隐藏超出容器的内容,如果需要的话 */
  464. // height: 100%;
  465. overflow-y: auto;
  466. }
  467. .content {
  468. /* 内容组件的样式 */
  469. position: relative; /* 相对于 page-container 定位 */
  470. z-index: 1; /* 确保内容在背景之上 */
  471. /* 其他样式... */
  472. }
  473. .scroll-container {
  474. // position: relative; /* 或者使用 fixed/absolute,根据需要调整 */
  475. width: 100vw;
  476. // height: 100vh; /* 视口高度 */
  477. // overflow: hidden; /* 隐藏超出容器的内容,如果需要的话 */
  478. // height: 100%;
  479. overflow-y: auto;
  480. scrollbar-width: none; /* Firefox */
  481. -ms-overflow-style: none; /* IE 10+ */
  482. }
  483. /* 针对Chrome, Safari和Opera等Webkit浏览器 */
  484. .scroll-container::-webkit-scrollbar {
  485. width: 0;
  486. height: 0;
  487. }
  488. /* 隐藏水平滚动条 */
  489. .scroll-container::-webkit-scrollbar {
  490. width: 0;
  491. height: 0;
  492. }
  493. /* 隐藏垂直滚动条 */
  494. .scroll-container::-webkit-scrollbar {
  495. width: 0;
  496. height: 0;
  497. }
  498. /* 同时隐藏水平和垂直滚动条 */
  499. .scroll-container::-webkit-scrollbar {
  500. width: 0;
  501. height: 0;
  502. }
  503. </style>