NoticeService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\NoticeModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 通知公告-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class NoticeService
  20. * @package App\Services\Api
  21. */
  22. class NoticeService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * NoticeService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new NoticeModel();
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 列表
  48. * @param int $num
  49. * @return array|mixed
  50. */
  51. public function getDataList($num = 9)
  52. {
  53. $cacheKey = "caches:index:notices";
  54. $datas = RedisService::get($cacheKey);
  55. if($datas){
  56. return $datas;
  57. }
  58. $datas = $this->model->where(['status'=>1,'mark'=>1])
  59. ->select(['id','title','create_time','content','status'])
  60. ->limit($num)
  61. ->get();
  62. $datas = $datas? $datas->toArray() : [];
  63. if($datas){
  64. RedisService::set($cacheKey, $datas, rand(300, 600));
  65. }
  66. return $datas;
  67. }
  68. /**
  69. * 获取详情
  70. * @param $id
  71. * @return array|mixed
  72. */
  73. public function getInfo($id)
  74. {
  75. $cacheKey = "caches:notices:info_{$id}";
  76. $info = RedisService::get($cacheKey);
  77. if($info){
  78. return $info;
  79. }
  80. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  81. ->select(['id','title','author','content','create_time'])
  82. ->first();
  83. $info = $info? $info->toArray() : [];
  84. if($info){
  85. $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
  86. $info['content'] = get_format_content($info['content']);
  87. RedisService::set($cacheKey, $info, rand(5,10));
  88. }
  89. return $info;
  90. }
  91. }