正在显示
7 个修改的文件
包含
108 行增加
和
10 行删除
| @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Bside\Product; | @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Bside\Product; | ||
| 5 | use App\Http\Controllers\Bside\BaseController; | 5 | use App\Http\Controllers\Bside\BaseController; |
| 6 | use App\Http\Logic\Bside\Product\CategoryLogic; | 6 | use App\Http\Logic\Bside\Product\CategoryLogic; |
| 7 | use App\Http\Requests\Bside\product\CategoryRequest; | 7 | use App\Http\Requests\Bside\product\CategoryRequest; |
| 8 | +use App\Rules\Ids; | ||
| 8 | use Illuminate\Http\Request; | 9 | use Illuminate\Http\Request; |
| 9 | 10 | ||
| 10 | /** | 11 | /** |
| @@ -46,7 +47,7 @@ class CategoryController extends BaseController | @@ -46,7 +47,7 @@ class CategoryController extends BaseController | ||
| 46 | public function delete(Request $request, CategoryLogic $logic) | 47 | public function delete(Request $request, CategoryLogic $logic) |
| 47 | { | 48 | { |
| 48 | $request->validate([ | 49 | $request->validate([ |
| 49 | - 'ids'=>'required' | 50 | + 'ids'=>['required', new Ids()] |
| 50 | ],[ | 51 | ],[ |
| 51 | 'ids.required' => 'ID不能为空' | 52 | 'ids.required' => 'ID不能为空' |
| 52 | ]); | 53 | ]); |
| @@ -68,7 +68,7 @@ class BaseLogic | @@ -68,7 +68,7 @@ class BaseLogic | ||
| 68 | 68 | ||
| 69 | // 数据分页设置 | 69 | // 数据分页设置 |
| 70 | if ($limit) { | 70 | if ($limit) { |
| 71 | - $result = $query->simplePaginate(); | 71 | + $result = $query->paginate($limit); |
| 72 | }else{ | 72 | }else{ |
| 73 | $result = $query->get(); | 73 | $result = $query->get(); |
| 74 | } | 74 | } |
| @@ -80,7 +80,7 @@ class BaseLogic | @@ -80,7 +80,7 @@ class BaseLogic | ||
| 80 | /** | 80 | /** |
| 81 | * 详情 | 81 | * 详情 |
| 82 | * @param $id | 82 | * @param $id |
| 83 | - * @return | 83 | + * @return array |
| 84 | * @author zbj | 84 | * @author zbj |
| 85 | * @date 2023/4/13 | 85 | * @date 2023/4/13 |
| 86 | */ | 86 | */ |
| @@ -169,13 +169,14 @@ class BaseLogic | @@ -169,13 +169,14 @@ class BaseLogic | ||
| 169 | /** | 169 | /** |
| 170 | * 格式化查询条件 | 170 | * 格式化查询条件 |
| 171 | * @param $map | 171 | * @param $map |
| 172 | + * @param $query | ||
| 172 | * @return mixed | 173 | * @return mixed |
| 173 | * @author zbj | 174 | * @author zbj |
| 174 | * @date 2023/4/13 | 175 | * @date 2023/4/13 |
| 175 | */ | 176 | */ |
| 176 | - public function formatQuery($map, $model = '') | 177 | + public function formatQuery($map, $query = '') |
| 177 | { | 178 | { |
| 178 | - $model = $model ?: $this->model; | 179 | + $model = $query ?: $this->model; |
| 179 | $query = $model->where(function ($query) use ($map) { | 180 | $query = $model->where(function ($query) use ($map) { |
| 180 | foreach ($map as $v) { | 181 | foreach ($map as $v) { |
| 181 | if ($v instanceof \Closure) { | 182 | if ($v instanceof \Closure) { |
| @@ -2,9 +2,16 @@ | @@ -2,9 +2,16 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Http\Logic\Bside\Product; | 3 | namespace App\Http\Logic\Bside\Product; |
| 4 | 4 | ||
| 5 | +use App\Helper\Arrays; | ||
| 5 | use App\Http\Logic\Bside\BaseLogic; | 6 | use App\Http\Logic\Bside\BaseLogic; |
| 6 | use App\Models\Product\Category; | 7 | use App\Models\Product\Category; |
| 7 | 8 | ||
| 9 | +/** | ||
| 10 | + * Class CategoryLogic | ||
| 11 | + * @package App\Http\Logic\Bside\Product | ||
| 12 | + * @author zbj | ||
| 13 | + * @date 2023/4/14 | ||
| 14 | + */ | ||
| 8 | class CategoryLogic extends BaseLogic | 15 | class CategoryLogic extends BaseLogic |
| 9 | { | 16 | { |
| 10 | public function __construct() | 17 | public function __construct() |
| @@ -13,4 +20,34 @@ class CategoryLogic extends BaseLogic | @@ -13,4 +20,34 @@ class CategoryLogic extends BaseLogic | ||
| 13 | 20 | ||
| 14 | $this->model = new Category(); | 21 | $this->model = new Category(); |
| 15 | } | 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 = Category::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(Arrays::splitFilterToArray($ids), 'intval'); | ||
| 39 | + foreach ($ids as $id){ | ||
| 40 | + $info = $this->getInfo($id); | ||
| 41 | + if(!$info){ | ||
| 42 | + continue; | ||
| 43 | + } | ||
| 44 | + //是否有子分类 | ||
| 45 | + if(Category::where('pid', $id)->count()){ | ||
| 46 | + $this->fail("分类{$info['title']}存在子分类,不能删除"); | ||
| 47 | + } | ||
| 48 | + //todo 是否有对应商品 | ||
| 49 | + | ||
| 50 | + return parent::delete($ids); | ||
| 51 | + } | ||
| 52 | + } | ||
| 16 | } | 53 | } |
| @@ -2,11 +2,7 @@ | @@ -2,11 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Http\Requests\Bside\product; | 3 | namespace App\Http\Requests\Bside\product; |
| 4 | 4 | ||
| 5 | -use App\Enums\Common\Demo; | ||
| 6 | -use BenSampo\Enum\Rules\EnumValue; | ||
| 7 | -use Illuminate\Contracts\Validation\Validator; | ||
| 8 | use Illuminate\Foundation\Http\FormRequest; | 5 | use Illuminate\Foundation\Http\FormRequest; |
| 9 | -use Illuminate\Validation\ValidationException; | ||
| 10 | 6 | ||
| 11 | /** | 7 | /** |
| 12 | * Class CategoryRequest | 8 | * Class CategoryRequest |
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Providers; | 3 | namespace App\Providers; |
| 4 | 4 | ||
| 5 | +use App\Services\PaginatorServer; | ||
| 5 | use Illuminate\Support\ServiceProvider; | 6 | use Illuminate\Support\ServiceProvider; |
| 6 | 7 | ||
| 7 | class AppServiceProvider extends ServiceProvider | 8 | class AppServiceProvider extends ServiceProvider |
| @@ -13,7 +14,10 @@ class AppServiceProvider extends ServiceProvider | @@ -13,7 +14,10 @@ class AppServiceProvider extends ServiceProvider | ||
| 13 | */ | 14 | */ |
| 14 | public function register() | 15 | public function register() |
| 15 | { | 16 | { |
| 16 | - // | 17 | + //自定义分页 |
| 18 | + $this->app->bind('Illuminate\Pagination\LengthAwarePaginator',function ($app,$options){ | ||
| 19 | + return new PaginatorServer($options['items'], $options['total'], $options['perPage'], $options['currentPage'] , $options['options']); | ||
| 20 | + }); | ||
| 17 | } | 21 | } |
| 18 | 22 | ||
| 19 | /** | 23 | /** |
app/Rules/Ids.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Rules; | ||
| 4 | + | ||
| 5 | +use App\Helper\Arrays; | ||
| 6 | +use Illuminate\Contracts\Validation\Rule; | ||
| 7 | + | ||
| 8 | +class Ids implements Rule | ||
| 9 | +{ | ||
| 10 | + | ||
| 11 | + /** | ||
| 12 | + * Determine if the validation rule passes. | ||
| 13 | + * | ||
| 14 | + * @param string $attribute | ||
| 15 | + * @param mixed $value | ||
| 16 | + * @return bool | ||
| 17 | + */ | ||
| 18 | + public function passes($attribute, $value) | ||
| 19 | + { | ||
| 20 | + $ids = array_filter(Arrays::splitFilterToArray($value), 'intval'); | ||
| 21 | + return boolval($ids); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * Get the validation error message. | ||
| 26 | + * | ||
| 27 | + * @return string | ||
| 28 | + */ | ||
| 29 | + public function message() | ||
| 30 | + { | ||
| 31 | + return 'ID不能为空'; | ||
| 32 | + } | ||
| 33 | +} |
app/Services/PaginatorServer.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Services; | ||
| 4 | + | ||
| 5 | +use Illuminate\Pagination\LengthAwarePaginator; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 自定义Paginate的分页参数 | ||
| 9 | + * Class PaginatorServer | ||
| 10 | + * @package App\Services | ||
| 11 | + * @author zbj | ||
| 12 | + * @date 2023/4/14 | ||
| 13 | + */ | ||
| 14 | +class PaginatorServer extends LengthAwarePaginator | ||
| 15 | +{ | ||
| 16 | + public function toArray() | ||
| 17 | + { | ||
| 18 | + return [ | ||
| 19 | + 'list' => $this->items->toArray(), | ||
| 20 | + 'total' => $this->total(), | ||
| 21 | + 'page' => $this->currentPage(), | ||
| 22 | + 'total_page' => $this->lastPage(), | ||
| 23 | + 'size' => $this->perPage(), | ||
| 24 | + ]; | ||
| 25 | + } | ||
| 26 | +} |
-
请 注册 或 登录 后发表评论