AdService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Common;
  12. use App\Models\AdModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 广告管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class AdService
  20. * @package App\Services\Common
  21. */
  22. class AdService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * AdService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new AdModel();
  33. }
  34. /**
  35. * 按广告位获取缓存列表
  36. * @param $position
  37. * @param int $num
  38. * @return array|mixed
  39. */
  40. public function getListByPosition($position, $num=0)
  41. {
  42. $cacheKey = "caches:banners:{$position}";
  43. $datas = RedisService::get($cacheKey);
  44. if($datas){
  45. return $datas;
  46. }
  47. $showNum = \App\Services\ConfigService::make()->getConfigByCode('show_banner_num', 6);
  48. $num = $num? $num : $showNum;
  49. $datas = $this->model->where(['position'=> $position,'status'=> 1,'mark'=>1])
  50. ->select(['id','cover','title','url','description','type'])
  51. ->limit($num)
  52. ->get()
  53. ->each(function($item, $k){
  54. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  55. });
  56. $datas = $datas? $datas->toArray() : [];
  57. if($datas){
  58. RedisService::set($cacheKey, $datas, rand(5, 10));
  59. }
  60. return $datas;
  61. }
  62. /**
  63. * 添加或编辑
  64. * @return array
  65. * @since 2020/11/11
  66. * @author laravel开发员
  67. */
  68. public function edit()
  69. {
  70. $data = request()->all();
  71. // 图片处理
  72. if(isset($data['cover'])){
  73. $data['cover'] = get_image_path($data['cover']);
  74. }
  75. // 结束时间
  76. if (isset($data['end_time'])) {
  77. $data['end_time'] = strtotime($data['end_time']);
  78. }
  79. return parent::edit($data); // TODO: Change the autogenerated stub
  80. }
  81. }