AdService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\AdModel;
  13. /**
  14. * 广告管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class AdService
  18. * @package App\Services
  19. */
  20. class AdService extends BaseService
  21. {
  22. /**
  23. * 构造函数
  24. * @author wesmiler
  25. * @since 2020/11/11
  26. * AdService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new AdModel();
  31. }
  32. /**
  33. * 添加或编辑
  34. * @return array
  35. * @since 2020/11/11
  36. * @author wesmiler
  37. */
  38. public function edit()
  39. {
  40. $data = request()->all();
  41. // 图片处理
  42. $cover = trim($data['cover']);
  43. if (strpos($cover, "temp")) {
  44. $data['cover'] = save_image($cover, 'ad');
  45. } else {
  46. $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
  47. }
  48. // 开始时间
  49. if (isset($data['start_time']) && $data['start_time']) {
  50. $data['start_time'] = strtotime($data['start_time']);
  51. }
  52. // 结束时间
  53. if (isset($data['end_time']) && $data['end_time']) {
  54. $data['end_time'] = strtotime($data['end_time']);
  55. }
  56. return parent::edit($data); // TODO: Change the autogenerated stub
  57. }
  58. /**
  59. * 获取广告轮播列表数据
  60. * @param $sortId 广告位ID
  61. * @return array|mixed
  62. */
  63. public function geListBySort($sortId, $num=6){
  64. $showNum = ConfigService::make()->getConfigByCode("index_ad_{$sortId}_num");
  65. $num = $showNum? $showNum : $num;
  66. $cacheKey = "caches:index:adverts:list_sort_{$sortId}_{$num}";
  67. $dataList = RedisService::get($cacheKey);
  68. if($dataList){
  69. return $dataList;
  70. }
  71. $dataList = $this->model::where(['ad_sort_id'=> $sortId,'mark'=> 1,'status'=> 1])
  72. ->where(function($query){
  73. $query->where('start_time',0)->orWhere('end_time', 0)->orWhere(function($query){
  74. $query->where('start_time','>=', time())->where('end_time','<=', time());
  75. });
  76. })
  77. ->select(['id','title','cover','type','description','url','view_num','width','height','status','sort'])
  78. ->orderBy('sort','asc')
  79. ->orderBy('create_time','desc')
  80. ->limit($num)
  81. ->get()
  82. ->each(function($item, $k){
  83. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  84. });
  85. $dataList = $dataList? $dataList->toArray() :[];
  86. if($dataList){
  87. RedisService::set($cacheKey, $dataList, rand(10, 30));
  88. }
  89. return $dataList;
  90. }
  91. /**
  92. * 获取广告位对应单广告数据
  93. * @param $sortId 广告位ID
  94. * @return array|mixed
  95. */
  96. public function geDataBySort($sortId){
  97. $cacheKey = "caches:index:adverts:data_sort_{$sortId}";
  98. $data = RedisService::get($cacheKey);
  99. if($data){
  100. return $data;
  101. }
  102. $data = $this->model::where(['ad_sort_id'=> $sortId,'mark'=> 1,'status'=> 1])
  103. ->where(function($query){
  104. $query->where('start_time',0)->orWhere('end_time', 0)->orWhere(function($query){
  105. $query->where('start_time','>=', time())->where('end_time','<=', time());
  106. });
  107. })
  108. ->select(['id','title','cover','type','description','url','view_num','width','height','status','sort'])
  109. ->orderBy('sort','asc')
  110. ->orderBy('create_time','desc')
  111. ->first();
  112. $data = $data? $data->toArray() : [];
  113. if($data){
  114. $data['cover'] = $data['cover']? get_image_url($data['cover']) : '';
  115. RedisService::set($cacheKey, $data, rand(10, 30));
  116. }
  117. return $data;
  118. }
  119. }