DanMu.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view style="position: relative;width: 750rpx;height: 360rpx;padding-top: 20rpx;">
  3. <view style="height:350rpx">
  4. <l-barrage :data="barrageData1">
  5. <!-- 自定义每条弹幕样式 content 为你传入的数据-->
  6. <template #default="{content}">
  7. <view class="" style="height: 60rpx;">
  8. <view
  9. style="display: flex;background: rgba(0,0,0,0.2);height: 36rpx;padding: 0 10rpx;line-height: 36rpx;border-radius: 4rpx;">
  10. <view class="" style="overflow: hidden;color: aliceblue;display: flex;"
  11. v-if="content.scene==1">
  12. 感恩
  13. <view class="">
  14. <image style="width: 32rpx;height: 32rpx;border-radius: 50%;margin-top: 4rpx;"
  15. :src="content.user_avatar" mode=""></image>
  16. </view>
  17. <span style="color: red;">{{content.user_name}}</span>祝福
  18. <span style="color: #FFC690;">{{content.receive_user_name}}</span>
  19. <view class="">
  20. <image style="width: 32rpx;height: 32rpx;border-radius: 50%;margin-top: 4rpx;"
  21. :src="content.icon_url" mode=""></image>
  22. </view>x{{content.pay_num}}
  23. </view>
  24. <view class="" style="overflow: hidden;color: aliceblue;" v-if="content.scene==3">
  25. 感恩
  26. <!-- <view class="">
  27. <image style="width: 32rpx;height: 32rpx;border-radius: 50%;margin-top: 4rpx;" :src="content.avatar" mode=""></image>
  28. </view> -->
  29. <span style="color: red;">{{content.user_name}}</span>送出
  30. <!-- <view class="">
  31. <image style="width: 32rpx;height: 32rpx;border-radius: 50%;margin-top: 4rpx;"
  32. src="../../static/hua.png" mode=""></image>
  33. </view> -->
  34. 🌹x{{content.pay_num}}
  35. </view>
  36. </view>
  37. </view>
  38. <view class="" style="height: 80rpx;font-size: 36rpx;">
  39. <view class="" style="overflow: hidden;color: #FFE21D;display: flex;background: rgba(0,0,0,0.2);height: 66rpx;padding: 0 10rpx;line-height: 66rpx;border-radius: 4rpx;font-weight: 600;" v-if="content.scene==2">
  40. 恭喜阳光少年
  41. <span style="color: #E45050;">{{content.receive_user_name}}</span>
  42. 成为阳光CHO
  43. ,感恩所有为<span style="color: #E45050;">{{content.receive_user_name}}</span>送出的祝福👏🎉
  44. <!-- <view class="">
  45. <image style="width: 32rpx;height: 32rpx;border-radius: 50%;margin-top: 4rpx;"
  46. :src="content.icon_url" mode=""></image>
  47. </view>x{{content.pay_num}} -->
  48. </view>
  49. </view>
  50. </template>
  51. </l-barrage>
  52. </view>
  53. <!-- <view class="inputs">
  54. <input v-model="value" />
  55. <button @click="onFire">发射</button>
  56. </view> -->
  57. </view>
  58. </template>
  59. <script setup>
  60. // 本页面使用第三方插件,文档地址是 https://ext.dcloud.net.cn/plugin?id=11800
  61. // 文档名称 barrage 弹幕 (HB插件市场搜索)
  62. import {
  63. ref
  64. } from 'vue'
  65. import {
  66. HTTP
  67. } from '@/api/http.js'
  68. import {
  69. onShow,
  70. onHide
  71. } from '@dcloudio/uni-app'
  72. let hide = true
  73. const value = ref('')
  74. const barrageData1 = ref([])
  75. let b = 0 //播报哪一条
  76. const onFire = (e) => {
  77. if (hide == false) {
  78. return
  79. }
  80. if (barrageData.value.length == 0) {
  81. return
  82. }
  83. // if(b>=barrageData.value.length-2){
  84. // b=0
  85. // }
  86. // console.log(b,barrageData.value[b])
  87. barrageData1.value.push(barrageData.value[b])
  88. b++
  89. // console.log(111111)
  90. if (b >= barrageData.value.length - 1) {
  91. b = 0
  92. // barrageData1.value = []
  93. timeJianGe = getRandomIntInRange()
  94. time()
  95. } else {
  96. timeJianGe = getRandomIntInRange()
  97. time()
  98. }
  99. }
  100. // 获取播报列表
  101. const barrageData = ref([
  102. // {
  103. // "user_flora_id": 6,
  104. // "user_id": 1,
  105. // "user_flora_queue_id": 29,
  106. // "pay_num": 1,
  107. // "user_avatar": "https://img2.baidu.com/it/u=1195773901,4039087122&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750",
  108. // "user_name": "张三李四",
  109. // "receive_user_name": "刘祉昊",
  110. // "content": "感恩17600525201为阳光少年刘祉昊送上祝福👏👏👏👏👏",
  111. // "flora": "晚香玉",
  112. // "icon_url": "https://img2.baidu.com/it/u=1195773901,4039087122&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750",
  113. // "scene": 1
  114. // }
  115. ])
  116. // for (let i = 0; i < 200; i++) {//假数据
  117. // barrageData.value.push(barrageData.value[0])
  118. // // barrageData.value[i].user_name= maskString(barrageData.value[i].user_name)
  119. // }
  120. // 隐藏部分姓名
  121. function maskString(str) {
  122. // if (str.length <= 4) {
  123. // // 根据长度不同进行不同的替换
  124. // switch (str.length) {
  125. // case 2:
  126. // return str[0] + "*";
  127. // case 3:
  128. // return str[0] + "*" + str[2];
  129. // case 4:
  130. // return str.slice(0, 2) + "**";
  131. // default:
  132. // return str;
  133. // }
  134. // } else {
  135. // // 长度超过4个,替换中间的字符
  136. // return str[0] + "**" + str[str.length - 1];
  137. // }
  138. }
  139. const BroadcastIndex = async () => { //获取弹幕列表
  140. let res = await HTTP.SY.BroadcastIndex({})
  141. if (res.code == 200) {
  142. barrageData.value = res.data
  143. // barrageData.value.push(res.data[0])
  144. time()
  145. }
  146. // console.log(res, '我是真实弹幕啊', barrageData.value)
  147. }
  148. // 处理弹幕播报逻辑-----------------------
  149. let timeJianGe = 200
  150. const time = () => {
  151. // console.log(11111111111111)
  152. let times = setTimeout(() => {
  153. onFire(b)
  154. clearInterval(times)
  155. }, timeJianGe)
  156. }
  157. // 返回随机数200-300
  158. function getRandomIntInRange(min, max) {
  159. let a = 1500
  160. let b = 200
  161. // console.log("1111111111----------")
  162. if (barrageData.value.length < 10) {
  163. // console.log("2222222222222222222222222222222222")
  164. a = 15000
  165. b = 9000
  166. }
  167. if (barrageData.value.length >= 10 && barrageData.value.length < 20) {
  168. a = 11000
  169. b = 7000
  170. }
  171. if (barrageData.value.length >= 20 && barrageData.value.length < 70) {
  172. a = 3000
  173. b = 700
  174. }
  175. if (barrageData.value.length >= 70 && barrageData.value.length < 100) {
  176. a = 2000
  177. b = 300
  178. }
  179. // 生成一个随机整数,并确保其在 [min, max] 范围内
  180. const range = a - b + 1;
  181. const array = new Uint32Array(1); // 创建一个长度为 1 的 Uint32Array
  182. window.crypto.getRandomValues(array); // 填充数组
  183. return b + (array[0] % range); // 计算随机整数并返回
  184. }
  185. // ------------------------------------------------------
  186. onShow(() => {
  187. BroadcastIndex()
  188. })
  189. onHide(() => { //离开页面时停止循环
  190. hide = false
  191. })
  192. </script>
  193. <style lang="scss" scoped>
  194. // 弹幕
  195. .daMu {
  196. position: absolute;
  197. display: flex;
  198. background: rgba(0, 0, 0, 0.3);
  199. font-weight: 400;
  200. font-size: 24rpx;
  201. color: #FFFFFF;
  202. padding: 4rpx 16rpx;
  203. border-radius: 8rpx;
  204. }
  205. </style>