Announcement.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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::getTypeList());
  18. }
  19. //获取子分类
  20. public function getSubtype(AnnouncementType $announcementType){
  21. $typeId = $this->request->param('type_id', 1, 'intval');
  22. $this->success('', $announcementType::getSubTypeByPId($typeId));
  23. }
  24. //列表
  25. public function list(AnnouncementModel $announcementModel, AnnouncementType $announcementType)
  26. {
  27. $typeId = $this->request->param('type_id', 1, 'intval');
  28. $paginator = $announcementModel->where('type_id', $typeId)
  29. ->where('status', 1)->where('to_lang', $this->lan)
  30. ->field('id,to_lang,weigh,img_url,createtime,title as title,introduction,body,is_top,sub_type_id')
  31. ->order('is_top desc, weigh desc')
  32. ->paginate($this->pageSize);
  33. $paginator = $announcementType::getSubTypeById($paginator);
  34. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  35. }
  36. public function show(int $id)
  37. {
  38. $info = (new AnnouncementModel)
  39. ->where('id', $id)
  40. //->where('status', 1)
  41. ->where('to_lang', $this->lan)
  42. ->field('id,img_url,to_lang,createtime,title,introduction,body')
  43. ->find();
  44. if(empty($info)){
  45. $this->error('公告信息不存');
  46. }
  47. $this->success('', $info);
  48. }
  49. }