uni-app 155朋友圈评论功能(二)

142 篇文章 4 订阅
139 篇文章 1 订阅

下图是我测试的截图

在这里插入图片描述

/pages/find/moments/moments.vue

<template>
	<view>
		<free-transparent-bar :scrollTop="scrollTop" @clickRight="clickRight"></free-transparent-bar>
		<view class="position-relative" style="height: 620rpx;">
			<image src="https://douyinzcmcss.oss-cn-shenzhen.aliyuncs.com/shengchengqi/datapic/1.jpg" mode="aspectFill" style="height: 590rpx;" class="bg-secondary w-100"></image>
			<image :src="user.avatar || '/static/images/demo/demo6.jpg'" style="width: 120rpx;height:120rpx;right: 30rpx;" mode="" class="bg-secondary rounded position-absolute bottom-0"></image>
			<text class="text-white font-md position-absolute" style="bottom: 45rpx;right: 160rpx">{{user.nickname || user.username}}</text>
		</view>
		
		<!-- 朋友圈列表样式 -->
		<free-moment-list v-for="(item,index) in list" :key='index' :item="item" :index="index" @action="doAction" @reply="replyEvent"></free-moment-list>
		
		<!-- 评论框 -->
		<free-popup ref="action" bottom transformOrigin="center bottom">
			<view style="height: 105rpx;" class="bg-light border-top flex align-center">
				<textarea fixed class="bg-white rounded p-2 font-md" style="height: 45rpx;width: 420rpx;" value="" placeholder=""  v-model="content" :focus="true"/>
				<free-icon-button @click="changeFaceModeal"><text class="iconfont font-lg">&#xe605;</text></free-icon-button>
				<free-main-button name="发送" :disabled="content.length===0" @click="send"></free-main-button>
			</view>
			
			<!-- 表情包 -->
			<scroll-view v-if="faceModal" scroll-y="true" style="height: 350rpx;" class="bg-light">
				<view class="flex flex-wrap">
					<view class="flex align-center justify-center" hover-class="bg-white" style="width:107rpx;height:107rpx;" v-for="(item,index) in faceList" :key="index" @click="addFace(item)">
						<text>{{item}}</text>
					</view>
				</view>
			</scroll-view>
		</free-popup>
		
		<!-- 上拉加载 -->
		<view class="flex align-center justify-center py-5 bg-light" v-if="list.length >= 10">
			<text class="text-muted font">{{loadmore}}</text>
		</view>
	</view>
</template>

<script>
	import freeTransparentBar from '@/components/free-ui/free-transparent-bar.vue';
	import freeMomentList from '@/components/free-ui/free-moment-list.vue';
	import freePopup from '@/components/free-ui/free-popup.vue';
	import freeIconButton from '@/components/free-ui/free-icon-button.vue';
	import freeMainButton from '@/components/free-ui/free-main-button.vue';
	import $H from '@/common/free-lib/request.js';
	import { mapState } from 'vuex';
	export default {
		components:{
			freeTransparentBar,
			freeMomentList,
			freePopup,
			freeIconButton,
			freeMainButton
		},
		data() {
			return {
				content:'',
				scrollTop:0,
				faceModal:false,
				faceList:["😀","😁","😂","😃","😄","😅","😆","😉","😊","😋","😎","😍","😘","😗","😙","😚","😇","😐","😑","😶","😏","😣","😥","😮","😯","😪","😫","😴","😌","😛","😜","😝","😒","😓","😔","😕","😲","😷","😖","😞","😟","😤","😢","😭","😦","😧","😨","😬","😰","😱","😳","😵","😡","😠"],
				commentIndex:-1,
				page:1,
				loadmore:'上拉加载更多',
				key:'moment_timeline',
				list:[],
				reply_user:false
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			if(this.loadmore !== '上拉加载更多'){
				return;
			}
			this.page += 1;
			this.loadmore = '加载中...';
			this.getData().catch(err=>{
				this.page -= 1;
				uni.showToast({
					title:'加载失败',
					icon:'none'
				});
				this.loadmore = '上拉加载更多';
			})
		},
		onLoad(e) {
			if(e.key){
				this.key  = e.key;
			}
			this.page = 1;
			this.getData();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.getData().then(res=>{
				uni.showToast({
					title:'刷新成功',
					icon:'none'
				});
				uni.stopPullDownRefresh();
			}).catch(err=>{
				uni.showToast({
					title:'刷新失败',
					icon:'none'
				});
				uni.stopPullDownRefresh();
			});
		},
		computed:{
			...mapState({
				user:state=>state.user.user
			})
		},
		methods: {
			// 点击操作菜单
			doAction(e){
				uni.showActionSheet({
					itemList: ['点赞','评论'],
					success: res => {
						if(res.tapIndex === 0){
							// 点赞
							this.doSupport(e)
						}else{
							this.content='';
							this.faceModal=false;
							this.commentIndex = e.index;
							this.reply_user = false;
							this.$refs.action.show();
							// this.doComment(e)
						}
					}
				});
			},
			// 获取数据
			getData(){
				return new Promise((result,reject)=>{
					let page = this.page;
					$H.get('/'+this.key+'/'+page).then(res=>{
						this.list = page === 1 ? res : [...this.list,...res];
						this.loadmore = this.list.length === (page * 10) ? '上拉加载更多' : '没有更多了'
						result(res);
					}).catch(err=>{
						reject(err);
					})
				})
			},
			// 点赞
			doSupport(e){
				$H.post('/moment/like',{id:e.item.moment_id}).then(res=>{
			   let i = e.item.likes.findIndex(item=>item.id === this.user.id);
			   if(i !== -1){
				   // 取消点赞
				   e.item.likes.splice(i,1);
			   }else{
				   // 点赞
				   e.item.likes.push({
					   id:this.user.id,
					   name:this.user.nickname || this.user.username
				   })
			   }
			   uni.showToast({
			   	title:i !== -1 ? '取消点赞成功' : '点赞成功',
				icon:'none'
			   });
			   
			  })
			},
			// 添加表情
			addFace(item){
				this.content += item;
			},
			// 开启关闭表情包面板
			changeFaceModeal(){
				uni.hideKeyboard();
				setTimeout(()=>{
					this.faceModal = !this.faceModal;
				},100);
			},
			// 发送
			send(){
				console.log(this.reply_user);
				let item = this.list[this.commentIndex];
				$H.post('/moment/comment',{
					id:item.moment_id,
					content:this.content,
					reply_id:this.reply_user ? this.reply_user.id : 0
				}).then(res=>{
					item.comments.push({
						content:this.content,
						user:{
							id:this.user.id,
							name:this.user.nickname || this.user.username
						},
						replay:this.reply_user ? this.reply : null
					})
					uni.showToast({
						title:'评论成功',
						icon:'none'
					})
				})
				this.$refs.action.hide();
			},
			replyEvent(e){
				this.content = '';
				this.faceModal = false,
				this.commentIndex = e.index,
				this.reply_user = e.reply;
				this.$refs.action.show();
			},
			// 选择发表朋友圈类型
			clickRight(){
				let list = [{
					name:'图文',
					key:'image'
				},
				{
					name:'视频',
					key:'video'
				},
				{
					name:'文字',
					key:'text'
				}];
				uni.showActionSheet({
					itemList:list.map(v=>v.name),
					success:res=>{
						uni.navigateTo({
							url: '../add-moment/add-moment?type='+list[res.tapIndex].key,
						});
					}
				})
			}
		},
	}
</script>

<style>

</style>

/components/free-ui/free-moment-list.vue

<template>
	<view class="p-3 flex align-start border-bottom border-light-secondary">
		<free-avater :src="item.avatar" size="80"></free-avater>
		<view class="pl-2 flex-1 flex flex-column">
			<!-- 昵称 -->
			<text class="font-md text-hover-primary mb-1">{{item.user_name}}</text>
			<!-- 内容 -->
			<text v-if="item.content" class="font-md text-dark mb-1">{{item.content}}</text>
			<!-- 图片 -->
			<view v-if="item.image.length" class="py-2 flex flex-wrap">
				<block v-for="(image,imageIndex) in item.image"
				:key="imageIndex">
					<!-- 单图 -->
					<free-image v-if="item.image.length === 1" :src="image" imageClass="rounded bg-secondary" @click="preview(image)"></free-image>
					<!-- 多图 -->
					<image v-else :src="image" mode="aspectFill" style="height: 180rpx;width: 180rpx;" class="bg-secondary mr-2 mb-2 rounded" @click="preview(image)"></image>
				</block>
			</view>
			<!-- 视频 -->
			<view v-if="item.video" class="py-2">
				<video :src="item.video.src" :poster="item.video.poster" controls style="height: 300rpx;width: 500rpx;" loop></video>
			</view>
			<!-- 时间|操作 -->
			<view class="flex align-center justify-between">
				<text class="font-sm text-light-muted">{{item.created_at|formatTime}}</text>
				<view class="rounded p-1 bg-light" @click="$emit('action',{item,index})">
					<text class="text-hover-primary iconfont font">&#xe62a;</text>
				</view>
			</view>
			<!-- 点赞列表|评论列表 -->
			<view class="bg-light mt-2" v-if="item.likes.length || item.comments.length">
				<!-- 点赞 -->
				<view v-if="item.likes.length" class="border-bottom flex align-start p-2">
					<text class="flex-shrink iconfont font text-hover-primary">&#xe637;</text>
					<view class="flex flex-1 flex-wrap ml-1">
						<text v-for="(s,si) in item.likes" :key="si" class="font text-hover-primary ml-1">{{s.name}}</text>
					</view>
				</view>
				<!-- 评论 -->
				<view v-if="item.comments.length" class="flex align-start p-2">
					<text class="flex-shrink iconfont font-md text-hover-primary">&#xe64e;</text>
					<view class="flex flex-column flex-1 ml-2">
						<view class="flex" 
						v-for="(c,ci) in item.comments"
						:key="ci" :index='index'>
							<text v-if="!c.reply" class="text-hover-primary font">{{c.user.name}}</text>
							<view v-else class="flex align-center">
								<text class="text-hover-primary font">{{c.user.name}} </text>
								<text class="text-muted font-sm">回复</text>
								<text class="text-hover-primary font">{{c.reply.name}}</text>
							</view>
							<text class="font text-dark flex-1"
							@click="$emit('reply',{
								item,
								index,
								reply:c.user
							})">{{c.content}}</text>
						</view>
					</view>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import freeAvater from '@/components/free-ui/free-avater.vue';
	import freeImage from '@/components/free-ui/free-image.vue';
	import $T from '@/common/free-lib/time.js';
	export default {
		components: {
			freeAvater,
			freeImage
		},
		props: {
			item: Object,
			index:Number
		},
		filters: {
			formatTime(value) {
				return $T.gettime(value);
			}
		},
		methods: {
			// 查看大图
			preview(src) {
				uni.previewImage({
					current:src,
					urls:this.item.image 
				})
			}
		},
	}
</script>

<style>
</style>

感谢大家观看,我们下次见

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2019ab

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值