正在显示
9 个修改的文件
包含
366 行增加
和
1 行删除
| @@ -41,7 +41,6 @@ class KeywordController extends BaseController | @@ -41,7 +41,6 @@ class KeywordController extends BaseController | ||
| 41 | 41 | ||
| 42 | public function save(KeywordRequest $request, KeywordLogic $logic) | 42 | public function save(KeywordRequest $request, KeywordLogic $logic) |
| 43 | { | 43 | { |
| 44 | - $this->param['route'] = generateRoute($this->param['title']); | ||
| 45 | $data = $logic->save($this->param); | 44 | $data = $logic->save($this->param); |
| 46 | return $this->success($data); | 45 | return $this->success($data); |
| 47 | } | 46 | } |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Http\Controllers\Bside\Product; | ||
| 4 | + | ||
| 5 | +use App\Helper\Arr; | ||
| 6 | +use App\Http\Controllers\Bside\BaseController; | ||
| 7 | +use App\Http\Logic\Bside\Product\ProductLogic; | ||
| 8 | +use App\Http\Requests\Bside\product\ProductRequest; | ||
| 9 | +use App\Rules\Ids; | ||
| 10 | +use Illuminate\Http\Request; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * Class ProductController | ||
| 14 | + * @package App\Http\Controllers\Bside | ||
| 15 | + * @author zbj | ||
| 16 | + * @date 2023/4/12 | ||
| 17 | + */ | ||
| 18 | +class ProductController extends BaseController | ||
| 19 | +{ | ||
| 20 | + | ||
| 21 | + public function index(ProductLogic $logic) | ||
| 22 | + { | ||
| 23 | + $map = []; | ||
| 24 | + if(!empty($this->param['search'])){ | ||
| 25 | + $map[] = ['title', 'like', "%{$this->param['search']}%"]; | ||
| 26 | + } | ||
| 27 | + $sort = ['id' => 'desc']; | ||
| 28 | + $data = $logic->getList($map, $sort, ['id', 'pid', 'title', 'image', 'keywords', 'describe', 'status','created_at'],0); | ||
| 29 | + return $this->success(Arr::listToTree($data)); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public function info(Request $request, ProductLogic $logic){ | ||
| 33 | + $request->validate([ | ||
| 34 | + 'id'=>'required' | ||
| 35 | + ],[ | ||
| 36 | + 'id.required' => 'ID不能为空' | ||
| 37 | + ]); | ||
| 38 | + $data = $logic->getInfo($this->param['id']); | ||
| 39 | + return $this->success(Arr::twoKeepKeys($data, ['id', 'pid', 'title', 'image', 'keywords', 'describe', 'status'])); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + public function save(ProductRequest $request, ProductLogic $logic) | ||
| 43 | + { | ||
| 44 | + $data = $logic->save($this->param); | ||
| 45 | + return $this->success($data); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public function delete(Request $request, ProductLogic $logic) | ||
| 49 | + { | ||
| 50 | + $request->validate([ | ||
| 51 | + 'ids'=>['required', new Ids()] | ||
| 52 | + ],[ | ||
| 53 | + 'ids.required' => 'ID不能为空' | ||
| 54 | + ]); | ||
| 55 | + | ||
| 56 | + $data = $logic->delete($this->param['ids']); | ||
| 57 | + return $this->success($data); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + //todo Ai生成 关键词和描述 | ||
| 61 | +} |
| @@ -4,7 +4,9 @@ namespace App\Http\Logic\Bside\Product; | @@ -4,7 +4,9 @@ namespace App\Http\Logic\Bside\Product; | ||
| 4 | 4 | ||
| 5 | use App\Helper\Arr; | 5 | use App\Helper\Arr; |
| 6 | use App\Http\Logic\Bside\BaseLogic; | 6 | use App\Http\Logic\Bside\BaseLogic; |
| 7 | +use App\Models\RouteMap; | ||
| 7 | use App\Models\Product\Keyword; | 8 | use App\Models\Product\Keyword; |
| 9 | +use Illuminate\Support\Facades\DB; | ||
| 8 | 10 | ||
| 9 | /** | 11 | /** |
| 10 | * Class KeywordLogic | 12 | * Class KeywordLogic |
| @@ -21,6 +23,20 @@ class KeywordLogic extends BaseLogic | @@ -21,6 +23,20 @@ class KeywordLogic extends BaseLogic | ||
| 21 | $this->model = new Keyword(); | 23 | $this->model = new Keyword(); |
| 22 | } | 24 | } |
| 23 | 25 | ||
| 26 | + public function save($param){ | ||
| 27 | + DB::beginTransaction(); | ||
| 28 | + try { | ||
| 29 | + $res = parent::save($param); | ||
| 30 | + //路由映射 | ||
| 31 | + RouteMap::setRoute($param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $res['id'], $param['project_id'] ?? 0); | ||
| 32 | + DB::commit(); | ||
| 33 | + }catch (\Exception $e){ | ||
| 34 | + DB::rollBack(); | ||
| 35 | + $this->fail('保存失败'); | ||
| 36 | + } | ||
| 37 | + return $this->success(); | ||
| 38 | + } | ||
| 39 | + | ||
| 24 | public function delete($ids){ | 40 | public function delete($ids){ |
| 25 | $ids= array_filter(Arr::splitFilterToArray($ids), 'intval'); | 41 | $ids= array_filter(Arr::splitFilterToArray($ids), 'intval'); |
| 26 | foreach ($ids as $id){ | 42 | foreach ($ids as $id){ |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Http\Logic\Bside\Product; | ||
| 4 | + | ||
| 5 | +use App\Helper\Arr; | ||
| 6 | +use App\Http\Logic\Bside\BaseLogic; | ||
| 7 | +use App\Models\Product\Product; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * Class ProductLogic | ||
| 11 | + * @package App\Http\Logic\Bside\Product | ||
| 12 | + * @author zbj | ||
| 13 | + * @date 2023/4/14 | ||
| 14 | + */ | ||
| 15 | +class ProductLogic extends BaseLogic | ||
| 16 | +{ | ||
| 17 | + public function __construct() | ||
| 18 | + { | ||
| 19 | + parent::__construct(); | ||
| 20 | + | ||
| 21 | + $this->model = new Product(); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public function save($param){ | ||
| 25 | + if(!empty($param['pid'])){ | ||
| 26 | + if(!empty($param['id']) && $param['pid'] == $param['id']){ | ||
| 27 | + $this->fail('上级分类不能是本分类'); | ||
| 28 | + } | ||
| 29 | + $p_cate = Product::find($param['pid']); | ||
| 30 | + if(!$p_cate){ | ||
| 31 | + $this->fail('上级分类不存在'); | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + return parent::save($param); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + public function delete($ids){ | ||
| 38 | + $ids= array_filter(Arr::splitFilterToArray($ids), 'intval'); | ||
| 39 | + foreach ($ids as $id){ | ||
| 40 | + $info = $this->getCacheInfo($id); | ||
| 41 | + if(!$info){ | ||
| 42 | + continue; | ||
| 43 | + } | ||
| 44 | + //是否有子分类 | ||
| 45 | + if(Product::where('pid', $id)->count()){ | ||
| 46 | + $this->fail("分类{$info['title']}存在子分类,不能删除"); | ||
| 47 | + } | ||
| 48 | + //todo 是否有对应商品 | ||
| 49 | + | ||
| 50 | + } | ||
| 51 | + return parent::delete($ids); | ||
| 52 | + } | ||
| 53 | +} |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Http\Requests\Bside\product; | ||
| 4 | + | ||
| 5 | +use Illuminate\Foundation\Http\FormRequest; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * Class ProductRequest | ||
| 9 | + * @package App\Http\Requests\Bside\product | ||
| 10 | + * @author zbj | ||
| 11 | + * @date 2023/4/12 | ||
| 12 | + */ | ||
| 13 | +class ProductRequest extends FormRequest | ||
| 14 | +{ | ||
| 15 | + /** | ||
| 16 | + * Determine if the user is authorized to make this request. | ||
| 17 | + * | ||
| 18 | + * @return bool | ||
| 19 | + */ | ||
| 20 | + public function authorize() | ||
| 21 | + { | ||
| 22 | + return true; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * Get the validation rules that apply to the request. | ||
| 27 | + * | ||
| 28 | + * @return array | ||
| 29 | + */ | ||
| 30 | + public function rules() | ||
| 31 | + { | ||
| 32 | + return [ | ||
| 33 | + 'title'=>'required|max:20', | ||
| 34 | + 'image'=>'required', | ||
| 35 | + 'keywords'=>'required|max:50', | ||
| 36 | + 'describe'=>'required|max:200', | ||
| 37 | + ]; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public function messages() | ||
| 41 | + { | ||
| 42 | + return [ | ||
| 43 | + 'title.required' => '请输入分类名称', | ||
| 44 | + 'title.max' => '分类名称不能超过20个字符', | ||
| 45 | + 'image.required' => '请上传分类图片', | ||
| 46 | + 'keywords.required' => '请输入分类关键词', | ||
| 47 | + 'keywords.max' => '分类关键词不能超过50个字符', | ||
| 48 | + 'describe.required' => '请输入分类描述', | ||
| 49 | + 'describe.max' => '分类描述不能超过200个字符', | ||
| 50 | + ]; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | +} |
| @@ -17,6 +17,18 @@ class Base extends Model | @@ -17,6 +17,18 @@ class Base extends Model | ||
| 17 | ]; | 17 | ]; |
| 18 | 18 | ||
| 19 | /** | 19 | /** |
| 20 | + * 日期序列化 勿删 删了时间就不是东八区时间了哈 | ||
| 21 | + * @param \DateTimeInterface $date | ||
| 22 | + * @return string | ||
| 23 | + * @author zbj | ||
| 24 | + * @date 2023/4/13 | ||
| 25 | + */ | ||
| 26 | + protected function serializeDate(\DateTimeInterface $date): string | ||
| 27 | + { | ||
| 28 | + return $date->format('Y-m-d H:i:s'); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + /** | ||
| 20 | * @name 列表数据 | 32 | * @name 列表数据 |
| 21 | * @return void | 33 | * @return void |
| 22 | * @author :liyuhang | 34 | * @author :liyuhang |
app/Models/Product/Product.php
0 → 100644
app/Models/RouteMap.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Models; | ||
| 4 | + | ||
| 5 | +use Illuminate\Database\Eloquent\Model; | ||
| 6 | +use Illuminate\Support\Facades\Log; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 路由映射表 | ||
| 10 | + * Class RouteMap | ||
| 11 | + * @package App\Http\Models | ||
| 12 | + * @author zbj | ||
| 13 | + * @date 2023/4/17 | ||
| 14 | + */ | ||
| 15 | +class RouteMap extends Model | ||
| 16 | +{ | ||
| 17 | + //设置关联表名 | ||
| 18 | + protected $table = 'gl_route_map'; | ||
| 19 | + | ||
| 20 | + //路由类型 | ||
| 21 | + const SOURCE_PRODUCT = 'product'; | ||
| 22 | + const SOURCE_PRODUCT_CATE = 'product_category'; | ||
| 23 | + const SOURCE_PRODUCT_KEYWORD = 'product_keyword'; | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * 生成路由标识 | ||
| 28 | + * @param $title | ||
| 29 | + * @param $source | ||
| 30 | + * @param $source_id | ||
| 31 | + * @param $project_id | ||
| 32 | + * @return string | ||
| 33 | + * @author zbj | ||
| 34 | + * @date 2023/4/17 | ||
| 35 | + */ | ||
| 36 | + public static function generateRoute($title, $source, $source_id, $project_id){ | ||
| 37 | + $i=1; | ||
| 38 | + $sign = generateRoute($title); | ||
| 39 | + $route = $sign; | ||
| 40 | + while(self::isExist($route, $project_id, $source, $source_id)){ | ||
| 41 | + $route = $sign .'-'.$i; | ||
| 42 | + $i++; | ||
| 43 | + } | ||
| 44 | + return $route; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 路由是否存在 | ||
| 49 | + * @param $route | ||
| 50 | + * @param $source | ||
| 51 | + * @param $source_id | ||
| 52 | + * @param $project_id | ||
| 53 | + * @return bool | ||
| 54 | + * @author zbj | ||
| 55 | + * @date 2023/4/17 | ||
| 56 | + */ | ||
| 57 | + protected static function isExist($route, $source, $source_id, $project_id){ | ||
| 58 | + $fixed = []; //固定的路由 | ||
| 59 | + if(in_array($route, $fixed)){ | ||
| 60 | + return true; | ||
| 61 | + } | ||
| 62 | + $route = self::where('project_id', $project_id)->where('route', $route)->first(); | ||
| 63 | + if($route){ | ||
| 64 | + if($route->source == $source && $route->source_id == $source_id){ | ||
| 65 | + return false; | ||
| 66 | + } | ||
| 67 | + return true; | ||
| 68 | + } | ||
| 69 | + return false; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * @param $title | ||
| 74 | + * @param $source | ||
| 75 | + * @param $source_id | ||
| 76 | + * @param int $project_id | ||
| 77 | + * @param bool $auto | ||
| 78 | + * @return bool | ||
| 79 | + * @throws \Exception | ||
| 80 | + * @author zbj | ||
| 81 | + * @date 2023/4/17 | ||
| 82 | + */ | ||
| 83 | + public static function setRoute($title, $source, $source_id, $project_id = 0, $auto=false){ | ||
| 84 | + $route = $title; | ||
| 85 | + | ||
| 86 | + if($auto){ | ||
| 87 | + $route = self::generateRoute($title, $source, $source_id, $project_id); | ||
| 88 | + } | ||
| 89 | + if(!$route){ | ||
| 90 | + throw new \Exception('路由不能为空'); | ||
| 91 | + } | ||
| 92 | + try { | ||
| 93 | + $route_map = self::where('project_id', $project_id)->where('source_id', $source_id)->where('source', $source)->first(); | ||
| 94 | + if(!$route_map){ | ||
| 95 | + $route_map = new self(); | ||
| 96 | + $route_map->source = $source; | ||
| 97 | + $route_map->source_id = $source_id; | ||
| 98 | + $route_map->project_id = $project_id; | ||
| 99 | + } | ||
| 100 | + $route_map->route = $route; | ||
| 101 | + $route_map->save(); | ||
| 102 | + }catch (\Exception $e){ | ||
| 103 | + throw new \Exception('路由映射失败'); | ||
| 104 | + } | ||
| 105 | + return $route; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * @param $route | ||
| 111 | + * @param $project_id | ||
| 112 | + * @return mixed | ||
| 113 | + * @author zbj | ||
| 114 | + * @date 2023/4/17 | ||
| 115 | + */ | ||
| 116 | + public function getRouteInfo($route, $project_id = 0){ | ||
| 117 | + return self::whereIn('project_id', [0, $project_id])->where('route', $route)->get(); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * @param $source | ||
| 122 | + * @param $source_id | ||
| 123 | + * @param $project_id | ||
| 124 | + * @return mixed | ||
| 125 | + * @author zbj | ||
| 126 | + * @date 2023/4/17 | ||
| 127 | + */ | ||
| 128 | + public static function getRoute($source, $source_id, $project_id = 0){ | ||
| 129 | + if(!$project_id){ | ||
| 130 | + return self::where([ | ||
| 131 | + 'project_id' => 0, | ||
| 132 | + 'source' => $source, | ||
| 133 | + 'source_id' => $source_id | ||
| 134 | + ])->value('route'); | ||
| 135 | + } | ||
| 136 | + return self::whereIn('project_id', [0, $project_id])->where('source', $source)->where('source_id', $source_id)->value('route'); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * @param $route | ||
| 141 | + * @param $source | ||
| 142 | + * @param int $project_id | ||
| 143 | + * @return mixed | ||
| 144 | + * @author zbj | ||
| 145 | + * @date 2023/4/17 | ||
| 146 | + */ | ||
| 147 | + public static function getSourceId($route, $source, $project_id = 0){ | ||
| 148 | + return self::whereIn('project_id', [0, $project_id])->where('source', $source)->where('route', $route)->value('source_id'); | ||
| 149 | + } | ||
| 150 | +} |
| @@ -35,6 +35,12 @@ Route::group([], function () { | @@ -35,6 +35,12 @@ Route::group([], function () { | ||
| 35 | 35 | ||
| 36 | //产品 | 36 | //产品 |
| 37 | Route::prefix('product')->group(function () { | 37 | Route::prefix('product')->group(function () { |
| 38 | + //产品 | ||
| 39 | + Route::get('/', [\App\Http\Controllers\Bside\Product\ProductController::class, 'index'])->name('product'); | ||
| 40 | + Route::get('/info', [\App\Http\Controllers\Bside\Product\ProductController::class, 'info'])->name('product_info'); | ||
| 41 | + Route::post('/save', [\App\Http\Controllers\Bside\Product\ProductController::class, 'save'])->name('product_save'); | ||
| 42 | + Route::any('/delete', [\App\Http\Controllers\Bside\Product\ProductController::class, 'delete'])->name('product_delete'); | ||
| 43 | + | ||
| 38 | //产品分类 | 44 | //产品分类 |
| 39 | Route::get('category', [\App\Http\Controllers\Bside\Product\CategoryController::class, 'index'])->name('product_category'); | 45 | Route::get('category', [\App\Http\Controllers\Bside\Product\CategoryController::class, 'index'])->name('product_category'); |
| 40 | Route::get('category/info', [\App\Http\Controllers\Bside\Product\CategoryController::class, 'info'])->name('product_category_info'); | 46 | Route::get('category/info', [\App\Http\Controllers\Bside\Product\CategoryController::class, 'info'])->name('product_category_info'); |
-
请 注册 或 登录 后发表评论