|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +use Illuminate\Support\Facades\Cache; |
| 5 | +use ModStart\Core\Assets\AssetsUtil; |
| 6 | + |
| 7 | +/** |
| 8 | + * @Util 内容区块 |
| 9 | + */ |
| 10 | +class MContentBlock |
| 11 | +{ |
| 12 | + const CACHE_KEY_PREFIX = 'ContentBlock:'; |
| 13 | + |
| 14 | + /** |
| 15 | + * @Util 根据name获取内容区块,60分钟缓存 |
| 16 | + * @param $name string name |
| 17 | + * @return array|null |
| 18 | + */ |
| 19 | + public static function getCached($name) |
| 20 | + { |
| 21 | + return Cache::remember(self::CACHE_KEY_PREFIX . "Name:" . $name, 60, function () use ($name) { |
| 22 | + return self::get($name); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @Util 根据id获取内容区块,60分钟缓存 |
| 28 | + * @param $id integer ID |
| 29 | + * @return array|null |
| 30 | + */ |
| 31 | + public static function getByIdCached($id) |
| 32 | + { |
| 33 | + return Cache::remember(self::CACHE_KEY_PREFIX . "Id:" . $id, 60, function () use ($id) { |
| 34 | + return self::getById($id); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + private static function getBy($where) |
| 39 | + { |
| 40 | + $m = \Module\ContentBlock\Model\ContentBlock::where($where) |
| 41 | + ->where(function ($query) { |
| 42 | + $query->whereNull('startTime') |
| 43 | + ->orWhere('startTime', '<=', date('Y-m-d H:i:s')); |
| 44 | + }) |
| 45 | + ->where(function ($query) { |
| 46 | + $query->whereNull('endTime') |
| 47 | + ->orWhere('endTime', '>=', date('Y-m-d H:i:s')); |
| 48 | + }) |
| 49 | + ->orderBy('sort', 'asc') |
| 50 | + ->first(); |
| 51 | + if (empty($m)) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + if ($m['image']) { |
| 55 | + $m['image'] = AssetsUtil::fixFull($m['image']); |
| 56 | + } |
| 57 | + return $m->toArray(); |
| 58 | + } |
| 59 | + |
| 60 | + public static function getById($id) |
| 61 | + { |
| 62 | + return self::getBy([ |
| 63 | + 'id' => $id, |
| 64 | + 'enable' => true, |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + public static function get($name) |
| 69 | + { |
| 70 | + return self::getBy([ |
| 71 | + 'name' => $name, |
| 72 | + 'enable' => true, |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @Util 根据name获取内容区块列表,60分钟缓存 |
| 78 | + * @param $name string 名称 |
| 79 | + * @param $limit int 最多返回多少个 |
| 80 | + * @return array |
| 81 | + */ |
| 82 | + public static function allCached($name, $limit = 0) |
| 83 | + { |
| 84 | + return Cache::remember(self::CACHE_KEY_PREFIX . $name . ':' . $limit, 60, function () use ($name, $limit) { |
| 85 | + return self::all($name, $limit); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private static function allBy($where, $limit) |
| 90 | + { |
| 91 | + $query = \Module\ContentBlock\Model\ContentBlock::where($where) |
| 92 | + ->where(function ($query) { |
| 93 | + $query->whereNull('startTime') |
| 94 | + ->orWhere('startTime', '<=', date('Y-m-d H:i:s')); |
| 95 | + }) |
| 96 | + ->where(function ($query) { |
| 97 | + $query->whereNull('endTime') |
| 98 | + ->orWhere('endTime', '>=', date('Y-m-d H:i:s')); |
| 99 | + }) |
| 100 | + ->orderBy('sort', 'asc'); |
| 101 | + if ($limit > 0) { |
| 102 | + $query = $query->limit($limit); |
| 103 | + } |
| 104 | + $ms = $query->get()->toArray(); |
| 105 | + AssetsUtil::recordsFixFullOrDefault($ms, ['image']); |
| 106 | + return $ms; |
| 107 | + } |
| 108 | + |
| 109 | + public static function all($name, $limit = 0) |
| 110 | + { |
| 111 | + return self::allBy([ |
| 112 | + 'name' => $name, |
| 113 | + 'enable' => true, |
| 114 | + ], $limit); |
| 115 | + } |
| 116 | +} |
0 commit comments