Post.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class Post extends Base
  6. {
  7. public function addSave($userId,$orgId){
  8. $data=[
  9. 'user_id'=>$userId,
  10. 'content'=>trim(input('content')),
  11. 'imgs'=>input('imgs'),
  12. 'enable'=>1,
  13. 'create_time'=>getTime(),
  14. 'update_time'=>getTime(),
  15. 'org_id'=>$orgId,
  16. ];
  17. if(!$data['content'] && !$data['imgs']){
  18. $this->error = '请添加内容或图片';
  19. return false;
  20. }
  21. $ret=$this->insert($data);
  22. return $ret;
  23. }
  24. public function lists($page,$size,$orgId,$userId){
  25. $offset = ($page - 1) * $size;
  26. $post=$this
  27. ->field('id,user_id,content,create_time,imgs')
  28. ->where([
  29. 'enable'=>1,
  30. 'del'=>0,
  31. 'org_id'=>$orgId,
  32. ])
  33. ->order('id desc')
  34. ->limit($offset,$size)
  35. ->select();
  36. $post = $post?$post->toArray():[];
  37. foreach ($post as $k=>$val){
  38. $like = Db::name('post_like')
  39. ->where('user_id',$userId)
  40. ->where('post_id',$val['id'])
  41. ->find();
  42. $post[$k]['status'] = $like?1:0;
  43. $post[$k]['imgs'] = empty($val['imgs']) ? [] : explode(',',$val['imgs']);
  44. $user_name=Db::name('user')
  45. ->field('real_name,head_image')
  46. ->where('id',$val['user_id'])
  47. ->find();
  48. $post[$k]['user_name']=$user_name['real_name'];
  49. $post[$k]['head_image']=$user_name['head_image'];
  50. //获取点赞人的名称
  51. $like=Db::name('post_like')
  52. ->alias('l')
  53. ->field('u.real_name,l.id as like_id')
  54. ->join('user u','u.id=l.user_id')
  55. ->where('l.post_id',$val['id'])
  56. ->select();
  57. $post[$k]['like']=$like;
  58. //获取回复信息
  59. $reply=Db::name('post_reply')
  60. ->field('id as reply_id,content,user_id,post_reply_id')
  61. ->where([
  62. 'post_id'=>$val['id'],
  63. 'enable'=>1,
  64. 'del'=>0,
  65. ])
  66. ->order('create_time asc')
  67. ->select();
  68. foreach ($reply as $key=>$value){
  69. $userName=Db::name('user')
  70. ->field('real_name')
  71. ->where('id',$value['user_id'])
  72. ->find();
  73. $replyName=Db::name('post_reply')
  74. ->alias('pr')
  75. ->join('user u','u.id = pr.user_id')
  76. ->field('u.real_name')
  77. ->where('pr.id',$value['post_reply_id'])
  78. ->find();
  79. $reply[$key]['userName']=$userName?$userName['real_name']:'';
  80. $reply[$key]['replyName']=$replyName?$replyName['real_name']:'';
  81. }
  82. $post[$k]['reply']=$reply;
  83. }
  84. return $post;
  85. }
  86. public function del($id){
  87. $data=['del'=>1];
  88. $del=$this->where('id',$id)->update($data);
  89. return $del;
  90. }
  91. }