作者 lyh

gx

@@ -59,7 +59,7 @@ class CategoryController extends BaseController @@ -59,7 +59,7 @@ class CategoryController extends BaseController
59 $v['url'] = $this->user['domain'] . RouteMap::getRoute(RouteMap::SOURCE_PRODUCT_CATE, $v['id'], $v['project_id']); 59 $v['url'] = $this->user['domain'] . RouteMap::getRoute(RouteMap::SOURCE_PRODUCT_CATE, $v['id'], $v['project_id']);
60 $cate_ids = $category->getChildIdsArr($v['id']); 60 $cate_ids = $category->getChildIdsArr($v['id']);
61 $v['product_num'] = CategoryRelated::whereIn('cate_id', $cate_ids)->distinct()->count('product_id'); 61 $v['product_num'] = CategoryRelated::whereIn('cate_id', $cate_ids)->distinct()->count('product_id');
62 -// $v['image_link'] = getImageUrl($v['image']); 62 + $v['image_link'] = getImageUrl($v['image']);
63 return $v; 63 return $v;
64 } 64 }
65 65
@@ -82,8 +82,15 @@ class CategoryController extends BaseController @@ -82,8 +82,15 @@ class CategoryController extends BaseController
82 return $data; 82 return $data;
83 } 83 }
84 84
85 - public function info(Request $request, CategoryLogic $logic){  
86 - $request->validate([ 85 + /**
  86 + * @remark :获取详情
  87 + * @name :info
  88 + * @author :lyh
  89 + * @method :post
  90 + * @time :2023/8/21 17:12
  91 + */
  92 + public function info(CategoryLogic $logic){
  93 + $this->request->validate([
87 'id'=>'required' 94 'id'=>'required'
88 ],[ 95 ],[
89 'id.required' => 'ID不能为空' 96 'id.required' => 'ID不能为空'
@@ -92,12 +99,27 @@ class CategoryController extends BaseController @@ -92,12 +99,27 @@ class CategoryController extends BaseController
92 return $this->success($data); 99 return $this->success($data);
93 } 100 }
94 101
  102 + /**
  103 + * @remark :保存
  104 + * @name :save
  105 + * @author :lyh
  106 + * @method :post
  107 + * @time :2023/8/21 17:13
  108 + */
95 public function save(CategoryRequest $request, CategoryLogic $logic) 109 public function save(CategoryRequest $request, CategoryLogic $logic)
96 { 110 {
97 - $data = $logic->save($this->param);  
98 - return $this->success($data); 111 + $request->validated();
  112 + $logic->categorySave();
  113 + $this->response('success');
99 } 114 }
100 115
  116 + /**
  117 + * @remark :删除数据
  118 + * @name :delete
  119 + * @author :lyh
  120 + * @method :post
  121 + * @time :2023/8/21 17:20
  122 + */
101 public function delete(Request $request, CategoryLogic $logic) 123 public function delete(Request $request, CategoryLogic $logic)
102 { 124 {
103 $request->validate([ 125 $request->validate([
@@ -23,7 +23,7 @@ class CategoryLogic extends BaseLogic @@ -23,7 +23,7 @@ class CategoryLogic extends BaseLogic
23 public function __construct() 23 public function __construct()
24 { 24 {
25 parent::__construct(); 25 parent::__construct();
26 - 26 + $this->param = $this->requestAll;
27 $this->model = new Category(); 27 $this->model = new Category();
28 } 28 }
29 29
@@ -41,6 +41,13 @@ class CategoryLogic extends BaseLogic @@ -41,6 +41,13 @@ class CategoryLogic extends BaseLogic
41 return $this->success($data); 41 return $this->success($data);
42 } 42 }
43 43
  44 + /**
  45 + * @remark :获取详情
  46 + * @name :getInfo
  47 + * @author :lyh
  48 + * @method :post
  49 + * @time :2023/8/21 17:14
  50 + */
44 public function getInfo($id) 51 public function getInfo($id)
45 { 52 {
46 $info = $this->model->read(['id'=>$id]); 53 $info = $this->model->read(['id'=>$id]);
@@ -49,26 +56,29 @@ class CategoryLogic extends BaseLogic @@ -49,26 +56,29 @@ class CategoryLogic extends BaseLogic
49 return $this->success($info); 56 return $this->success($info);
50 } 57 }
51 58
52 - public function save($param){  
53 - $param['pid'] = $param['pid'] ?? 0;  
54 - if(!empty($param['pid'])){  
55 - if(!empty($param['id']) && $param['pid'] == $param['id']){  
56 - $this->fail('上级分类不能是本分类');  
57 - }  
58 - $p_cate = $this->getCacheInfo($param['pid']);  
59 - if(!$p_cate){  
60 - $this->fail('上级分类不存在');  
61 - }  
62 - } 59 + /**
  60 + * @remark :保存分类
  61 + * @name :categorySave
  62 + * @author :lyh
  63 + * @method :post
  64 + * @time :2023/8/21 17:14
  65 + */
  66 + public function categorySave(){
63 DB::beginTransaction(); 67 DB::beginTransaction();
64 try { 68 try {
65 - $res = parent::save($param); 69 + if(isset($this->param['id']) && !empty($this->param['id'])){
  70 + $this->handleEditParam($this->param);
  71 + $id = $this->param['id'];
  72 + $this->model->edit($this->param,['id'=>$this->param['id']]);
  73 + }else{
  74 + $this->param['project_id'] = $this->user['project_id'];
  75 + $id = $this->model->addReturnId();
  76 + }
66 //路由映射 77 //路由映射
67 - $route = RouteMap::setRoute($param['title'], RouteMap::SOURCE_PRODUCT_CATE, $res['id'], $this->user['project_id']); 78 + $route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_CATE, $id, $this->user['project_id']);
68 DB::commit(); 79 DB::commit();
69 } catch (\Exception $e){ 80 } catch (\Exception $e){
70 DB::rollBack(); 81 DB::rollBack();
71 - errorLog('产品分类保存失败', $param, $e);  
72 $this->fail('保存失败'); 82 $this->fail('保存失败');
73 } 83 }
74 //通知更新 84 //通知更新
@@ -76,6 +86,25 @@ class CategoryLogic extends BaseLogic @@ -76,6 +86,25 @@ class CategoryLogic extends BaseLogic
76 return $this->success(); 86 return $this->success();
77 } 87 }
78 88
  89 + public function handleEditParam(&$param){
  90 + if($param['pid'] == $param['id']){
  91 + $this->fail('上级分类不能是本分类');
  92 + }
  93 + $info = $this->model->read(['id'=>$param['id']]);
  94 + $sub_info = $this->model->read(['pid'=>$param['id']]);
  95 + if(($info['pid'] != $param['pid']) && ($sub_info != false)){
  96 + $this->fail('当前分类拥有字分类,不允许修改上级分类');
  97 + }
  98 + return true;
  99 + }
  100 +
  101 + /**
  102 + * @remark :删除
  103 + * @name :delete
  104 + * @author :lyh
  105 + * @method :post
  106 + * @time :2023/8/21 17:23
  107 + */
79 public function delete($ids, $map = []){ 108 public function delete($ids, $map = []){
80 $ids= array_filter(Arr::splitFilterToArray($ids), 'intval'); 109 $ids= array_filter(Arr::splitFilterToArray($ids), 'intval');
81 110
@@ -17,40 +17,6 @@ class Base extends Model @@ -17,40 +17,6 @@ class Base extends Model
17 ]; 17 ];
18 18
19 /** 19 /**
20 - * 监听模型事件  
21 - * @author zbj  
22 - * @date 2023/4/25  
23 - */  
24 - protected static function booted()  
25 - {  
26 - //模型实例操作才会触发  
27 -  
28 - //保存前数据 $row->original['xx']  
29 - //保存后数据 $row->xx  
30 - static::saved(function ($row) {  
31 - //删除缓存  
32 - $row->original && static::clearCache($row);  
33 - });  
34 -  
35 - static::deleted(function ($row) {  
36 - //删除缓存  
37 - $row->original && static::clearCache($row);  
38 - });  
39 - }  
40 -  
41 - /**  
42 - * 删除缓存 子类重写此方法  
43 - *  
44 - * @param $row  
45 - * @return bool  
46 - * @author zbj  
47 - * @date 2023/4/25  
48 - */  
49 - public static function clearCache($row){  
50 - return true;  
51 - }  
52 -  
53 - /**  
54 * 日期序列化 勿删 删了时间就不是东八区时间了哈 20 * 日期序列化 勿删 删了时间就不是东八区时间了哈
55 * @param \DateTimeInterface $date 21 * @param \DateTimeInterface $date
56 * @return string 22 * @return string
@@ -100,11 +66,11 @@ class Base extends Model @@ -100,11 +66,11 @@ class Base extends Model
100 } 66 }
101 67
102 /** 68 /**
103 - * @name :获取单条数据详情  
104 - * @param array  
105 - * @return mixed  
106 - * @author :liyuhang  
107 - * @method get 69 + * @remark :获取数据详情
  70 + * @name :read
  71 + * @author :lyh
  72 + * @method :post
  73 + * @time :2023/8/21 17:19
108 */ 74 */
109 public function read($condition,$filed = ['*']) 75 public function read($condition,$filed = ['*'])
110 { 76 {
@@ -118,10 +84,11 @@ class Base extends Model @@ -118,10 +84,11 @@ class Base extends Model
118 } 84 }
119 85
120 /** 86 /**
121 - * @name :新增  
122 - * @return void  
123 - * @author :liyuhang  
124 - * @method post 87 + * @remark :添加数据
  88 + * @name :add
  89 + * @author :lyh
  90 + * @method :post
  91 + * @time :2023/8/21 17:18
125 */ 92 */
126 public function add($data){ 93 public function add($data){
127 $data['created_at'] = date('Y-m-d H:i:s'); 94 $data['created_at'] = date('Y-m-d H:i:s');
@@ -130,10 +97,24 @@ class Base extends Model @@ -130,10 +97,24 @@ class Base extends Model
130 } 97 }
131 98
132 /** 99 /**
133 - * @name :编辑  
134 - * @return void  
135 - * @author :liyuhang  
136 - * @method post 100 + * @remark :保存返回数据主键
  101 + * @name :addReturnId
  102 + * @author :lyh
  103 + * @method :post
  104 + * @time :2023/8/21 17:17
  105 + */
  106 + public function addReturnId($data){
  107 + $data['created_at'] = date('Y-m-d H:i:s');
  108 + $data['updated_at'] = $data['created_at'];
  109 + return $this->insertGetId($data);
  110 + }
  111 +
  112 + /**
  113 + * @remark :编辑
  114 + * @name :edit
  115 + * @author :lyh
  116 + * @method :post
  117 + * @time :2023/8/21 17:18
137 */ 118 */
138 public function edit($data,$condition){ 119 public function edit($data,$condition){
139 if(isset($data['id']) && !empty($data['id'])){ 120 if(isset($data['id']) && !empty($data['id'])){
@@ -144,11 +125,13 @@ class Base extends Model @@ -144,11 +125,13 @@ class Base extends Model
144 return $query->update($data); 125 return $query->update($data);
145 126
146 } 127 }
  128 +
147 /** 129 /**
148 - * @name : 删除数据  
149 - * @return void  
150 - * @author :liyuhang  
151 - * @method 130 + * @remark :删除数据
  131 + * @name :del
  132 + * @author :lyh
  133 + * @method :post
  134 + * @time :2023/8/21 17:18
152 */ 135 */
153 public function del($condition){ 136 public function del($condition){
154 $query = $this->formatQuery($condition); 137 $query = $this->formatQuery($condition);
@@ -233,4 +216,36 @@ class Base extends Model @@ -233,4 +216,36 @@ class Base extends Model
233 return $query; 216 return $query;
234 } 217 }
235 218
  219 + /**
  220 + * 监听模型事件
  221 + * @author zbj
  222 + * @date 2023/4/25
  223 + */
  224 + protected static function booted()
  225 + {
  226 + //保存前数据 $row->original['xx']
  227 + //保存后数据 $row->xx
  228 + static::saved(function ($row) {
  229 + //删除缓存
  230 + $row->original && static::clearCache($row);
  231 + });
  232 +
  233 + static::deleted(function ($row) {
  234 + //删除缓存
  235 + $row->original && static::clearCache($row);
  236 + });
  237 + }
  238 +
  239 + /**
  240 + * 删除缓存 子类重写此方法
  241 + *
  242 + * @param $row
  243 + * @return bool
  244 + * @author zbj
  245 + * @date 2023/4/25
  246 + */
  247 + public static function clearCache($row){
  248 + return true;
  249 + }
  250 +
236 } 251 }