作者 赵彬吉

update

@@ -167,20 +167,19 @@ class Logic @@ -167,20 +167,19 @@ class Logic
167 if(!$ids){ 167 if(!$ids){
168 $this->fail('ID不能为空'); 168 $this->fail('ID不能为空');
169 } 169 }
170 - $map[] = ['id', 'in', $ids];  
171 -  
172 - $res = $this->formatQuery($map)->delete();  
173 - if($res){  
174 170
  171 + foreach ($ids as $id){
  172 + $model = $this->getCacheInfo($id);
  173 + if(!$model){
  174 + continue;
  175 + }
  176 + $model->delete();
175 if($this->is_cache){ 177 if($this->is_cache){
176 - foreach ($ids as $id){  
177 - Cache::forget($this->getInfoCacheKey($id));  
178 - } 178 + Cache::forget($this->getInfoCacheKey($id));
179 } 179 }
180 - return $this->success();  
181 - }else{  
182 - $this->fail('删除失败');  
183 } 180 }
  181 +
  182 + return $this->success();
184 } 183 }
185 184
186 /** 185 /**
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 namespace App\Models; 3 namespace App\Models;
4 4
5 use Illuminate\Database\Eloquent\Model; 5 use Illuminate\Database\Eloquent\Model;
  6 +use Illuminate\Support\Facades\Cache;
6 use Illuminate\Support\Facades\DB; 7 use Illuminate\Support\Facades\DB;
7 8
8 class Base extends Model 9 class Base extends Model
@@ -15,6 +16,41 @@ class Base extends Model @@ -15,6 +16,41 @@ class Base extends Model
15 'created_at' => 'datetime:Y-m-d H:i:s', 16 'created_at' => 'datetime:Y-m-d H:i:s',
16 'updated_at' => 'datetime:Y-m-d H:i:s', 17 'updated_at' => 'datetime:Y-m-d H:i:s',
17 ]; 18 ];
  19 +
  20 + /**
  21 + * 监听模型事件
  22 + * @author zbj
  23 + * @date 2023/4/25
  24 + */
  25 + protected static function booted()
  26 + {
  27 + //模型实例操作才会触发
  28 +
  29 + //保存前数据 $row->original['xx']
  30 + //保存后数据 $row->xx
  31 + static::saved(function ($row) {
  32 + //删除缓存
  33 + $row->original && static::clearCache($row);
  34 + });
  35 +
  36 + static::deleted(function ($row) {
  37 + //删除缓存
  38 + $row->original && static::clearCache($row);
  39 + });
  40 + }
  41 +
  42 + /**
  43 + * 删除缓存 子类重写此方法
  44 + *
  45 + * @param $row
  46 + * @return bool
  47 + * @author zbj
  48 + * @date 2023/4/25
  49 + */
  50 + public static function clearCache($row){
  51 + return true;
  52 + }
  53 +
18 /** 54 /**
19 * 日期序列化 勿删 删了时间就不是东八区时间了哈 55 * 日期序列化 勿删 删了时间就不是东八区时间了哈
20 * @param \DateTimeInterface $date 56 * @param \DateTimeInterface $date
@@ -56,4 +56,9 @@ class Menu extends Base @@ -56,4 +56,9 @@ class Menu extends Base
56 } 56 }
57 return $data; 57 return $data;
58 } 58 }
  59 +
  60 + public static function clearCache($row){
  61 + $cache_key = 'manage_menu_' . $row->original['route_name'];
  62 + Cache::forget($cache_key);
  63 + }
59 } 64 }