123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Exception;
- class Post extends Base
- {
- public function addSave($userId,$orgId){
- $data=[
- 'user_id'=>$userId,
- 'content'=>trim(input('content')),
- 'imgs'=>input('imgs'),
- 'enable'=>1,
- 'create_time'=>getTime(),
- 'update_time'=>getTime(),
- 'org_id'=>$orgId,
- ];
- if(!$data['content'] && !$data['imgs']){
- $this->error = '请添加内容或图片';
- return false;
- }
- $ret=$this->insert($data);
- return $ret;
- }
- public function lists($page,$size,$orgId,$userId){
- $offset = ($page - 1) * $size;
- $post=$this
- ->field('id,user_id,content,create_time,imgs')
- ->where([
- 'enable'=>1,
- 'del'=>0,
- 'org_id'=>$orgId,
- ])
- ->order('id desc')
- ->limit($offset,$size)
- ->select();
- $post = $post?$post->toArray():[];
- foreach ($post as $k=>$val){
- $like = Db::name('post_like')
- ->where('user_id',$userId)
- ->where('post_id',$val['id'])
- ->find();
- $post[$k]['status'] = $like?1:0;
- $post[$k]['imgs'] = empty($val['imgs']) ? [] : explode(',',$val['imgs']);
- $user_name=Db::name('user')
- ->field('real_name,head_image')
- ->where('id',$val['user_id'])
- ->find();
- $post[$k]['user_name']=$user_name['real_name'];
- $post[$k]['head_image']=$user_name['head_image'];
- //获取点赞人的名称
- $like=Db::name('post_like')
- ->alias('l')
- ->field('u.real_name,l.id as like_id')
- ->join('user u','u.id=l.user_id')
- ->where('l.post_id',$val['id'])
- ->select();
- $post[$k]['like']=$like;
- //获取回复信息
- $reply=Db::name('post_reply')
- ->field('id as reply_id,content,user_id,post_reply_id')
- ->where([
- 'post_id'=>$val['id'],
- 'enable'=>1,
- 'del'=>0,
- ])
- ->order('create_time asc')
- ->select();
- foreach ($reply as $key=>$value){
- $userName=Db::name('user')
- ->field('real_name')
- ->where('id',$value['user_id'])
- ->find();
- $replyName=Db::name('post_reply')
- ->alias('pr')
- ->join('user u','u.id = pr.user_id')
- ->field('u.real_name')
- ->where('pr.id',$value['post_reply_id'])
- ->find();
- $reply[$key]['userName']=$userName?$userName['real_name']:'';
- $reply[$key]['replyName']=$replyName?$replyName['real_name']:'';
- }
- $post[$k]['reply']=$reply;
- }
- return $post;
- }
- public function del($id){
- $data=['del'=>1];
- $del=$this->where('id',$id)->update($data);
- return $del;
- }
- }
|