Announcement.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\AnnouncementModel;
  5. class Announcement extends Api
  6. {
  7. protected array $noNeedLogin = ['list','show'];
  8. const toLang = ['zh' => 0, 'en' => 1];
  9. protected int $lan = 0;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->lan = self::toLang[$this->request->getLan()];
  14. }
  15. //列表
  16. public function list(AnnouncementModel $announcementModel)
  17. {
  18. $typeId = $this->request->param('type_id', 1, 'intval');
  19. $paginator = $announcementModel->where('type_id', $typeId)
  20. ->where('status', 1)->where('to_lang', $this->lan)
  21. ->order('id DESC,weigh desc')
  22. ->field('id,to_lang,img_url,createtime,title as title,introduction,body')
  23. ->paginate($this->pageSize);
  24. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  25. }
  26. public function show(int $id)
  27. {
  28. $info = (new AnnouncementModel)
  29. ->where('id', $id)
  30. ->where('status', 1)->where('to_lang', $this->lan)
  31. ->field('id,img_url,to_lang,createtime,title,introduction,body')
  32. ->find();
  33. if(empty($info)){
  34. $this->error('公告信息不存');
  35. }
  36. $this->success('', $info);
  37. }
  38. }