小程序云开发教程六:贴子的审核

别人发布了,就得审核呀。所以这篇我们来讲审核功能的实现。
条件: 不能审核自己的,并且是审核次数在0-5次的。
那么审核的逻辑是什么呢?
我在这篇的逻辑是: 总共可以审核5次,就是通过存储通过和不通过的次数,然后对比它们,如果通过:不通过的比 >1.4,或者1.5左右,那么就判定这个是通过的。
我们来看布局, 和首页展示的差不多,一个用户信息,一个图片,一个用户发布的文字,还有通过,不通过按钮。

代码如下:

<!--pages/validContent/validContent.wxml-->
<view wx:if='{{!data}}' class='flexDownC mt40'>
  <image src='../../images/404.png' class='p404'></image>
  <text>暂无贴子可以审核</text>
</view>
<view wx:else>
  <view class='userInfo flexRowL' > 
    <view class='user flexC'> 
      <image src='{{data.userImg || defaultImg}}' class='userImg'></image> {{data.username || '糗皮虎'}}</view> 
    </view>
    <view class='txt'>{{data.content}}</view>

    <view class='img' wx:for="{{data.image}}" wx:for-item='imgItem' > 
      <image lazy-load="{{lazy_load}}" mode='widthFix' src='{{imgItem}}' class='{{data.image.length ==1 ?"dzImg1": data.image.length == 2 ?"dzimg2": data.image.length == 3 ? "dzImg3" : data.image.length == 4 ? "dzImg4": "" }} dzImg' ></image>     
    </view>

    <view class='btns flexSpaceBet'>
    <!--不通过-->
      <image  class='passi ml20' src='../../images/noPass.png'  bindtap='unPassItem'></image>
    <!--通过-->
      <image  class='passi mr20' src='../../images/pass.png' bindtap='passItem'></image>
    </view>
</view>

wxss:可以直接引入首页的样式

/* pages/validContent/validContent.wxss */
@import '../index/index.wxss';

page{
  font-size: 24rpx;
  color: #999;
}

.btns{
  width: 100%;
  height: 100rpx;
  position: absolute;
  bottom: 60rpx;
  left: 0;
}

.passi{
  width: 100rpx;
  height: 100rpx;
}

js部分:使用云函数passItem, 目的是将id,通过的次数增1, 审核次数增1

// pages/validContent/validContent.js
const db = wx.cloud.database()
const _ = db.command;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultImg: '../../images/tx.png',
    data: '',
    id: '',
    limitCount: 1,
    skipCount: 1 //跳过条数
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    this.search()

  },
  //搜索
  search: function(){
    var skipCount = this.data.skipCount;
    db.collection('funnys').where({
      validTime: _.lt(5).and(_.gte(0)), //审核次数大于等于0 小于5
      _openid: _.neq(wx.getStorageSync('openId')) //不是他自己上传的贴子
    }).skip(skipCount).limit(this.data.limitCount).get({
      success: res => {
        console.log(res)
        if (res.data.length > 0)
          this.setData({
            data: res.data[0],
            id: res.data[0].id,
            skipCount: skipCount + 1
          })
      },
      fail: err => {
        wx.showToast({
          title: '出错',
          icon: 'none'
        })
      }
      })
  },
//  通过
  passItem: function(e){
    if (this.data.id){
      wx.cloud.callFunction({
        name: 'passItem',
        data: {
          id: this.data.id,
          validTime: this.data.data.validTime+1,
          passTime: this.data.data.passTime+1,
          unPassTime: this.data.data.unPassTime
        },
        success : res =>{
          wx.showToast({
            title: '通过帖子',
          })
          this.search()
        },
        fail: err =>{
          wx.showToast({
            title: '出现错误',
            icon: 'none'
          })
        }
      })
    }
  },
  unPassItem: function (e) {

 	 if (this.data.id){
      wx.cloud.callFunction({
        name: 'passItem',
        data: {
          id: this.data.id,
          validTime: this.data.data.validTime+1,
          passTime: this.data.data.passTime,
          unPassTime: this.data.data.unPassTime+1
        },
        success : res =>{
          wx.showToast({
            title: '打回帖子',
          })
          this.search()
        },
        fail: err =>{
          wx.showToast({
            title: '出现错误',
            icon: 'none'
          })
        }
      })
    }	
  }

})

云函数,新建passItem和安装环境完毕后,写入代码:

// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()



// 云函数入口函数
exports.main = async (event, context) => {
//es6 语法
  var { id,  passTime, validTime  } = event;
  console.log('云函数passItem成功',  id)

  // console.warn(data)

  try {
    return await db.collection('funnys').where({
      id: Number(id)
    }).update({
      data: {
     //   validStatus: 0,
        validTime: validTime,
        passTime: passTime,
        unPassTime: unPassTime
      },
      success: res => {
        console.log('云函数passItem成功', id)

      },
      fail: e => {
        console.error(e)
      }
    })
  } catch (e) {
    console.error(e)
  }

}



现在, 审核功能就完成了。

小程序云开发教程一: 新建一个云开发项目以及基本布局
小程序云开发教程二:数据的获取(爬虫)
小程序云开发教程三: 数据的布局以及展示
小程序云开发教程四:云函数的使用与点赞功能的实现
小程序云开发教程五: 图片上传及发表文字的实现
那么,到此为止,点赞功能就基本完成了, 详细代码请看:
https://github.com/LWJcoder/qiupihu

大家看在我码字那么辛苦的份上,顺手给github点一个小星星呗 ?
以上代码我会放在我的github上: LWJCoder

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值