企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[toc] # 3.10 弹幕组件开发(三) - 发送弹幕 ## 3.10.1 封装弹幕方法 ``` <script> export default { data(){ return { scrollIntoView : "", list : [ ] } }, mounted(){ // let id = 1; // //由于弹幕的数据都是从后端传递过来了,所以我们在这只是先进行模拟一下 // setInterval(()=>{ // this.list.push({ // id : id, // name : "昵称" + id, // content : "九月直播666" // }) // //调用置于底部方法 // this.toBottom(); // id++ // },1000) }, methods:{ //发送弹幕方法 send(data){ this.list.push(data) this.toBottom() }, //置于底部风发 toBottom(){ setTimeout(()=>{ let len = this.list.length; if(len > 0 && this.list[len - 1]){ this.scrollIntoView = 'danmu' + this.list[len - 1].id; } },200) } } } </script> ``` ## 3.10.2 在live.nvue组件调用封装的方法 ``` <!-- 弹幕 --> <f-danmu ref="danmu"></f-danmu> ``` ``` <script> import fGift from "../../components/live/f-gift.vue" import uniPopup from "../../components/uni-ui/uni-popup/uni-popup.vue" import fDanmu from "../../components/live/f-danmu.vue" export default { data(){ return { statusBarHeight : 0, content : "" } }, components: { fGift, uniPopup, fDanmu }, onLoad(){ //获取通知栏的高度 let res = uni.getSystemInfoSync(); this.statusBarHeight = res.statusBarHeight; }, mounted(){ //模拟发送礼物 setInterval(()=>{ this.$refs.gift.send({ username : "发送人", avatar : "", gift_name : "蛋糕", gift_image : "/static/gift/3.png", num : 1 }) },3000) //模拟发送弹幕 let id = 1; setInterval(()=>{ this.$refs.danmu.send({ id : id, name : "昵称" + id, content : "九月直播666" }) id++; },3000) }, methods:{ openInput(){ this.$refs.input.open() }, submit(){ this.$refs.input.close() } } } </script> ``` ## 3.10.3 判断提交弹幕的内容为空,则输入提示框不消失 ``` submit(){ if(this.content == ""){ return; } this.$refs.input.close() } ``` ## 3.10.4 设置弹幕的内容为输入框提交的内容 ``` <script> import fGift from "../../components/live/f-gift.vue" import uniPopup from "../../components/uni-ui/uni-popup/uni-popup.vue" import fDanmu from "../../components/live/f-danmu.vue" export default { data(){ return { statusBarHeight : 0, content : "" } }, components: { fGift, uniPopup, fDanmu }, onLoad(){ //获取通知栏的高度 let res = uni.getSystemInfoSync(); this.statusBarHeight = res.statusBarHeight; }, mounted(){ //模拟发送礼物 setInterval(()=>{ this.$refs.gift.send({ username : "发送人", avatar : "", gift_name : "蛋糕", gift_image : "/static/gift/3.png", num : 1 }) },3000) //模拟发送弹幕 // let id = 1; // setInterval(()=>{ // this.$refs.danmu.send({ // id : id, // name : "昵称" + id, // content : "九月直播666" // }) // id++; // },3000) }, methods:{ openInput(){ this.$refs.input.open() }, submit(){ //发送内容为空,提示框不消失 if(this.content == ""){ return; } //发送弹幕 this.$refs.danmu.send({ id : Math.random() * 100, name : "昵称", content : this.content }) //清空输入的内容 this.content = ""; //关闭输入提示框 this.$refs.input.close() } } } </script> ``` ## 3.10.5 设置当输入提示框内容为空时,发送按钮禁用 ``` <!-- 输入框弹出层 --> <uni-popup type="bottom" ref="input"> <view class="bg-white flex align-center px-3" style="height: 120rpx;"> <input placeholder="说点什么呢..." v-model="content" type="text" style="height: 80rpx;" class="border rounded flex-1 px-3 font-md"> <view :class="content == '' ? 'bg-main-disabled' : 'bg-main'" class=" flex ml-3 align-center py-2 rounded px-2 justify-center" @click="submit"> <text class="font text-white">发送</text> </view> </view> </uni-popup> ``` ## 3.10.6 实现关闭直播间返回上一页功能 ``` <template> <view class="page"> <!-- 直播内容 --> <video class="flex-1" src="http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8" autoplay :controls="false"></video> <!-- 头部 --> <view style="position: fixed; left: 0; right: 0;" :style="{top : `${statusBarHeight}px`}"> <!-- 个人信息|观看详细信息 --> <view class="px-2 flex justify-between" style="height:80rpx;"> <!-- 个人信息 --> <view style="width: 325rpx; background-color: rgba(0,0,0,0.4);" class="flex align-center rounded-circle"> <view class="p"> <image src="../../static/tabbar/min.png" style="width: 70rpx; height: 70rpx;" class="rounded-circle"></image> </view> <!-- 昵称与浏览量 --> <view class="flex-1 flex flex-column justify-center"> <text class="text-white font">昵称</text> <text class="text-white font-sm">100</text> </view> <!-- 关注按钮 --> <view class="rounded-circle flex align-center bg-danger justify-center" style="width: 70rpx; height: 70rpx;"> <text class="text-white">+</text> </view> </view> <!-- 观看情况 --> <view style="width: 325rpx; background-color: rgba(0,0,0,0.4);" class="flex align-center rounded-circle"> <!-- 观看的用户 --> <scroll-view scroll-x="true" class="flex-1 flex"> <view class="p" v-for="(item,index) in 20" :key="index"> <image src="../../static/tabbar/min.png" style="width: 70rpx; height: 70rpx;" class="rounded-circle"></image> </view> </scroll-view> <!-- 观看人数 --> <view class="rounded-circle flex align-center bg-danger justify-center" style="width: 70rpx; height: 70rpx;"> <text class="text-white font-sm">1000</text> </view> </view> </view> <!--金币--> <view class="px-2 my-2" style="height: 80rpx;"> <view style="width: 325rpx; background-color: rgba(0,0,0,0.4);" class="flex align-center rounded-circle"> <view class="p"> <text class="text-warning">金币</text> </view> <view class="flex-1 flex flex-column justify-center"> <text class="text-white font">100</text> </view> </view> </view> <!-- 礼物 --> <f-gift ref="gift"></f-gift> </view> <!-- 弹幕 --> <f-danmu ref="danmu"></f-danmu> <!-- 底部评论..等 --> <view class="position-fixed right-0 bottom-0 left-0 flex align-center justify-between" style="height: 120rpx;"> <view class="px-2" > <view @click="openInput" style="height:80rpx;border-radius: 100rpx;background-color: rgba(255,255,255,0.12);" class="flex justify-center px-3 align-center"> <text class="text-white font">说点什么...</text> </view> </view> <view class="flex align-center"> <view style="height:80rpx; width: 80rpx; background-color: rgba(255,255,255,0.12);" class="flex mr-1 justify-center rounded-circle align-center"> <text class="iconfont text-white" style="font-size: 40px; ">&#xe633;</text> </view> <view style="height:80rpx; width: 80rpx;" class="flex mr-1 justify-center rounded-circle align-center bg-warning"> <text class="iconfont">&#xe67c;</text> </view> <view style="height:80rpx; width: 80rpx; background-color: rgba(255,255,255,0.12);" class="flex mr-1 justify-center rounded-circle align-center"> <text class="iconfont text-white">&#xe7cd;</text> </view> <view @click="back" style="height:80rpx; width: 80rpx; background-color: rgba(255,255,255,0.12);" class="flex mr-1 justify-center rounded-circle align-center"> <text class="iconfont text-white">&#xe607;</text> </view> </view> </view> <!-- 输入框弹出层 --> <uni-popup type="bottom" ref="input"> <view class="bg-white flex align-center px-3" style="height: 120rpx;"> <input placeholder="说点什么呢..." v-model="content" type="text" style="height: 80rpx;" class="border rounded flex-1 px-3 font-md"> <view :class="content == '' ? 'bg-main-disabled' : 'bg-main'" class=" flex ml-3 align-center py-2 rounded px-2 justify-center" @click="submit"> <text class="font text-white">发送</text> </view> </view> </uni-popup> </view> </template> <script> import fGift from "../../components/live/f-gift.vue" import uniPopup from "../../components/uni-ui/uni-popup/uni-popup.vue" import fDanmu from "../../components/live/f-danmu.vue" export default { data(){ return { statusBarHeight : 0, content : "" } }, components: { fGift, uniPopup, fDanmu }, onLoad(){ //获取通知栏的高度 let res = uni.getSystemInfoSync(); this.statusBarHeight = res.statusBarHeight; }, mounted(){ //模拟发送礼物 setInterval(()=>{ this.$refs.gift.send({ username : "发送人", avatar : "", gift_name : "蛋糕", gift_image : "/static/gift/3.png", num : 1 }) },3000) //模拟发送弹幕 // let id = 1; // setInterval(()=>{ // this.$refs.danmu.send({ // id : id, // name : "昵称" + id, // content : "九月直播666" // }) // id++; // },3000) }, methods:{ //关闭直播间,返回上一页 back(){ uni.navigateBack({ delta: 1 }) }, openInput(){ this.$refs.input.open() }, submit(){ //发送内容为空,提示框不消失 if(this.content == ""){ return; } //发送弹幕 this.$refs.danmu.send({ id : Math.random()*100, name : "昵称", content : this.content }) //清空输入的内容 this.content = ""; //关闭输入提示框 this.$refs.input.close() } } } </script> <style> .page{ flex : 1; } </style> ```