Announcement.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\AnnouncementModel;
  5. use app\common\model\AnnouncementType;
  6. class Announcement extends Api
  7. {
  8. protected array $noNeedLogin = ['gettype','list','show'];
  9. protected string $lan = '';
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->lan = $this->request->getLan();
  14. }
  15. //获取大分类
  16. public function gettype(AnnouncementModel $announcementModel){
  17. $this->success('', $announcementModel::getTypeAllList($this->lan));
  18. }
  19. //列表
  20. public function list(AnnouncementModel $announcementModel, AnnouncementType $announcementType)
  21. {
  22. $typeId = $this->request->param('type_id', 1, 'intval');
  23. $subTypeId = $this->request->param('sub_type_id', 0, 'intval'); //子分类id
  24. $title = $this->request->param('title/s', ''); //标题
  25. $paginator = $announcementModel->where('type_id', $typeId)->where('status', 1)->where('to_lang', $this->lan);
  26. //子分类
  27. if($subTypeId > 0) $paginator =$paginator->where('find_in_set(:id,sub_type_id)',['id'=>$subTypeId]);
  28. //标题
  29. if(!empty($title)) $paginator =$paginator->where('title', 'LIKE', '%'.$title.'%');
  30. $paginator =$paginator->field('id,to_lang,weigh,img_url,createtime,title as title,is_top,sub_type_id,introduction,body')->order('is_top desc, weigh desc, createtime desc')->paginate($this->pageSize);
  31. $paginator = $announcementType::getSubTypeById($paginator, $this->lan);
  32. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  33. }
  34. public function show(int $id)
  35. {
  36. $info = (new AnnouncementModel)
  37. ->where('id', $id)
  38. //->where('status', 1)
  39. ->where('to_lang', $this->lan)
  40. ->field('id,img_url,to_lang,createtime,title,introduction,body')
  41. ->find();
  42. if(empty($info)){
  43. $this->error('公告信息不存');
  44. }
  45. $this->success('', $info);
  46. }
  47. }