rich.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <view class="wrap">
  3. <view class="main" v-if="richData">
  4. <view class="b-head">
  5. <view class="b-title">{{ richData.name }}</view>
  6. <view class="b-info">
  7. <view class="s-time">{{ richData.course_name }}</view>
  8. <view class="s-views">
  9. <view class="iconfont icon-eye-fill"></view>
  10. <view class="s-cont">{{ richData.count_load }}</view>
  11. </view>
  12. </view>
  13. </view>
  14. <view class="b-rich">
  15. <rich-text :nodes="contentRich"></rich-text>
  16. </view>
  17. <view class="b-from" v-if="richData.from || richData.author">
  18. <view class="s-from" v-if="richData.from">来源: {{ richData.from }}</view>
  19. <view class="s-author" v-if="richData.author">作者: {{ richData.author }}</view>
  20. </view>
  21. </view>
  22. <!-- 加载中 -->
  23. <load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
  24. <view class="read-state" :class="{ 'f-full': playState.totalPlayTime >= 3600 }">
  25. <view class="s-tit">阅读时长:</view>
  26. <view class="s-time">{{ readTime }}</view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import { syncPlayState, getContentDetail } from '@/service/api/course.js';
  32. import { getTimestamp } from '@/common/util.js';
  33. import mixinsCommon from '@/mixins/common.js';
  34. import { formatRichText } from '@/common/util.js';
  35. export default {
  36. mixins: [mixinsCommon],
  37. data() {
  38. return {
  39. richData: null,
  40. playState: {
  41. intervalTimer: 0,
  42. diffTime: 0,
  43. currentOffset: 0,
  44. playTime: 0,
  45. totalPlayTime: 0
  46. },
  47. loadingType: 1,
  48. loadingText: ''
  49. };
  50. },
  51. computed: {
  52. contentRich() {
  53. if (!this.richData) {
  54. return '';
  55. }
  56. return formatRichText(this.richData.content);
  57. },
  58. readTime() {
  59. const second = this.playState.totalPlayTime;
  60. let timeStr = '';
  61. // 转换为时
  62. let h = parseInt((second / 60 / 60) % 24);
  63. if (h > 0) {
  64. h = h < 10 ? '0' + h : h;
  65. timeStr += h + ':';
  66. }
  67. // 转换为分
  68. let m = parseInt((second / 60) % 60);
  69. m = m < 10 ? '0' + m : m;
  70. timeStr += m + ':';
  71. // 转换成秒
  72. let s = parseInt(second % 60);
  73. s = s < 10 ? '0' + s : s;
  74. timeStr += s;
  75. return timeStr;
  76. }
  77. },
  78. onLoad(options) {
  79. this.contentId = options.cid;
  80. this.loadDetail();
  81. if (!this.playState.intervalTimer) {
  82. this.watchPlayState();
  83. }
  84. },
  85. onUnload() {
  86. console.log('onUnload', this.playState.intervalTimer);
  87. if (this.playState.intervalTimer > 0) {
  88. console.log('clear intervalTimer');
  89. clearInterval(this.playState.intervalTimer);
  90. this.playState.intervalTimer = 0;
  91. }
  92. },
  93. methods: {
  94. loadDetail() {
  95. getContentDetail(this.contentId).then(([err, res]) => {
  96. console.log('getContentDetail', err, res);
  97. if (!err) {
  98. this.loadingType = -1;
  99. this.richData = res;
  100. this.playState.diffTime = res.server_time - getTimestamp();
  101. uni.setNavigationBarTitle({
  102. title: res.name
  103. });
  104. if (res.learn_data) {
  105. this.playState.totalPlayTime = res.learn_data.total_play_time;
  106. if (res.learn_data.current_offset > 0) {
  107. setTimeout(() => {
  108. uni.pageScrollTo({
  109. scrollTop: res.learn_data.current_offset
  110. });
  111. }, 500);
  112. }
  113. }
  114. } else {
  115. this.loadingType = 3;
  116. this.loadingText = err.data.msg || '加载失败';
  117. }
  118. });
  119. },
  120. watchPlayState() {
  121. console.log('watchPlayState', this.playState);
  122. this.playState.playTime = 0;
  123. this.playState.intervalTimer = setInterval(() => {
  124. // console.log('playTime', this.playState.playTime)
  125. this.playState.playTime++;
  126. this.playState.totalPlayTime++;
  127. if (this.playState.playTime >= 5) {
  128. console.log('upload playState ing');
  129. this.uploadPlayState();
  130. }
  131. }, 1000);
  132. },
  133. uploadPlayState() {
  134. console.log('uploadPlayState', this.playState);
  135. if (this.richData && !this.richData.trial_view) {
  136. const syncTime = getTimestamp() + this.playState.diffTime;
  137. syncPlayState(this.richData.course_id, this.richData.id, 0, this.playState.currentOffset, this.playState.playTime, syncTime).then(([err, res]) => {
  138. console.log('syncPlayState', err, res);
  139. });
  140. }
  141. this.playState.playTime = 0;
  142. }
  143. },
  144. onPageScroll(e) {
  145. this.playState.currentOffset = e.scrollTop;
  146. }
  147. };
  148. </script>
  149. <style lang="scss">
  150. page {
  151. padding-bottom: env(safe-area-inset-bottom);
  152. background: $pq-bg-color;
  153. }
  154. .wrap {
  155. background: #fff;
  156. }
  157. .main {
  158. padding: 20upx;
  159. }
  160. .b-head {
  161. margin-top: 20upx;
  162. .b-title {
  163. font-size: 30upx;
  164. color: #333;
  165. }
  166. .b-info {
  167. margin-top: 20upx;
  168. display: flex;
  169. justify-content: space-between;
  170. .s-time {
  171. font-size: 26upx;
  172. color: #999;
  173. }
  174. .s-views {
  175. display: flex;
  176. align-items: center;
  177. .iconfont {
  178. margin-right: 10upx;
  179. font-size: 30upx;
  180. color: #eee;
  181. }
  182. .s-cont {
  183. font-size: 26upx;
  184. color: #999;
  185. }
  186. }
  187. }
  188. }
  189. .b-rich {
  190. margin-top: 50upx;
  191. background: #fff;
  192. font-size: 28upx;
  193. min-height: 500upx;
  194. }
  195. .b-from {
  196. margin-top: 20upx;
  197. display: flex;
  198. justify-content: space-between;
  199. font-size: 24upx;
  200. color: #999;
  201. }
  202. .read-state {
  203. position: fixed;
  204. padding: 0 20upx;
  205. right: 20upx;
  206. bottom: 20upx;
  207. background: rgba(0, 0, 0, 0.5);
  208. width: 215upx;
  209. height: 60upx;
  210. border-radius: 10upx;
  211. display: flex;
  212. align-items: center;
  213. justify-content: space-between;
  214. color: #fff;
  215. &.f-full {
  216. width: 210upx;
  217. }
  218. .s-tit {
  219. font-size: 24upx;
  220. }
  221. .s-time {
  222. font-size: 24upx;
  223. }
  224. }
  225. </style>