Article.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\client\controller;
  3. use think\facade\Request;
  4. use app\event\FootPrints;
  5. use app\model\VideoType;
  6. use app\model\Article as ArticleModel;
  7. class Article extends Base
  8. {
  9. /**
  10. * 文章列表
  11. */
  12. public function list($page, $limit, $type = '', $label = '', $keyword = '')
  13. {
  14. $condition[] = ['root_id', '=', request()->token['root_org']];
  15. $condition[] = ['publish', '=', 1];
  16. $condition[] = ['delete_time', '=', 0];
  17. empty($label) ?: $condition[] = ['label', '=', $label];
  18. empty($type) ?: $condition[] = ['type', '=', $type];
  19. empty($keyword) ?: $condition[] = ['title', 'like', '%' . $keyword . '%'];
  20. // 获取数据
  21. $data = ArticleModel::where($condition)->withAttr('content', function ($value, $data) {
  22. $value = htmlspecialchars_decode($value);
  23. $value = preg_replace('/\s/', '', $value);
  24. $value = str_replace("&nbsp;", "", $value);
  25. $value = strip_tags($value);
  26. $text = mb_substr($value, 0, 40, "utf-8");
  27. return $text;
  28. })->field('id,title,content,cover_img,cover_share_img')->page($page, $limit)->order('uploadtime desc')->select()->toArray();
  29. $count = ArticleModel::where($condition)->count();
  30. return json(['code' => 0, 'data' => $data, 'count' => $count, 'msg' => '获取成功']);
  31. }
  32. /**
  33. * 类型获取
  34. */
  35. public function type()
  36. {
  37. $type = VideoType::field('id,name')->where(['pid' => 0, 'type' => 'article', 'root_id' => request()->token['root_org']])->select()->toArray();
  38. $label = VideoType::field('id,pid,name')->where([['pid', '>', 0], ['type', '=', 'article'], ['root_id', '=', request()->token['root_org']]])->select()->toArray();
  39. foreach ($type as &$val) {
  40. $val['sonLabel'] = [];
  41. foreach ($label as $typeVal) {
  42. if ($val['id'] == $typeVal['pid']) {
  43. $val['sonLabel'][] = $typeVal;
  44. }
  45. }
  46. }
  47. return json(['code' => 0, 'data' => $type, 'msg' => '获取成功']);
  48. }
  49. /**
  50. * 获取视频详情
  51. */
  52. public function info($article_id)
  53. {
  54. $article = ArticleModel::field('id,title,content,cover_img,hot,cover_share_img')->where(['id' => $article_id, 'publish' => 1, 'delete_time' => 0])->find();
  55. if (empty($article)) {
  56. $article = [
  57. 'title' => '该文章不存在',
  58. 'content' => '您访问的内容不存在',
  59. 'cover_img' => '',
  60. 'hot' => 0
  61. ];
  62. return json(['code' => 0, 'msg' => '获取成功', 'data' => $article]);
  63. }
  64. $token = request()->token;
  65. if (!$token['isEmployee']) {
  66. ArticleModel::where('id', '=', $article_id)->inc('hot')->update();
  67. ArticleModel::where('id', $article_id)->inc('view_times')->update();
  68. }
  69. // 添加足迹
  70. if (!empty($token['uid']) && !$token['isEmployee']) {
  71. event(new FootPrints($token['uid'], $token['share_employee'] ?? 0, $token['share_org'] ?? 0, $article));
  72. }
  73. return json(['code' => 0, 'msg' => '获取成功', 'data' => $article]);
  74. }
  75. }