小云z 2024-04-27 14:02 采纳率: 100%
浏览 10
已结题

请看看这段代码什么意思。

请帮忙看看这段代码什么意思。


```html
<template>
    <view class="blogitem">
        <view class="head">
            <view class="userinfo">
                <view class="avatar">
                    <image 
          :src="giveAvatar(props.dataList)"  mode="aspectFill"></image>
                </view>
        
                <view class="name">{{giveName(props.dataList)}}</view>
                <view class="time">
          <!-- 日期格式化 -->
                    <uni-dateformat :date="props.dataList.publish_date" format="MM月dd hh:mm" :threshold="[60000,3600000*24*30*360]"></uni-dateformat>
                </view>
            </view>
            
            <view class="more" @click="clickMore">
                <text class="iconfont icon-ellipsis"></text>
            </view>
        </view>
        
        <view class="body">
            <view class="title" @click="goDetail">{{props.dataList.title}}</view>
      <!-- 有相应的描述信息才显示该模块 -->
            <view class="text" @click="goDetail">
                <view class="t">{{props.dataList.description}}</view>
            </view>
            <view class="piclist" v-if="props.dataList.picurls.length">
                <view class="pic" :class="props.dataList.picurls.length==1 ? 'only': ''" v-for="(item,index) in props.dataList.picurls" :key="item">
                    <image @click="clickPic(index)" :src="item" mode="aspectFill"></image>
                </view>                    
            </view>
        </view>
        
        
        <view class="info">
            <view class="box"><text class="iconfont icon-a-27-liulan"></text> <text>{{props.dataList.view_count}}</text></view>
            <view class="box" @click="goDetail"><text class="iconfont icon-a-5-xinxi"></text> <text>{{props.dataList.comment_count && props.dataList.comment_count>0 ? props.dataList.comment_count: "评论"}}</text></view>
            <view class="box" :class="props.dataList.isLike ? 'active':''" @click="clickLike"><text class="iconfont icon-a-106-xihuan"></text> <text>{{props.dataList.like_count ? props.dataList.like_count: "点赞"}}</text></view>
        </view>
        
        
        <up-action-sheet :actions="list" :show="sheetShow" cancelText="取消" :closeOnClickOverlay="true" :closeOnClickAction="true" @select="sheetSelect" @close="onClose"> </up-action-sheet>
        
    </view>
</template>

<script setup>
  import {store} from '@/uni_modules/uni-id-pages/common/store.js'
  import {giveName,giveAvatar,likeFun} from '../../utils/tools.js'
  import { ref } from 'vue';
  const db=uniCloud.database()
  const picArr = ref([1,2])
  const props= defineProps({
    dataList:Array
  })
  const emit = defineEmits(['delEvent'])
  
  let list = ref([
    {
      name:"修改",
      type:"edit",
      disabled: true
    },
    {
      name:"删除",
      type:"del",
      color:"#F56C6C",
      disabled: true
    }])
    // 表示弹窗的状态
  let sheetShow = ref(false)
  let likeTime = ref(Date.now())
  // 定义方法
  // 点击图片进行预览操作
  const clickPic = (index)=>{
    uni.previewImage({
      urls:props.dataList.picurls,
      current:index
    })
  }
  
  // 点击跳转到详情
  const goDetail = ()=>{
    uni.navigateTo({
      url:"/pages/detail/detail?id="+props.dataList._id
    })
  }
  // 点击选择
  const sheetSelect = (e)=>{
    sheetShow.value = false
    let type = e.type
    if(type=="del") {
      delFun()
    } else if(type=="edit") {
      modifyFunc()
    }
  }
  // 删除方法:
  const delFun = ()=>{
    uni.showLoading({
      title:"加载中..."
    })
    db.collection("blog_articles").doc(props.dataList._id).update({
      delState:true
    }).then(res=>{
      uni.hideLoading()
      uni.showToast({
        title:"删除成功",
        icon:"none"
      })
      // 删除成功后告知父组件(首页),要求进行数据的重新加载
      emit('delEvent',true)
      console.log(res)
    }).catch(err=>{
     uni.hideLoading()
    })
  }
  
  // 修改方法
  const modifyFunc = ()=>{
    console.log(props.dataList)
    const obj = {
      id: props.dataList._id,
      title: props.dataList.title,
      content: props.dataList.content
    }
    uni.navigateTo({
      url:"/pages/edit/edit?detail="+JSON.stringify(obj)
    })
  }
  
  // 取消弹窗
  const onClose = ()=>{
    sheetShow.value = false
  }
  
  // 点击更多操作
  const clickMore = ()=>{
    // 当前账户的id信息
    let uid = uniCloud.getCurrentUserInfo().uid
    console.log(uid)
    // 每一条blog的发布者可以修改和删除blog,超级管理员也可以修改和删除blog
    if(uid == props.dataList.user_id[0]._id || uniCloud.getCurrentUserInfo().role[0] == 'admin') {
      list.value.forEach(item=>{
        item.disabled = false
      })
    }
    sheetShow.value = true
  }
  
  const clickLike = ()=>{
    // 1.登陆后才可以进行点赞操作
    if(!store.hasLogin) {
      uni.showModal({
        title:"登录后才能进行操作,是否确认登录",
        success:res=>{
          // 点击确认跳转到登录页面
          if(res.confirm) {
            uni.navigateTo({
              // 登录页面地址
              url:'/'+ pageJson.uniIdRouter.loginPage
            })
          }
        }
      })
      return
    }
    
    // 2.防止恶意点击代码逻辑
    let time = Date.now()
    if(time - likeTime.value < 2000) {
      uni.showToast({
        title:"操作太频繁",
        "icon":"none"
      }) 
      return
    }
    // 点赞的无感操作
    props.dataList.isLike ? props.dataList.like_count--:props.dataList.like_count++
    props.dataList.isLike = !props.dataList.isLike
    likeTime.value = time
    
    // 3.调用点赞方法
    likeFun(props.dataList._id)
  }
</script>


```

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-27 14:02
    关注

    引自免费微信小程序:皆我百晓生

    这段代码是基于Vue.js框架的一个组件,用于展示博客文章列表中的一个项目(item)。组件名为blogitem,主要由头部(包含用户信息、发布时间)、主体(标题、简介、图片列表)和底部信息(浏览量、评论数、点赞数)组成,并且提供了一个上拉菜单(up-action-sheet)用于实现编辑和删除操作。

    HTML 部分:

    1. <view class="blogitem"> 是整个博客项目的外层容器。
    2. <view class="head"> 包含用户头像、用户名、发布时间。
    3. <view class="body"> 包含文章标题、简介和图片列表。
      • 如果存在描述信息,则会显示<view class="text">模块。
      • 使用v-if判断图片是否存在,若存在则显示图片列表。
    4. <view class="info"> 显示浏览量、评论数和点赞数,同时提供了点赞功能。
    5. <up-action-sheet> 是一个上拉菜单,展示了编辑和删除选项。

    JavaScript 部分:

    1. import 导入外部文件,如全局变量、工具函数等。
    2. const db = uniCloud.database() 创建云数据库实例。
    3. defineProps({ dataList: Array }) 定义接收父组件传递的数据。
    4. const emit = defineEmits(['delEvent']) 定义触发事件的方法,用于向父组件发送删除操作完成的信息。
    5. let list = ref([...]) 初始化上拉菜单的选项。
    6. let sheetShow = ref(false) 控制上拉菜单的显示状态。
    7. let likeTime = ref(Date.now()) 记录最后一次点赞的时间戳,用于防抖操作。
    8. 定义了多个方法:
      • clickPic(index):点击图片进行预览。
      • goDetail():点击标题跳转到详情页。
      • sheetSelect(e):处理上拉菜单的选择事件。
      • delFun():执行删除操作。
      • modifyFunc():进入编辑页面。
      • onClose():关闭上拉菜单。
      • clickMore():点击更多按钮时,根据用户权限启用编辑和删除选项并打开上拉菜单。
      • clickLike():处理点赞功能,包括登录验证、防抖操作以及更新点赞状态。

    这个组件的主要作用是渲染单个博客条目,并提供基本的操作功能,如查看详情、点赞、编辑和删除。通过props接收父组件传递的数据,并通过emit将一些特定操作的结果反馈给父组件。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 已采纳回答 4月27日
  • 创建了问题 4月27日