Article.php 6.0 KB

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