lz-red-book.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view class="lzRedBook" :style="[{height:set.screenHeight+'px'},bookStyle]" @click.stop="clickCenter">
  3. <!-- <view class="center">{{ bookText }}</view> -->
  4. <swiper class="swiper-box" @change="change" @animationfinish="animationfinish" :current="0" :vertical="scrollDirection=='leftRight'?false:true" :skip-hidden-item-layout="true" :style="{height:set.screenHeight+'px'}">
  5. <swiper-item v-for="(item ,index) in swiper.bookTextArr" :key="index">
  6. <view class="swiper-item">
  7. <view class="swiper-text">
  8. {{item}}
  9. </view>
  10. <!-- 底部电池部分 -->
  11. <view class="item-footer" :style="bookStyle">
  12. <view class="f-left">
  13. <text class="iconfont icon-dianchi"><text class="icon-dianchi-center" :style="{ width: system.quantity + '%' }"></text></text>
  14. <text class="time">{{ system.time }}</text>
  15. </view>
  16. <view class="f-center"></view>
  17. <view class="f-right">
  18. {{swiper.pageNum}}/{{swiper.bookTextArr.length}}
  19. </view>
  20. </view>
  21. </view>
  22. </swiper-item>
  23. </swiper>
  24. <!-- 这里是动态计算一页多少字的 -->
  25. <view class="opcity" >{{bookTextNew}}</view>
  26. </view>
  27. </template>
  28. <script>
  29. let DLTime, timeInter;
  30. let copyBookText='',forNum=100,addNum=400,reduceNum=400//默认一页从100个字开始计算,超出就减少,少了就增加
  31. export default {
  32. props: {
  33. bookText: {
  34. type: String,
  35. default: ''
  36. },
  37. bookStyle: {
  38. //书的样式
  39. type: Object,
  40. default: function() {
  41. return {};
  42. }
  43. },
  44. scrollDirection: {
  45. //滚动方向leftRight左右,上下topBottom
  46. type: String,
  47. default: 'leftRight'
  48. },
  49. },
  50. data() {
  51. return {
  52. set:{
  53. screenHeight: '', //屏幕宽度
  54. screenWidth:'',//屏幕宽度
  55. boxHight:0,
  56. },
  57. bookTextNew:'',
  58. swiper:{
  59. bookTextArr:[],//书的文本格式化(以每页多少字为数组的一项
  60. pageNum:1,
  61. vertical:false,//滑动方向是否为纵向
  62. mode: 'round',
  63. isEnd:false,//是否滑到最后一个了触发请求下一个数据
  64. isStart:false,//是否滑到最开始的地方
  65. },
  66. isEndFor:false,//是否循环结束
  67. system: {
  68. quantity: 0, //系统电量
  69. time: '' //系统时间
  70. },
  71. };
  72. },
  73. mounted() {
  74. //初始化
  75. this.init()
  76. // #ifdef APP-PLUS
  77. //每分钟获取一次电量
  78. this.dianliang();
  79. DLTime = setInterval(() => {
  80. this.dianliang();
  81. }, 60000);
  82. //每秒获取一次时间
  83. timeInter = setInterval(() => {
  84. this.getTimes();
  85. }, 1000);
  86. // #endif
  87. },
  88. methods: {
  89. //初始化
  90. init(){
  91. let that=this
  92. that.isEndFor=false
  93. copyBookText=this.bookText
  94. that.swiper.bookTextArr=[]
  95. // 获取元素信息
  96. that.getSystemInfo();
  97. },
  98. //获取系统信息
  99. getSystemInfo() {
  100. let that = this;
  101. uni.getSystemInfo({
  102. success: function(res) {
  103. that.set.screenHeight = res.screenHeight;
  104. that.set.screenWidth = res.screenWidth;
  105. //动态计算每一页文字数
  106. if(copyBookText.length>100){
  107. forNum=100
  108. that.forGet()//默认100个字起步
  109. }else{
  110. that.isEndFor=true
  111. that.swiper.bookTextArr.push(copyBookText)
  112. }
  113. }
  114. });
  115. },
  116. //循环获取一页多少文字,默认从100个字,然后动态获取文字内容高度与屏幕高度对比,如果文字高度小于屏幕高度,就加40个字
  117. forGet(){
  118. let that=this
  119. that.bookTextNew=copyBookText.substr(0,forNum)
  120. let String=copyBookText.substr(forNum)
  121. if(String.length>0){//如果一章大于100个字
  122. that.$nextTick(function(){
  123. var opcity = uni.createSelectorQuery().select('.opcity');
  124. opcity.boundingClientRect(data => {
  125. if(data.height<that.set.screenHeight-60 && that.isEndFor==false){
  126. forNum+=50
  127. that.forGet()
  128. }else{
  129. that.swiper.bookTextArr.push(that.bookTextNew)
  130. //拿到除去上一页的文字
  131. copyBookText=copyBookText.substr(forNum)
  132. //动态计算每一页文字数
  133. that.getNextPage()
  134. }
  135. }).exec();
  136. })
  137. }else{
  138. console.log(copyBookText);
  139. that.isEndFor=true
  140. that.swiper.bookTextArr.push(copyBookText)
  141. }
  142. },
  143. //动态计算每一页文字数
  144. getNextPage(){
  145. let that=this
  146. // debugger
  147. if(copyBookText.length>100){
  148. forNum=100
  149. that.forGet()//默认100个字起步
  150. }else{
  151. that.isEndFor=true
  152. that.swiper.bookTextArr.push(copyBookText)
  153. }
  154. console.log(that.swiper.bookTextArr);
  155. },
  156. //滑动时触发
  157. change(e) {
  158. console.log(e);
  159. this.swiper.pageNum = e.detail.current+1;
  160. this.swiper.isEnd=false
  161. this.swiper.isStart=false
  162. },
  163. animationfinish(){
  164. if(this.swiper.isEnd){
  165. console.log('已经划到最后一个了');
  166. this.$emit('scrollEnd');
  167. }
  168. if(this.swiper.isStart){
  169. console.log('已经划到最开始');
  170. this.$emit('scrollStart');
  171. }
  172. if(this.swiper.pageNum==this.swiper.bookTextArr.length){
  173. this.swiper.isEnd=true//已经划到最后
  174. }else{
  175. this.swiper.isEnd=false
  176. }
  177. if(this.swiper.pageNum==1){
  178. this.swiper.isStart=true//已经划到最开始
  179. }else{
  180. this.swiper.isStart=false
  181. }
  182. },
  183. //获取系统电量
  184. dianliang() {
  185. var that = this;
  186. if (uni.getSystemInfoSync().platform != 'ios') {
  187. var main = plus.android.runtimeMainActivity();
  188. var Intent = plus.android.importClass('android.content.Intent');
  189. var recevier = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
  190. onReceive: function(context, intent) {
  191. var action = intent.getAction();
  192. if (action == Intent.ACTION_BATTERY_CHANGED) {
  193. var level = intent.getIntExtra('level', 0); //电量
  194. var voltage = intent.getIntExtra('voltage', 0); //电池电压
  195. var temperature = intent.getIntExtra('temperature', 0); //电池温度
  196. that.system.quantity = level; //电量
  197. }
  198. }
  199. });
  200. var IntentFilter = plus.android.importClass('android.content.IntentFilter');
  201. var filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  202. main.registerReceiver(recevier, filter);
  203. } else {
  204. var UIDevice = plus.ios.import('UIDevice');
  205. var dev = UIDevice.currentDevice();
  206. if (!dev.isBatteryMonitoringEnabled()) {
  207. dev.setBatteryMonitoringEnabled(true);
  208. }
  209. var level = dev.batteryLevel();
  210. that.system.quantity = level * 100;
  211. }
  212. },
  213. getTimes() {
  214. var times = new Date();
  215. this.system.time =
  216. (times.getHours() < 10 ? '0' + times.getHours() : times.getHours()) + ':' + (times.getMinutes() < 10 ? '0' + times.getMinutes() : times.getMinutes());
  217. },
  218. //点击中间位置
  219. clickCenter() {
  220. this.swiper.isStart=false
  221. this.$emit('clickCenter');
  222. }
  223. }
  224. };
  225. </script>
  226. <style scoped lang="scss">
  227. .lzRedBook {
  228. font-size: 18px;overflow: hidden;
  229. .opcity{
  230. background: red;overflow: hidden;padding: 30px;box-sizing: border-box;opacity: 0;
  231. }
  232. .swiper-text{
  233. padding:20px;box-sizing: border-box;
  234. }
  235. .swiper-box{
  236. width: 100%;
  237. }
  238. .item-footer {
  239. position: absolute;bottom: 0;width: 100%;height: 20px;display: flex;justify-content: space-between;align-items: center;padding:0 20px;font-size: 10px!important;
  240. }
  241. }
  242. </style>