AdService.php 2.6 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\ActionLogModel;
  13. use App\Models\AdModel;
  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. * @return array
  38. * @since 2020/11/11
  39. * @author laravel开发员
  40. */
  41. public function edit()
  42. {
  43. $data = request()->all();
  44. // 图片处理
  45. $cover = trim($data['cover']);
  46. if (strpos($cover, "temp")) {
  47. $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
  48. }
  49. ActionLogModel::setTitle("添加或编辑轮播广告");
  50. ActionLogModel::record();
  51. return parent::edit($data); // TODO: Change the autogenerated stub
  52. }
  53. /**
  54. * 按未知获取广告
  55. * @param $position 广告位
  56. * @param $locale 语言:zh-中文,en-英文
  57. * @param int $limit
  58. * @return array|mixed
  59. */
  60. public function getListByType($position, $locale, $limit=6)
  61. {
  62. $cahceKey = "caches:adverts:{$position}_{$locale}_{$limit}";
  63. $datas = RedisService::get($cahceKey);
  64. if($datas){
  65. return $datas;
  66. }
  67. $datas = $this->model->where(['position'=> $position,'locale'=> $locale,'status'=>1,'mark'=>1])
  68. ->select(['id','title','cover','locale','sort','url','status'])
  69. ->limit($limit? $limit : 6)
  70. ->get();
  71. $datas = $datas? $datas->toArray() : [];
  72. if($datas){
  73. foreach ($datas as &$item){
  74. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  75. }
  76. RedisService::set($cahceKey, $datas, rand(3600, 7200));
  77. }
  78. return $datas;
  79. }
  80. }