AdService.php 2.4 KB

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