作者 Your Name

gx

  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 +}
  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;
  4 +
  5 +use Illuminate\Foundation\Http\FormRequest;
  6 +
  7 +class ProjectRoleRequest extends FormRequest
  8 +{
  9 + /**
  10 + * Determine if the user is authorized to make this request.
  11 + *
  12 + * @return bool
  13 + */
  14 + public function authorize()
  15 + {
  16 + return true;
  17 + }
  18 +
  19 + public function rules()
  20 + {
  21 + return [
  22 + 'name'=>'required|max:11',
  23 + 'role_menu'=>'required|string',
  24 + ];
  25 + }
  26 +
  27 + public function messages()
  28 + {
  29 + return [
  30 + 'name.required'=>'名称必须填写',
  31 + 'name.max' => '名称不大于11字符.',
  32 + 'role_menu.required'=>'角色列表必须填写',
  33 + ];
  34 + }
  35 +}
  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 +}