Article.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\mainapp\controller;
  3. use app\mainapp\BaseController;
  4. use app\common\model\User as UserModel;
  5. use app\common\model\Article as ArticleModel;
  6. use app\common\model\ArticleCate as ArticleCateModel;
  7. use app\common\model\ArticleComment as ArticleCommentModel;
  8. use app\common\model\ArticleThumb as ArticleThumbModel;
  9. use app\common\model\ArticleCollect as ArticleCollectModel;
  10. use echowx\WxProgram;
  11. class Article extends BaseController
  12. {
  13. // 文章评论
  14. public function listComment()
  15. {
  16. $ppage = input('ppage/d', 1);
  17. $psize = input('psize/d', 20);
  18. $articleid = input('articleid/d', 0);
  19. $userid = input('userid/d', 0);
  20. $plist = ArticleCommentModel::with(['user','puser'])->where(['articleid'=>$articleid])
  21. ->whereRaw(" status=:status1 OR ( status=:status2 AND userid=:userid ) ", ['status1' => 1, 'status2' => 2, 'userid' => $userid])
  22. ->order('id','desc')->page($ppage)->limit($psize)->select();
  23. $count = ArticleCommentModel::where(['articleid'=>$articleid])
  24. ->whereRaw(" status=:status1 OR ( status=:status2 AND userid=:userid ) ", ['status1' => 1, 'status2' => 2, 'userid' => $userid])
  25. ->count();
  26. page_result(0, "", array(
  27. 'plist' => $plist,
  28. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
  29. 'count' => $count,
  30. ));
  31. }
  32. public function sendComment()
  33. {
  34. $details = input('details/s', '', 'trim');
  35. if (empty($details)){
  36. page_result(1, "评论内容不能为空");
  37. }
  38. $wxprogram = new WxProgram();
  39. $res = $wxprogram->security_msg_sec_check($details);
  40. if ($res==false){
  41. page_result(1, "内容含有违法违规内容");
  42. }
  43. $articleid = input('articleid/d', 0);
  44. $article = ArticleModel::findOrEmpty($articleid);
  45. if ($article->isEmpty()){
  46. page_result(1, "文章资讯信息不存在");
  47. }
  48. $userid = input('userid/d', 0);
  49. $user = UserModel::findOrEmpty($userid);
  50. if ($user->isEmpty()){
  51. page_result(1, "用户信息不存在");
  52. }
  53. $puserid = input('puserid/d', 0);
  54. $newcomment = new ArticleCommentModel;
  55. $newcomment->save([
  56. 'userid' => $userid,
  57. 'articleid' => $articleid,
  58. 'puserid' => $puserid,
  59. 'details' => $details,
  60. 'createtime' => time(),
  61. 'status' => 2
  62. ]);
  63. $comment = ArticleCommentModel::with(['user','puser'])->where('id', '=', $newcomment->id)->select();
  64. page_result(0, "", array(
  65. 'comment' => $comment
  66. ));
  67. }
  68. // 详情
  69. public function getArticle()
  70. {
  71. $articleid = input('articleid/d', 0);
  72. $article = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->append(['createtime_text'])->findOrEmpty($articleid);
  73. if ($article->isEmpty()){
  74. page_result(1, "文章资讯信息不存在");
  75. }
  76. $article->volume += 1;
  77. $article->save();
  78. $userid = input('userid/d', 0);
  79. $mythumb = ArticleThumbModel::where([['userid','=',$userid],['articleid','=',$articleid]])->count();
  80. $mycollect = ArticleCollectModel::where([['userid','=',$userid],['articleid','=',$articleid]])->count();
  81. $commentlist = ArticleCommentModel::with(['user','puser'])->where(['articleid'=>$articleid,'status'=>1])->order('id','desc')->limit(3)->select();
  82. $articlelist = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->where([['status','=',1],['createtime','<=',time()]])->order(['priority'=>'desc','id'=>'desc'])->limit(3)->select();
  83. page_result(0, "", array(
  84. 'article' => $article,
  85. 'mythumb' => $mythumb,
  86. 'mycollect' => $mycollect,
  87. 'articlelist' => $articlelist,
  88. 'commentlist' => $commentlist
  89. ));
  90. }
  91. // 点赞
  92. public function setThumb()
  93. {
  94. $articleid = input('articleid/d', 0);
  95. $article = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->findOrEmpty($articleid);
  96. if ($article->isEmpty()){
  97. page_result(1, "文章资讯信息不存在");
  98. }
  99. $userid = input('userid/d', 0);
  100. if ($userid==0){
  101. page_result(1, "你还未登录,不能点赞");
  102. }
  103. $thumb = ArticleThumbModel::where([['userid','=',$userid],['articleid','=',$articleid]])->findOrEmpty();
  104. $mythumb = 0;
  105. if ($thumb->isEmpty()){
  106. ArticleThumbModel::create([
  107. 'userid' => $userid,
  108. 'articleid' => $articleid,
  109. 'createtime' => time()
  110. ]);
  111. $mythumb = 1;
  112. }else{
  113. $thumb->delete();
  114. }
  115. $thumbcount = ArticleThumbModel::where('articleid','=',$articleid)->count();
  116. page_result(0, "", array(
  117. 'mythumb' => $mythumb,
  118. 'thumbcount' => $thumbcount
  119. ));
  120. }
  121. // 收藏
  122. public function setCollect()
  123. {
  124. $articleid = input('articleid/d', 0);
  125. $article = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->findOrEmpty($articleid);
  126. if ($article->isEmpty()){
  127. page_result(1, "文章资讯信息不存在");
  128. }
  129. $userid = input('userid/d', 0);
  130. if ($userid==0){
  131. page_result(1, "你还未登录,不能点赞");
  132. }
  133. $collect = ArticleCollectModel::where([['userid','=',$userid],['articleid','=',$articleid]])->findOrEmpty();
  134. $mycollect = 0;
  135. if ($collect->isEmpty()){
  136. ArticleCollectModel::create([
  137. 'userid' => $userid,
  138. 'articleid' => $articleid,
  139. 'createtime' => time()
  140. ]);
  141. $mycollect = 1;
  142. }else{
  143. $collect->delete();
  144. }
  145. page_result(0, "", array(
  146. 'mycollect' => $mycollect
  147. ));
  148. }
  149. // 列表
  150. public function listArticle()
  151. {
  152. $ppage = input('ppage/d', 1);
  153. $psize = input('psize/d', 20);
  154. $map[] = ['status','=',1];
  155. $map[] = ['createtime','<=',time()];
  156. $cateid = input('cateid/d');
  157. if ($cateid!=0){
  158. $map[] = ['cateid', '=', $cateid];
  159. }
  160. $plist = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->where($map)->order(['priority'=>'desc','id'=>'desc'])->page($ppage)->limit($psize)->append(['createtime_text'])->select();
  161. page_result(0, "", array(
  162. 'plist' => $plist,
  163. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
  164. ));
  165. }
  166. // 全部分类
  167. public function allCate()
  168. {
  169. $allcate = ArticleCateModel::where('status',1)->order(['priority'=>'desc','id'=>'desc'])->select()->toArray();
  170. array_unshift( $allcate, array('id'=>0,'title'=>'全部') );
  171. page_result(0, "", array('allcate'=>$allcate));
  172. }
  173. }