微信小程序文章评论

  后台

class ArticleController
{
    //

    /**
     *注释:  文章列表
     *用户名:过客
     *日期:2021/9/17
     *时间:20:51
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function articleList()
    {

        $data=Article::select();
        if ($data){
         return returnAjaxData(1,$data,'查询成功');
        }else{
            return returnAjaxData(0,'','查询失败');
        }
    }

    /**
     *注释:  文章详情
     *用户名:过客
     *日期:2021/9/17
     *时间:20:51
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function articleGet(Request $request)
    {
        $data=Article::where('id',$request['id'])->find();
        $coms=Comment::with(['user'])->where('article_id','=',$data['id'])->select()->toArray();
           $com=order($coms);
        
        return json(['code'=>200,'data'=>$data,'com'=>$com,'msg'=>'ok']);
    }


    /**
     *注释: 文章评论
     *用户名:过客
     *日期:2021/9/18
     *时间:11:06
     * @param Request $request
     */

    public function articleComment(Request $request)
    {
        $param=$request->param();
        $param['uid']=$request->uid;
        $dataRes=Comment::create($param);
        if ($dataRes){
            return returnAjaxData(1,$dataRes,'评论成功');
        }else{
            return returnAjaxData(0,'','评论失败');
        }
    }

    /**
     *注释:文章评论展示
     *用户名:过客
     *日期:2021/9/19
     *时间:15:08
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function articleFind(Request $request)
    {
        $id=$request['id'];
        $data=Comment::where('id',$id)->find();
        if ($data){
            return returnAjaxData(1,$data,'查询成功');
        }else{
            return returnAjaxData(0,'','查询失败');
        }

    }

    /**
     *注释: 文章回复评论
     *用户名:过客
     *日期:2021/9/18
     *时间:13:52
     * @param Request $request
     */
    public function articleGo(Request $request)
    {

        $data=$request->param();
        $data['uid']=$request->uid;
        $dataRes=Comment::create($data);
        if ($dataRes){
            return returnAjaxData(1,$dataRes,'回复成功');
        }else{
            return returnAjaxData(0,'','回复失败');
        }

    }

public function articleLike(Request $request)
{
    //文章的id
  $id=$request->param('article_id');
    //用户id
   $uid=$request->uid;
    //查询一条数据 查询有没有 文章id 以及用户id
    $arr = Like::where('article_id', $id)->find();
    //如果没有就添加入库 点赞成功 并且点赞数量加1
    if (empty($arr)) {
        $arr['article_id'] = $id;
        $arr['uid'] = $uid;
        $res = Like::create($arr);
        //添加结果
        if ($res) {
            $arr = Like::where('article_id', $id)->find();
            $data=Like::where('article_id', $id)->update(['number' => $arr['number'] + 1]);
            return json(['code' => 200, 'msg' => '点赞成功', 'data' => $data]);
        } else {
            return json(['code' => 400, 'msg' => '点赞失败', 'data' => null]);
        }
    } else {
        //如果点赞过了就取消点赞删除这个点赞记录
        $del = Like::where('article_id', $id)->delete();
        if ($del) {
            return json(['code' => 300, 'msg' => '取消成功', 'data' => $id]);
        } else {
            return json(['code' => 500, 'msg' => '取消失败', 'data' => null]);
        }

    }
}


public function like(){
        $dataRes=Like::select();
        if ($dataRes){
            return returnAjaxData(1,$dataRes,'查询成功');
        }else{
            return returnAjaxData(0,'','查询失败');
        }

}

}

前台


<block wx:for="{{articlelist}}" wx:key="item">

  <l-card type="primary"  image="{{item.image}}">
       <view >标题:<text class="a">{{item.title}}</text></view>
        <!-- 此处为content -->
        <view class="content">
         内容:{{item.coment}}
        </view>
        <view>
       浏览数量:{{item.page}}
        </view>
      <navigator url="/pages/detail/detail?id={{item.id}}">
    
        <l-button type="default">详情</l-button>
      </navigator>
    
    </l-card>
  
</block>



 data: {
    articlelist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    wx.request({
      url: 'http://www.tp1.com/api/articleList',
      header:{  
        	 'token':wx.getStorageSync('token')
      		 },
      success:res=>{
        this.setData({
          articlelist:res.data.data
        })
      }
    })

  },

<l-card type="avatar"l-class="card"  image="{{detail.image}}"
    title="{{detail.title}}" describe="18个小时前">
        <!-- 此处为content -->
        <view class="avter-content">
        {{detail.coment}}
        </view>
        <view class="avter-share-container">
        </view>
  </l-card>


    <view> <l-button bindtap="fabulous">点赞<view>
    {{like}}
    </view></l-button></view>





<block  wx:for="{{com}}">

<view>
   <view wx:if="{{item.pid==0}}"> <navigator url="/pages/reply/reply?id={{item.id}}"> 用户评论:{{item.text}}</navigator></view>
   <view wx:else>
   
    客服回复评论:{{item.text}}
   
  </view>
</view>
</block>
  

  <l-form name="student" l-form-btn-class="l-form-btn-class" bind:linsubmit="submit">
    <l-form-item label="评论内容:" name="text">
        <l-textarea id="text" bind:lininput="text"  />
    </l-form-item>
 
    <view slot="submit">
        <l-button>提交评论</l-button>
    </view>
</l-form>


Page({

  /**
   * 页面的初始数据
   */
  data: {
   detail:[],
   com:[],
   like:'',

   text:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let id=options.id
    wx.request({
      url: 'http://www.tp1.com/api/articleGet',
      data:{id:id},
      header:{ 
        	 'token':wx.getStorageSync('token')
    		 },
      success:res=>{
        this.setData({
         detail:res.data.data,
         com:res.data.com
        })
      }
    })
  },

  text(e){
    this.setData({
      text:e.detail.value
    })
  },

  submit(e){
    console.log(e)
    let id=this.data.detail.id
    wx.request({
      url: 'http://www.tp1.com/api/articleComment',
      data:{
              text:this.data.text,
              article_id:this.data.detail.id,
      },
      method:"post",
      header:{'token':wx.getStorageSync('token')},
      success:res=>{
          //     console.log(res);
              if(res.data.code==200){
                      wx.navigateTo({
                        url:"/pages/detail/detail?id="+id
                                            })
              }
      }
    })

  },

  fabulous(e){
 
   wx.request({
     url: 'http://www.tp1.com/api/articleLike',
     header:{
      'token':wx.getStorageSync('token')
     },
     data:{
      article_id:this.data.detail.id,
     },

     success:res=>{
       if (res.data.code==200) {
        wx.request({
          url: 'http://www.tp1.com/api/like',
          success:ret=>{
            this.setData({
              like:ret.data.data[0].number
            })
          }
        })
       }else{
         if (res.data.code==300) {
          wx.request({
            url: 'http://www.tp1.com/api/like',
            success:ret=>{
              this.setData({
                like:ret.data.data
              })
            }
          })
         }
       }
     }
   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.lin.initValidateForm(this)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
<block>
  <view>用户评论:{{replys.text}}</view>
</block>

<l-form name="student" l-form-btn-class="l-form-btn-class" bind:linsubmit="submit">
    <l-form-item label="回复内容:" name="text">
        <l-textarea id="text" />
    </l-form-item>
 
    <view slot="submit">
        <l-button>回复评论</l-button>
    </view>
</l-form>



// pages/reply/reply.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    replys:[],
  },

  reply(e){
    console.log(e);
    this.setData({
      reply:e.detail.value
    })
  },


  submit(e){
    console.log(e.detail.values.text)
    let  text = e.detail.values.text;
    wx.request({
      url: 'http://www.tp1.com/api/articleGo',
      data:{text:text,pid:this.data.replys.id,article_id:this.data.replys.article_id},
      method:"post",
      header:{'token':wx.getStorageSync('token')},
      success:res=>{
        console.log(res)
        if(res.data.code=200){
          wx.navigateTo({
            url: '/pages/detail/detail',
          })
        }
    }
    })
   

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.lin.initValidateForm(this)
    let id=options.id
    
    wx.request({
      url: 'http://www.tp1.com/api/articleFind',
      data:{id:id},
      header:{
        'token':wx.getStorageSync('token')
      },
      success:res=>{
        console.log(res.data.data)
        this.setData({
          replys:res.data.data
        })
      }

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值