AdService.php 3.0 KB

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