四零语境前端代码仓库
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.

739 lines
25 KiB

  1. <template>
  2. <view class="yingbing-reader" ref="yingbingReader" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend">
  3. <view class="yingbing-scroll-reader" :class="{'yingbing-hidden': pageType != 'scroll'}" :style="[wrapperStyle]">
  4. <reader-header :progress="currentProgress" :title="currentTitle" v-if="headerShow && currentTitle"></reader-header>
  5. <view class="yingbing-scroll yingbing-reader-content" ref="yingbingReaderContent">
  6. <reader-scroller
  7. ref="scroll"
  8. :autoplay="autoplaySync"
  9. :loadmoreable="!isLoading && !autoplaySync && pages.length > 0"
  10. :pulldownable="!isLoading && !autoplaySync"
  11. @pulldown="handlePulldown" @loadmore="handlePullup" @scroll="handleScroll" :data="pageType == 'scroll' ? pages : []">
  12. <!-- #ifndef MP-->
  13. <template v-slot="{item, index}">
  14. <!-- #endif -->
  15. <!-- #ifdef MP -->
  16. <template v-for="(item, index) in pages" :slot="`wx:${index}`">
  17. <!-- #endif -->
  18. <view :style="{position: 'relative', 'height': contentHeight + 'px'}" v-if="item.type == 'slot' && pageType == 'scroll'">
  19. <!-- 微信小程序vue2虽然支持在v-for中嵌套同名插槽但会一直报错引起页面卡顿vue3不支持在v-for中嵌套同名插槽所以增加2种微信小程序使用方式 -->
  20. <!-- #ifdef MP -->
  21. <slot :name="item.content + ':' + item.index"></slot>
  22. <!-- #endif -->
  23. <!-- #ifndef MP -->
  24. <slot :name="item.content" :item="item" :index="index"></slot>
  25. <!-- #endif -->
  26. </view>
  27. <read-content :item="item" v-if="item.type == 'text'"></read-content>
  28. <view class="yingbing-reader-content-loading" :style="{'height': contentHeight + 'px'}" v-if="item.type == 'error'" @touchstart.stop.prevent @touchmove.stop.prevent @touchend.stop.prevent="handleReload(item)">
  29. <text class="yingbing-reader-content-loading-text" :style="{color: color}">{{errorText}}</text>
  30. </view>
  31. <view class="yingbing-reader-content-loading" :style="{'height': contentHeight + 'px'}" v-if="item.type == 'loading'">
  32. <reader-loading size="20px" :color="color || ''"></reader-loading>
  33. <text class="yingbing-reader-content-loading-text" :style="{color: color}">{{loadingText}}</text>
  34. </view>
  35. </template>
  36. </reader-scroller>
  37. </view>
  38. <reader-footer ref="footer" :total="currentTotal" :current="currentPage" v-if="footerShow && currentTotal"></reader-footer>
  39. </view>
  40. <yingbing-flip
  41. :class="{'yingbing-hidden': pageType == 'scroll'}"
  42. class="yingbing-flip-reader yingbing-reader-absolute"
  43. ref="flip"
  44. :autoplay="autoplaySync"
  45. :interval="interval"
  46. :current="current"
  47. :type="pageType"
  48. :data="pageType != 'scroll' ? pages : []"
  49. :background="background"
  50. :duration="300"
  51. :unableClickPage="unableClickPage"
  52. :pulldownable="!isLoading"
  53. :pullupable="!isLoading"
  54. @change="handleChange"
  55. @pulldown="handlePulldown"
  56. @pullup="handlePullup">
  57. <!-- #ifndef MP-->
  58. <template v-slot="{item, index}">
  59. <!-- #endif -->
  60. <!-- #ifdef MP -->
  61. <template v-for="(item, index) in pages" :slot="`wx:${index}`">
  62. <!-- #endif -->
  63. <view class="yingbing-reader-absolute yingbing-flip-reader-wrapper" :style="[wrapperStyle]">
  64. <reader-header :progress="item.progress" :title="item.title || title" v-if="getFlipHeaderShow(item)"></reader-header>
  65. <view class="yingbing-flip-reader-content">
  66. <template v-if="item.type == 'slot' && pageType != 'scroll'">
  67. <!-- 微信小程序vue2虽然支持在v-for中嵌套同名插槽但会一直报错引起页面卡顿vue3不支持在v-for中嵌套同名插槽所以增加2种微信小程序使用方式 -->
  68. <!-- #ifdef MP -->
  69. <slot :name="item.content + ':' + item.index"></slot>
  70. <!-- #endif -->
  71. <!-- #ifndef MP -->
  72. <slot :name="item.content" :item="item" :index="index"></slot>
  73. <!-- #endif -->
  74. </template>
  75. <read-content class="yingbing-reader-absolute" :item="item" v-if="item.type == 'text'"></read-content>
  76. <view class="yingbing-reader-absolute yingbing-reader-content-loading" v-if="item.type == 'error'" @touchstart.stop.prevent @touchmove.stop.prevent @touchend.stop.prevent="handleReload(item)">
  77. <text class="yingbing-reader-content-loading-text" :style="{color: color}">{{errorText}}</text>
  78. </view>
  79. <view class="yingbing-reader-absolute yingbing-reader-content-loading" v-if="item.type == 'loading'">
  80. <reader-loading size="20px" :color="color || ''"></reader-loading>
  81. <text class="yingbing-reader-content-loading-text" :style="{color: color}">{{loadingText}}</text>
  82. </view>
  83. </view>
  84. <reader-footer :total="item.total" :current="item.current" v-if="getFlipFooterShow(item)"></reader-footer>
  85. </view>
  86. </template>
  87. <template #pulldownDefault>
  88. <view class="loading-box">
  89. <text class="loading-text">{{isPulldownEnd ? prevChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : prevChapterDefaultText.split('').join('\n') }}</text>
  90. </view>
  91. </template>
  92. <template #pulldownReady>
  93. <view class="loading-box">
  94. <text class="loading-text">{{isPulldownEnd ? prevChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterReadyText.split('').join('\n') }}</text>
  95. </view>
  96. </template>
  97. <template #pulldownLoading>
  98. <view class="loading-box">
  99. <text class="loading-text">{{isPulldownEnd ? prevChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterLoadingText.split('').join('\n') }}</text>
  100. </view>
  101. </template>
  102. <template #pulldownSuccess>
  103. <view class="loading-box">
  104. <text class="loading-text">{{isPulldownEnd ? prevChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterSuccessText.split('').join('\n') }}</text>
  105. </view>
  106. </template>
  107. <template #pulldownFail>
  108. <view class="loading-box">
  109. <text class="loading-text">{{isPulldownEnd ? prevChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterFailText.split('').join('\n') }}</text>
  110. </view>
  111. </template>
  112. <template #pullupDefault>
  113. <view class="loading-box">
  114. <text class="loading-text">{{isPullupEnd ? nextChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : nextChapterDefaultText.split('').join('\n') }}</text>
  115. </view>
  116. </template>
  117. <template #pullupReady>
  118. <view class="loading-box">
  119. <text class="loading-text">{{isPullupEnd ? nextChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterReadyText.split('').join('\n') }}</text>
  120. </view>
  121. </template>
  122. <template #pullupLoading>
  123. <view class="loading-box">
  124. <text class="loading-text">{{isPullupEnd ? nextChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterLoadingText.split('').join('\n') }}</text>
  125. </view>
  126. </template>
  127. <template #pullupSuccess>
  128. <view class="loading-box">
  129. <text class="loading-text">{{isPullupEnd ? nextChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterSuccessText.split('').join('\n') }}</text>
  130. </view>
  131. </template>
  132. <template #pullupFail>
  133. <view class="loading-box">
  134. <text class="loading-text">{{isPullupEnd ? nextChapterEndText.split('').join('\n') : chapterLoading ? chapterLoadingText.split('').join('\n') : chapterFailText.split('').join('\n') }}</text>
  135. </view>
  136. </template>
  137. </yingbing-flip>
  138. <!-- 循环computed计算组件避免计算冲突 -->
  139. <computed
  140. :window-width="windowWidth"
  141. :window-height="windowHeight"
  142. ref="computed" v-for="(c, i) in computeds"
  143. :key="c"></computed>
  144. </view>
  145. </template>
  146. <script>
  147. import Computed from './computed.vue'
  148. import ReadContent from './content.vue'
  149. import ReaderLoading from '../loading/loading.vue'
  150. import ReaderHeader from '../header/header.vue'
  151. import ReaderFooter from '../footer/footer.vue'
  152. import ReaderScroller from '../scroller/scroller.vue'
  153. import FlipReaderMixin from './flip-reader.js'
  154. import ScrollReaderMixin from './scroll-reader.js'
  155. import TouchClickMixin from '../mixin/touch-click.js'
  156. import TipMixin from '../mixin/tip.js'
  157. const threshold = 50//点击事件阙值
  158. export default {
  159. options: {
  160. addGlobalClass: true,
  161. virtualHost: true, // 将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等,而是希望自定义组件内部的第一层节点能够响应 flex 布局或者样式由自定义组件本身完全决定
  162. },
  163. mixins: [FlipReaderMixin, ScrollReaderMixin, TouchClickMixin, TipMixin],
  164. components: {
  165. ReadContent,
  166. ReaderHeader,
  167. ReaderFooter,
  168. ReaderLoading,
  169. ReaderScroller,
  170. Computed
  171. },
  172. provide () {
  173. return {
  174. getColor: () => this.color,
  175. getFontSize: () => this.fontSize,
  176. getFontFamily: () => this.fontFamily,
  177. getLineGap: () => this.lineGap,
  178. getTopGap: () => this.topGap,
  179. getBottomGap: () => this.bottomGap,
  180. getSlide: () => this.slide,
  181. getHeaderShow: () => this.headerShow,
  182. getFooterShow: () => this.footerShow,
  183. getBackShow: () => this.backShow,
  184. getSplit: () => this.split,
  185. getTotalChapter: () => this.totalChapter,
  186. getSelectable: () => this.selectable,
  187. getPageType: () => this.pageType,
  188. getMeasureSize: () => this.measureSize,
  189. getCharSize: () => this.charSize
  190. }
  191. },
  192. props: {
  193. //自动翻页/滚动
  194. autoplay: {
  195. type: Boolean,
  196. default: false
  197. },
  198. //自动翻页/滚动周期
  199. interval: {
  200. type: [String, Number],
  201. default: 5000
  202. },
  203. //字体颜色
  204. color: {
  205. type: String,
  206. default: '#333333'
  207. },
  208. //字体大小(单位px)
  209. fontSize: {
  210. type: [String, Number],
  211. default: 15
  212. },
  213. //行间距(单位px)
  214. lineGap: {
  215. type: [Number, String],
  216. default: 15
  217. },
  218. //页面左右边距(单位px)
  219. slide: {
  220. type: [Number, String],
  221. default: 20
  222. },
  223. //页面上边距(单位px)
  224. topGap: {
  225. type: [Number, String],
  226. default: 10
  227. },
  228. //页面下边距(单位px)
  229. bottomGap: {
  230. type: [Number, String],
  231. default: 10
  232. },
  233. //展示头部
  234. headerShow: {
  235. type: Boolean,
  236. default: true
  237. },
  238. //展示底部
  239. footerShow: {
  240. type: Boolean,
  241. default: true
  242. },
  243. //展示返回图标
  244. backShow: {
  245. type: Boolean,
  246. default: false
  247. },
  248. //总章节数量 用于计算进度
  249. totalChapter: {
  250. type: [Number, String],
  251. default: 0
  252. },
  253. //字体名称
  254. fontFamily: {
  255. type: String,
  256. default: 'Arial'
  257. },
  258. //背景颜色
  259. background: {
  260. type: String,
  261. default: '#fcd281'
  262. },
  263. //开启预加载
  264. preloadable: {
  265. type: Boolean,
  266. default: false
  267. },
  268. //开启文本选择
  269. selectable: {
  270. type: Boolean,
  271. default: false
  272. },
  273. //还剩多少页时,开始加载下一章节
  274. loadPageNum: {
  275. type: [Number, String],
  276. default: 4
  277. },
  278. //是否关闭点击左右2侧位置翻页
  279. unableClickPage: {
  280. type: Boolean,
  281. default: false
  282. },
  283. //翻页方式
  284. pageType: {
  285. type: String,
  286. default: 'scroll'
  287. },
  288. //分隔符
  289. split: {
  290. type: String,
  291. default: ''
  292. },
  293. //字符尺寸
  294. measureSize: {
  295. type: Object,
  296. default () {
  297. return new Object
  298. }
  299. },
  300. //指定字符尺寸集合
  301. charSize: {
  302. type: Array,
  303. default () {
  304. return new Array
  305. }
  306. }
  307. },
  308. computed: {
  309. wrapperStyle () {
  310. return {
  311. 'padding-top': this.topGap + 'px',
  312. 'padding-bottom': this.bottomGap + 'px',
  313. 'background': this.background,
  314. 'padding-left': this.slide + 'px',
  315. 'padding-right': this.slide + 'px'
  316. }
  317. },
  318. //是否正在初始化或者跳转章节内容
  319. isLoading () {
  320. return this.pages.some(p => p.type == 'loading')
  321. },
  322. //是否渲染到第一章
  323. isPulldownEnd () {
  324. return this.pages.findIndex(c => c.isStart) > -1
  325. },
  326. //是否渲染到最后一章
  327. isPullupEnd () {
  328. return this.pages.findIndex(c => c.isEnd) > -1
  329. }
  330. },
  331. data () {
  332. return {
  333. computeds: [],//计算集合,为了避免计算冲突
  334. pages: [],//渲染页面集合
  335. chapters: [],//章节列表集合
  336. chapterLoading: false,//章节请求loading
  337. current: 0,//当前页面索引
  338. title: '',//小说标题
  339. contentHeight: 0//内容区域高度
  340. }
  341. },
  342. beforeDestroy () {
  343. this.abort()
  344. uni.$off('yingbing-reader-back')//清楚back监听
  345. },
  346. beforeCreate() {
  347. uni.$on('yingbing-reader-back', () => {
  348. this.$emit('back')
  349. })
  350. },
  351. methods: {
  352. //翻往上一页
  353. prev () {
  354. if ( this.current <= 0 ) {
  355. uni.showToast({
  356. title: '前面没有了',
  357. icon: 'none'
  358. })
  359. return
  360. }
  361. this.$refs.scroll && this.$refs.scroll.scrollToIndex(this.current - 1, true)//滚动模式
  362. this.$refs.flip && this.$refs.flip.flipToPrev()//翻页模式
  363. },
  364. //翻往下一页
  365. next () {
  366. if ( this.current >= this.pages.length - 1 ) {
  367. uni.showToast({
  368. title: '后面没有了',
  369. icon: 'none'
  370. })
  371. return
  372. }
  373. this.$refs.scroll && this.$refs.scroll.scrollToIndex(this.current + 1, true)//滚动模式
  374. this.$refs.flip && this.$refs.flip.flipToNext()//翻页模式
  375. },
  376. //页面刷新
  377. refresh () {
  378. this.abort()
  379. if ( this.pages.length == 0 ) return//如果页面数量为0不做操作
  380. const page = this.pages[this.current]//当前页面
  381. const current = page.index//当前页面章节索引
  382. const start = page.start//当前页面开始位置
  383. this.refreshTimer = setTimeout(() => {
  384. this.render({current, start})//调用change事件
  385. }, 300)
  386. },
  387. //初始化
  388. init ({chapters, title, current, start = 0}) {
  389. if ( !chapters || chapters.length == 0 ) {
  390. uni.showToast({
  391. title: '至少传入一个章节内容',
  392. icon: 'none'
  393. })
  394. return
  395. }
  396. this.abort()
  397. this.autoplaySync = false//关闭自动播放
  398. this.title = title || ''//存储小说标题
  399. this.chapters = chapters//存储传入的章节内容
  400. // this.pages = [{index: current, type: 'loading'}]//显示loading
  401. setTimeout(() => {
  402. const chapterIndex = this.chapters.findIndex(c => c.index == current)
  403. if ( chapterIndex == -1 ) current = this.chapters[0].index//强制转换类型为int
  404. this.render({current, start})
  405. }, 20)
  406. },
  407. //跳转章节
  408. change({chapters, current, start = 0}) {
  409. if ( !current ) {//current必、传
  410. uni.showToast({
  411. title: 'current必传',
  412. icon: 'none'
  413. })
  414. return
  415. }
  416. this.abort()
  417. this.autoplaySync = false//关闭自动播放
  418. if ( chapters && chapters.length > 0 ) {//如果传入章节内容
  419. chapters.forEach(chapter => {
  420. const index = this.chapters.findIndex(c => c.index == chapter.index)//是否已经包含相同章节
  421. if (index > -1) this.chapters[index] = chapter//如果包含则更新
  422. else this.chapters.push(chapter)//否则添加新章节
  423. })
  424. }
  425. setTimeout(() => {
  426. const chapterIndex = this.chapters.findIndex(c => c.index == current)//查找对应current的章节内容
  427. if ( chapterIndex > -1 ) this.render({current, start})//如果过包含开始渲染内容
  428. else this.handleChangeLoadmore(current, start)//如果不包含,抛出loadmore事件,去获取内容
  429. }, 20)
  430. },
  431. async render (data) {
  432. const rect = await this.getRect()//获取组件尺寸
  433. this.windowWidth = rect.width//记录组件宽度
  434. this.windowHeight = rect.height//记录组件高度
  435. const contentRect = await this.getContentRect()//获取内容尺寸
  436. this.contentHeight = contentRect.height//记录内容区域高度
  437. if ( this.pageType == 'scroll' ) this.scrollRender(data)
  438. else this.flipRender(data)
  439. },
  440. //重加载事件
  441. handleReload (item) {
  442. this.pages = [{
  443. type: 'loading',
  444. index: item.index,
  445. start: item.start || undefined//记录章节定位
  446. }]//显示刷新效果
  447. this.handleChangeLoadmore(item.index, item.start)//触发loadmore
  448. },
  449. //change事件触发loadmore加载更多章节并渲染
  450. handleChangeLoadmore (index, start) {
  451. this.loadmoreFunc = (status, chapter) => {
  452. //请求完成后删除请求集合中的章节索引
  453. if (status == 'success') {//获取内容成功
  454. this.render({chapter, current: index, start})
  455. } else {
  456. this.pages = [{
  457. type: 'error',
  458. index: index,
  459. start: start || undefined//记录章节定位
  460. }]//显示错误页面
  461. this.current = 0
  462. this.$refs.flip && this.$refs.flip.refresh()
  463. }
  464. }
  465. this.$emit('loadmore', index, this.loadmoreFunc.bind(this))
  466. },
  467. //翻页改变事件
  468. handleChange (e) {
  469. this.current = e.current//记录当前定位
  470. this.$emit('change', e)//抛出change事件
  471. if ( e.current >= this.pages.length - this.loadPageNum && !this.chapterLoading ) {//如果翻到了最后loadPageNum页,并且没有加载章节
  472. const page = this.pages[this.pages.length-1]//回去最后页面
  473. if ( page.isEnd ) return
  474. const index = page.index + 1//
  475. const chapterIndex = this.chapters.findIndex(c => c.index == index)//查找对应index的章节内容
  476. if ( chapterIndex > -1 ) {//如果过包含开始渲染内容
  477. this.chapterLoading = true//开启章节加载等待
  478. this.handleLoadRender('success', this.chapters[chapterIndex])//渲染章节
  479. } else {
  480. if ( this.preloadable ) {//开启了预加载功能
  481. this.chapterLoading = true//开启章节加载等待
  482. this.loadmoreFunc = (status, chapter) => {//如果不包含,抛出loadmore事件,去获取内容
  483. this.handleLoadRender(status, chapter)//渲染章节
  484. }
  485. this.$emit('loadmore', index, this.loadmoreFunc.bind(this))
  486. }
  487. }
  488. }
  489. },
  490. //渲染下一章节
  491. handleLoadRender (status, chapter, callback, isPrev) {
  492. if ( this.pageType == 'scroll' ) this.handleScrollLoadRender(status, chapter, callback, isPrev)
  493. else this.handleFlipLoadRender(status, chapter, callback, isPrev)
  494. },
  495. //上拉事件
  496. handlePullup (callback) {
  497. // if ( this.isPullupEnd || this.chapterLoading || this.isLoading || this.current < this.pages.length - 1 ) {//如果最后页面是结束章节或者当前正在等待加载新章节或者当前页面不是最后一页则返回
  498. // callback( this.isPullupEnd && this.pageType == 'scroll' ? 'end' : 'success')//如果是滚动模式并且章节全部加载关闭则返回end否则success
  499. // return
  500. // }
  501. if ( this.pageType == 'scroll' ) {
  502. if ( this.isPullupEnd || this.chapterLoading || this.isLoading ) {
  503. callback(this.isPullupEnd ? 'end' : 'ready')
  504. return
  505. }
  506. } else {
  507. if ( this.isPullupEnd || this.chapterLoading || this.isLoading || this.current < this.pages.length - 1 ) {//如果最后页面是结束章节或者当前正在等待加载新章节或者当前页面不是最后一页则返回
  508. callback('success')//如果是滚动模式并且章节全部加载关闭则返回end否则success
  509. return
  510. }
  511. }
  512. const index = this.pages[this.pages.length-1].index + 1
  513. const chapterIndex = this.chapters.findIndex(c => c.index == index)//查找对应index的章节内容
  514. this.chapterLoading = true//开启章节加载等待
  515. if ( chapterIndex > -1 ) {//如果过包含开始渲染内容
  516. this.handleLoadRender('success', this.chapters[chapterIndex], callback)//渲染章节
  517. } else {
  518. this.loadmoreFunc = (status, chapter) => {//如果不包含,抛出loadmore事件,去获取内容
  519. this.handleLoadRender(status, chapter, callback)//渲染章节
  520. }
  521. this.$emit('loadmore', index, this.loadmoreFunc.bind(this))
  522. }
  523. },
  524. //下拉事件
  525. handlePulldown (callback) {
  526. if ( this.isPulldownEnd || this.chapterLoading || this.isLoading ) {
  527. callback( this.isPullupEnd && this.pageType == 'scroll' ? 'end' : 'success')//如果是滚动模式并且章节全部加载关闭则返回end否则success
  528. return
  529. }
  530. const index = this.pages[0].index - 1
  531. const chapterIndex = this.chapters.findIndex(c => c.index == index)//查找对应index的章节内容
  532. this.chapterLoading = true//开启章节加载等待
  533. if ( chapterIndex > -1 ) {//如果过包含开始渲染内容
  534. this.handleLoadRender('success', this.chapters[chapterIndex], true)//渲染章节
  535. } else {
  536. this.loadmoreFunc = (status, chapter) => {//如果不包含,抛出loadmore事件,去获取内容
  537. this.handleLoadRender(status, chapter, callback, true)//渲染章节
  538. }
  539. this.$emit('loadmore', index, this.loadmoreFunc)
  540. }
  541. },
  542. //处理页面
  543. handlePages (arr) {
  544. arr = arr.filter(item => item.type != 'loading')//去掉加载页
  545. arr.sort((a, b) => a.index - b.index)//根据章节索引排序
  546. return arr
  547. },
  548. //清楚刷新定时器
  549. clearRefreshTimer () {
  550. if ( this.refreshTimer ) {
  551. clearTimeout(this.refreshTimer)
  552. this.refreshTimer = null
  553. }
  554. },
  555. //清楚loadmore回调
  556. clearLoadmoreFunc () {
  557. if ( this.loadmoreFunc ) this.loadmoreFunc = null
  558. },
  559. //中断正在进行的计算和请求
  560. abort () {
  561. this.clearRefreshTimer()//清空刷新定时器
  562. this.clearLoadmoreFunc()//清空请求回调
  563. this.computeds = []
  564. },
  565. //获取计算组件id
  566. getComputedId () {
  567. return (new Date().getTime() / 1000).toString() + (Math.random() * 100)
  568. },
  569. getRect () {
  570. return new Promise(resolve => {
  571. // #ifdef APP-NVUE
  572. uni.requireNativePlugin('dom').getComponentRect(this.$refs.yingbingReader, res => {
  573. resolve(res.size)
  574. })
  575. // #endif
  576. // #ifndef APP-NVUE
  577. uni.createSelectorQuery().in(this).select('.yingbing-reader').boundingClientRect(data => {
  578. resolve(data)
  579. }).exec();
  580. // #endif
  581. })
  582. },
  583. //获取内容区域高度
  584. getContentRect () {
  585. return new Promise(resolve => {
  586. // #ifdef APP-NVUE
  587. uni.requireNativePlugin('dom').getComponentRect(this.$refs.yingbingReaderContent, res => {
  588. resolve(res.size)
  589. })
  590. // #endif
  591. // #ifndef APP-NVUE
  592. uni.createSelectorQuery().in(this).select('.yingbing-reader-content').boundingClientRect(data => {
  593. resolve(data)
  594. }).exec();
  595. // #endif
  596. })
  597. }
  598. },
  599. watch: {
  600. pageType (newVal, oldVal) {
  601. if ( newVal == 'scroll' ) {//当翻页模式变为滚动模式时
  602. this.autoplaySync = false//暂时关闭自动播放
  603. this.$nextTick(function () {
  604. this.$refs.scroll && this.$refs.scroll.scrollToIndex(this.current)//定位滚动位置
  605. setTimeout(() => {
  606. this.autoplaySync = this.autoplay//开启自动播放
  607. }, 100)
  608. })
  609. }
  610. },
  611. //颜色改变时刷新当前页面,否则APP-VUE可能不会立即生效
  612. color () {
  613. if ( this.$refs.flip && this.pages[this.current] ) this.$refs.flip.forceUpdate(this.current)
  614. },
  615. fontSize () {
  616. this.$nextTick(function () {
  617. this.refresh()
  618. })
  619. },
  620. lineGap () {
  621. this.$nextTick(function () {
  622. this.refresh()
  623. })
  624. },
  625. slide () {
  626. this.$nextTick(function () {
  627. this.refresh()
  628. })
  629. },
  630. topGap () {
  631. this.$nextTick(function () {
  632. this.refresh()
  633. })
  634. },
  635. bottomGap () {
  636. this.$nextTick(function () {
  637. this.refresh()
  638. })
  639. },
  640. fontFamily () {
  641. this.$nextTick(function () {
  642. this.refresh()
  643. })
  644. },
  645. autoplay (newVal) {
  646. if ( this.pageType == 'scroll' ) {
  647. if ( !this.isLoading ) this.autoplaySync = newVal
  648. } else {
  649. this.autoplaySync = newVal
  650. }
  651. }
  652. }
  653. }
  654. </script>
  655. <style scoped>
  656. .yingbing-reader {
  657. /* #ifndef APP-NVUE */
  658. width: 100%;
  659. height: 100%;
  660. /* #endif */
  661. /* #ifdef APP-NVUE */
  662. flex: 1;
  663. /* #endif */
  664. position: relative;
  665. overflow: hidden;
  666. }
  667. .yingbing-reader-content-loading {
  668. display: flex;
  669. flex-direction: column;
  670. align-items: center;
  671. justify-content: center;
  672. }
  673. .yingbing-reader-content-loading-text {
  674. margin-top: 10px;
  675. font-size: 15px;
  676. }
  677. .yingbing-reader-content-loading-tip {
  678. margin-top: 10px;
  679. font-size: 12px;
  680. }
  681. .yingbing-hidden {
  682. visibility: hidden;
  683. }
  684. /* 滚动模式样式 */
  685. .yingbing-scroll-reader {
  686. /* #ifndef APP-NVUE */
  687. width: 100%;
  688. height: 100%;
  689. display: flex;
  690. flex-direction: column;
  691. box-sizing: border-box;
  692. /* #endif */
  693. /* #ifdef APP-NVUE */
  694. flex: 1;
  695. /* #endif */
  696. position: relative;
  697. }
  698. .yingbing-scroll {
  699. flex: 1;
  700. position: relative;
  701. }
  702. /* 翻页模式样式 */
  703. .yingbing-reader-absolute {
  704. position: absolute;
  705. left: 0;
  706. top: 0;
  707. right: 0;
  708. bottom: 0;
  709. }
  710. .yingbing-flip-reader .loading-box {
  711. background-color: rgba(0,0,0,.2);
  712. align-items: center;
  713. justify-content: center;
  714. /* #ifdef APP-NVUE */
  715. flex: 1;
  716. /* #endif */
  717. /* #ifndef APP-NVUE */
  718. display: flex;
  719. flex-direction: column;
  720. height: 100%;
  721. /* #endif */
  722. }
  723. .yingbing-flip-reader .loading-text {
  724. color: #ffffff;
  725. }
  726. .yingbing-flip-reader-wrapper {
  727. /* #ifndef APP-NVUE */
  728. display: flex;
  729. flex-direction: column;
  730. /* #endif */
  731. }
  732. .yingbing-flip-reader-content {
  733. position: relative;
  734. flex: 1;
  735. }
  736. </style>