Post.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace addons\blog\controller\wxapp;
  3. use addons\blog\model\Category;
  4. use addons\blog\model\Post as PostModel;
  5. use addons\blog\model\Comment;
  6. /**
  7. * 日志
  8. */
  9. class Post extends Base
  10. {
  11. protected $noNeedLogin = ['*'];
  12. /**
  13. * 日志列表
  14. */
  15. public function index()
  16. {
  17. $category = (int)$this->request->request('category/d');
  18. $page = (int)$this->request->request('page/d', 1);
  19. $page = max(1, $page);
  20. $postList = PostModel::with('category')
  21. ->where('status', 'normal')
  22. ->where($category ? "category_id='{$category}'" : "1=1")
  23. ->field('id,category_id,title,summary,diyname,image,createtime')
  24. ->page($page)
  25. ->order('weigh desc,id desc')
  26. ->select();
  27. foreach ($postList as $index => &$item) {
  28. $item['summary'] = mb_substr(trim(strip_tags($item['summary'])), 0, 100);
  29. }
  30. $this->success('', ['postList' => $postList]);
  31. }
  32. /**
  33. * 日志详情
  34. */
  35. public function detail()
  36. {
  37. $id = $this->request->request('id/d');
  38. $post = PostModel::get($id);
  39. if (!$post || $post['status'] == 'hidden') {
  40. $this->error(__('No specified article found'));
  41. }
  42. $category = Category::get($post['category_id']);
  43. if (!$category) {
  44. $this->error(__('No specified channel found'));
  45. }
  46. $post->setInc("views", 1);
  47. $commentList = Comment::where('post_id', $post->id)
  48. ->order('createtime', 'desc')
  49. ->where('status', 'normal')
  50. ->limit(10)
  51. ->select();
  52. foreach ($commentList as $index => $item) {
  53. $item->visible(['id', 'username', 'avatar', 'content', 'comments']);
  54. }
  55. $category->visible(['id', 'name']);
  56. $post->visible(['id', 'title', 'comments', 'content', 'description', 'image', 'summary', 'thumb', 'url', 'views']);
  57. $this->request->token();
  58. $this->success('', ['postInfo' => $post, 'categoryInfo' => $category, 'commentList' => $commentList]);
  59. }
  60. }