AdService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\AdModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 广告管理-服务类
  17. * Class AdService
  18. * @package App\Services\Api
  19. */
  20. class AdService extends BaseService
  21. {
  22. /**
  23. * 构造函数
  24. * AdService constructor.
  25. */
  26. public function __construct()
  27. {
  28. $this->model = new AdModel();
  29. }
  30. /**
  31. * 静态入口
  32. * @return AdService|null
  33. */
  34. public static function make()
  35. {
  36. return parent::make(); // TODO: Change the autogenerated stub
  37. }
  38. /**
  39. * 获取广告列表
  40. * @param int $position 广告位置:1-首页
  41. * @param int $listRows 返回数量
  42. * @return array|false|mixed
  43. */
  44. public function getList($position=1, $listRows = 6)
  45. {
  46. $cacheKey = "caches:adverts:index_{$position}_{$listRows}";
  47. if($list = RedisService::get($cacheKey)){
  48. return $list;
  49. }
  50. $list = $this->model->where(['position'=> $position,'mark'=>1,'status'=>1])
  51. ->select(['id','title','cover as image','type'])
  52. ->orderBy('sort','desc')
  53. ->limit($listRows)
  54. ->get()
  55. ->each(function($item, $k){
  56. $item['image'] = $item['image']? get_image_url($item['image']) : '';
  57. });
  58. $list = $list? $list->toArray() : [];
  59. if($list){
  60. RedisService::set($cacheKey, $list, rand(10, 30));
  61. }
  62. return $list;
  63. }
  64. }