作者 lyh

gx

... ... @@ -59,7 +59,7 @@ class CategoryController extends BaseController
$v['url'] = $this->user['domain'] . RouteMap::getRoute(RouteMap::SOURCE_PRODUCT_CATE, $v['id'], $v['project_id']);
$cate_ids = $category->getChildIdsArr($v['id']);
$v['product_num'] = CategoryRelated::whereIn('cate_id', $cate_ids)->distinct()->count('product_id');
// $v['image_link'] = getImageUrl($v['image']);
$v['image_link'] = getImageUrl($v['image']);
return $v;
}
... ... @@ -82,8 +82,15 @@ class CategoryController extends BaseController
return $data;
}
public function info(Request $request, CategoryLogic $logic){
$request->validate([
/**
* @remark :获取详情
* @name :info
* @author :lyh
* @method :post
* @time :2023/8/21 17:12
*/
public function info(CategoryLogic $logic){
$this->request->validate([
'id'=>'required'
],[
'id.required' => 'ID不能为空'
... ... @@ -92,12 +99,27 @@ class CategoryController extends BaseController
return $this->success($data);
}
/**
* @remark :保存
* @name :save
* @author :lyh
* @method :post
* @time :2023/8/21 17:13
*/
public function save(CategoryRequest $request, CategoryLogic $logic)
{
$data = $logic->save($this->param);
return $this->success($data);
$request->validated();
$logic->categorySave();
$this->response('success');
}
/**
* @remark :删除数据
* @name :delete
* @author :lyh
* @method :post
* @time :2023/8/21 17:20
*/
public function delete(Request $request, CategoryLogic $logic)
{
$request->validate([
... ...
... ... @@ -23,7 +23,7 @@ class CategoryLogic extends BaseLogic
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new Category();
}
... ... @@ -41,6 +41,13 @@ class CategoryLogic extends BaseLogic
return $this->success($data);
}
/**
* @remark :获取详情
* @name :getInfo
* @author :lyh
* @method :post
* @time :2023/8/21 17:14
*/
public function getInfo($id)
{
$info = $this->model->read(['id'=>$id]);
... ... @@ -49,26 +56,29 @@ class CategoryLogic extends BaseLogic
return $this->success($info);
}
public function save($param){
$param['pid'] = $param['pid'] ?? 0;
if(!empty($param['pid'])){
if(!empty($param['id']) && $param['pid'] == $param['id']){
$this->fail('上级分类不能是本分类');
}
$p_cate = $this->getCacheInfo($param['pid']);
if(!$p_cate){
$this->fail('上级分类不存在');
}
}
/**
* @remark :保存分类
* @name :categorySave
* @author :lyh
* @method :post
* @time :2023/8/21 17:14
*/
public function categorySave(){
DB::beginTransaction();
try {
$res = parent::save($param);
if(isset($this->param['id']) && !empty($this->param['id'])){
$this->handleEditParam($this->param);
$id = $this->param['id'];
$this->model->edit($this->param,['id'=>$this->param['id']]);
}else{
$this->param['project_id'] = $this->user['project_id'];
$id = $this->model->addReturnId();
}
//路由映射
$route = RouteMap::setRoute($param['title'], RouteMap::SOURCE_PRODUCT_CATE, $res['id'], $this->user['project_id']);
$route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_CATE, $id, $this->user['project_id']);
DB::commit();
} catch (\Exception $e){
DB::rollBack();
errorLog('产品分类保存失败', $param, $e);
$this->fail('保存失败');
}
//通知更新
... ... @@ -76,6 +86,25 @@ class CategoryLogic extends BaseLogic
return $this->success();
}
public function handleEditParam(&$param){
if($param['pid'] == $param['id']){
$this->fail('上级分类不能是本分类');
}
$info = $this->model->read(['id'=>$param['id']]);
$sub_info = $this->model->read(['pid'=>$param['id']]);
if(($info['pid'] != $param['pid']) && ($sub_info != false)){
$this->fail('当前分类拥有字分类,不允许修改上级分类');
}
return true;
}
/**
* @remark :删除
* @name :delete
* @author :lyh
* @method :post
* @time :2023/8/21 17:23
*/
public function delete($ids, $map = []){
$ids= array_filter(Arr::splitFilterToArray($ids), 'intval');
... ...
... ... @@ -17,40 +17,6 @@ class Base extends Model
];
/**
* 监听模型事件
* @author zbj
* @date 2023/4/25
*/
protected static function booted()
{
//模型实例操作才会触发
//保存前数据 $row->original['xx']
//保存后数据 $row->xx
static::saved(function ($row) {
//删除缓存
$row->original && static::clearCache($row);
});
static::deleted(function ($row) {
//删除缓存
$row->original && static::clearCache($row);
});
}
/**
* 删除缓存 子类重写此方法
*
* @param $row
* @return bool
* @author zbj
* @date 2023/4/25
*/
public static function clearCache($row){
return true;
}
/**
* 日期序列化 勿删 删了时间就不是东八区时间了哈
* @param \DateTimeInterface $date
* @return string
... ... @@ -100,11 +66,11 @@ class Base extends Model
}
/**
* @name :获取单条数据详情
* @param array
* @return mixed
* @author :liyuhang
* @method get
* @remark :获取数据详情
* @name :read
* @author :lyh
* @method :post
* @time :2023/8/21 17:19
*/
public function read($condition,$filed = ['*'])
{
... ... @@ -118,10 +84,11 @@ class Base extends Model
}
/**
* @name :新增
* @return void
* @author :liyuhang
* @method post
* @remark :添加数据
* @name :add
* @author :lyh
* @method :post
* @time :2023/8/21 17:18
*/
public function add($data){
$data['created_at'] = date('Y-m-d H:i:s');
... ... @@ -130,10 +97,24 @@ class Base extends Model
}
/**
* @name :编辑
* @return void
* @author :liyuhang
* @method post
* @remark :保存返回数据主键
* @name :addReturnId
* @author :lyh
* @method :post
* @time :2023/8/21 17:17
*/
public function addReturnId($data){
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = $data['created_at'];
return $this->insertGetId($data);
}
/**
* @remark :编辑
* @name :edit
* @author :lyh
* @method :post
* @time :2023/8/21 17:18
*/
public function edit($data,$condition){
if(isset($data['id']) && !empty($data['id'])){
... ... @@ -144,11 +125,13 @@ class Base extends Model
return $query->update($data);
}
/**
* @name : 删除数据
* @return void
* @author :liyuhang
* @method
* @remark :删除数据
* @name :del
* @author :lyh
* @method :post
* @time :2023/8/21 17:18
*/
public function del($condition){
$query = $this->formatQuery($condition);
... ... @@ -233,4 +216,36 @@ class Base extends Model
return $query;
}
/**
* 监听模型事件
* @author zbj
* @date 2023/4/25
*/
protected static function booted()
{
//保存前数据 $row->original['xx']
//保存后数据 $row->xx
static::saved(function ($row) {
//删除缓存
$row->original && static::clearCache($row);
});
static::deleted(function ($row) {
//删除缓存
$row->original && static::clearCache($row);
});
}
/**
* 删除缓存 子类重写此方法
*
* @param $row
* @return bool
* @author zbj
* @date 2023/4/25
*/
public static function clearCache($row){
return true;
}
}
... ...