💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[toc] # 3.12 送礼物弹框组件(二) - 功能 ## 3.12.1 实现关闭送礼物弹框效果 ``` <view style="height: 100rpx;" class="flex justify-between align-center"> <text class="text-main font-md ml-3">礼物</text> <view @click="closeGift" style="height:100rpx; width: 100rpx;" class="flex justify-center align-center"> <text class="iconfont">&#xe607;</text> </view> </view> //关闭送礼物弹出层 closeGift(){ this.$refs.giftPopup.close() }, ``` ## 3.12.3 创建礼物的模拟数据 1. 在项目的根目录下创建demo文件夹 2. 在demo文件夹内创建gift.js文件 3. 在gift.js文件内写模拟的礼物数据 ``` export default [{ "image": "/static/gift/1.png", "id": 1, "name": "鸡蛋", "coin": 1 }, { "image": "/static/gift/2.png", "id": 2, "name": "666", "coin": 2 }, { "image": "/static/gift/3.png", "id": 3, "name": "雪糕", "coin": 3 }, { "image": "/static/gift/4.png", "id": 4, "name": "龙虾", "coin": 4 }, { "image": "/static/gift/5.png", "id": 5, "name": "精灵球", "coin": 5 }, { "image": "/static/gift/6.png", "id": 6, "name": "胶囊", "coin": 6 }, { "image": "/static/gift/7.png", "id": 7, "name": "爱心", "coin": 7 }, { "image": "/static/gift/8.png", "id": 8, "name": "花", "coin": 8 }] ``` ## 3.12.2 将模拟的礼物数据进行动态渲染 1. 引入模拟数据的js文件 2. 在data中定义gifts属性为数组,用来保存礼物的数据 3. 在页面加载的时候给gifts的数据为js文件内模拟的数据 4. 设置v-for遍历的属性为gifts,并对数据进行动态绑定 ``` <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 @click="openGift" 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> <!-- 送礼物弹出层 --> <uni-popup type="bottom" ref="giftPopup"> <view style="width: 750rpx; height: 550rpx;" class="bg-white"> <view style="height: 100rpx;" class="flex justify-between align-center"> <text class="text-main font-md ml-3">礼物</text> <view @click="closeGift" style="height:100rpx; width: 100rpx;" class="flex justify-center align-center"> <text class="iconfont">&#xe607;</text> </view> </view> <swiper class="border-top border-bottom" :indicator-dots="true" style="height: 350rpx;" :duration="500"> <swiper-item> <view class="flex flex-wrap"> <view v-for="(item,index) in gifts" :key="index" style="width: 187.5rpx; height: 175rpx;" class="flex flex-column justify-center align-center"> <image :src="item.image" style="width: 100rpx; height: 100rpx;"></image> <view class="flex mt-1"> <text class="text-warning font mr-1">{{item.name}}</text> <text class="text-secondary font">{{item.coin}}</text> </view> </view> </view> </swiper-item> </swiper> <view style="height: 100rpx;" class="flex align-center justify-end"> <view class="bg-warning flex mr-3 align-center py-2 rounded px-2 justify-center" > <text class="font">充值</text> </view> <view class="bg-main flex mr-3 align-center py-2 rounded px-2 justify-center" > <text class="font text-white">发送</text> </view> </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" import demoGifts from "@/demo/gifts.js" export default { data(){ return { statusBarHeight : 0, content : "", gifts : [] } }, components: { fGift, uniPopup, fDanmu }, onLoad(){ //获取通知栏的高度 let res = uni.getSystemInfoSync(); this.statusBarHeight = res.statusBarHeight; //礼物数据 this.gifts = demoGifts; }, 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:{ //关闭送礼物弹出层 closeGift(){ this.$refs.giftPopup.close() }, //打开送礼物弹出层 openGift(){ this.$refs.giftPopup.open() }, //关闭直播间,返回上一页 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> ``` ## 3.12.4 实现当点击礼物的时候显示对应的边框的效果 ``` <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 @click="openGift" 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> <!-- 送礼物弹出层 --> <uni-popup type="bottom" ref="giftPopup"> <view style="width: 750rpx; height: 550rpx;" class="bg-white"> <view style="height: 100rpx;" class="flex justify-between align-center"> <text class="text-main font-md ml-3">礼物</text> <view @click="closeGift" style="height:100rpx; width: 100rpx;" class="flex justify-center align-center"> <text class="iconfont">&#xe607;</text> </view> </view> <swiper class="border-top border-bottom" :indicator-dots="true" style="height: 350rpx;" :duration="500"> <swiper-item> <view class="flex flex-wrap"> <view @click="giftActiveId = item.id" :class="item.id == giftActiveId ? 'border boder-main' : ''" v-for="(item,index) in gifts" :key="index" style="width: 187.5rpx; height: 175rpx;" class="flex flex-column justify-center align-center"> <image :src="item.image" style="width: 100rpx; height: 100rpx;"></image> <view class="flex mt-1"> <text class="text-warning font mr-1">{{item.name}}</text> <text class="text-secondary font">{{item.coin}}</text> </view> </view> </view> </swiper-item> </swiper> <view style="height: 100rpx;" class="flex align-center justify-end"> <view class="bg-warning flex mr-3 align-center py-2 rounded px-2 justify-center" > <text class="font">充值</text> </view> <view class="bg-main flex mr-3 align-center py-2 rounded px-2 justify-center" > <text class="font text-white">发送</text> </view> </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" import demoGifts from "@/demo/gifts.js" export default { data(){ return { statusBarHeight : 0, content : "", gifts : [], giftActiveId : 0 } }, components: { fGift, uniPopup, fDanmu }, onLoad(){ //获取通知栏的高度 let res = uni.getSystemInfoSync(); this.statusBarHeight = res.statusBarHeight; //礼物数据 this.gifts = demoGifts; }, 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:{ //关闭送礼物弹出层 closeGift(){ this.$refs.giftPopup.close() }, //打开送礼物弹出层 openGift(){ this.$refs.giftPopup.open() }, //关闭直播间,返回上一页 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> ``` ## 3.12.5 实现发送礼物功能 ``` <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 @click="openGift" 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> <!-- 送礼物弹出层 --> <uni-popup type="bottom" ref="giftPopup"> <view style="width: 750rpx; height: 550rpx;" class="bg-white"> <view style="height: 100rpx;" class="flex justify-between align-center"> <text class="text-main font-md ml-3">礼物</text> <view @click="closeGift" style="height:100rpx; width: 100rpx;" class="flex justify-center align-center"> <text class="iconfont">&#xe607;</text> </view> </view> <swiper class="border-top border-bottom" :indicator-dots="true" style="height: 350rpx;" :duration="500"> <swiper-item> <view class="flex flex-wrap"> <view @click="giftActiveId = item.id" :class="item.id == giftActiveId ? 'border boder-main' : ''" v-for="(item,index) in gifts" :key="index" style="width: 187.5rpx; height: 175rpx;" class="flex flex-column justify-center align-center"> <image :src="item.image" style="width: 100rpx; height: 100rpx;"></image> <view class="flex mt-1"> <text class="text-warning font mr-1">{{item.name}}</text> <text class="text-secondary font">{{item.coin}}</text> </view> </view> </view> </swiper-item> </swiper> <view style="height: 100rpx;" class="flex align-center justify-end"> <view class="bg-warning flex mr-3 align-center py-2 rounded px-2 justify-center" > <text class="font">充值</text> </view> <view class="bg-main flex mr-3 align-center py-2 rounded px-2 justify-center" > <text class="font text-white" @click="sendGift">发送</text> </view> </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" import demoGifts from "@/demo/gifts.js" export default { data(){ return { statusBarHeight : 0, content : "", gifts : [], giftActiveId : 0 } }, components: { fGift, uniPopup, fDanmu }, onLoad(){ //获取通知栏的高度 let res = uni.getSystemInfoSync(); this.statusBarHeight = res.statusBarHeight; //礼物数据 this.gifts = demoGifts; }, 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:{ //发送礼物 sendGift(){ //如果要发送的礼物不存在,则不继续往下执行,并给一个提示 if(this.giftActiveId == 0){ return uni.showToast({ title: "请选择要发送的礼物", icon : "none" }) } //查找出对应的礼物 let index = this.gifts.findIndex(item=> item.id == this.giftActiveId) if(index == -1){ return; } let g = this.gifts[index]; //进行发送 this.$refs.gift.send({ username : "发送人", avatar : "", gift_name : g.name, gift_image : g.image, num : 1 }) //关闭送礼物弹出层 this.closeGift(); }, //关闭送礼物弹出层 closeGift(){ this.$refs.giftPopup.close() }, //打开送礼物弹出层 openGift(){ this.$refs.giftPopup.open() }, //关闭直播间,返回上一页 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> ```