fix:category

This commit is contained in:
LAPTOP-7SGDREK0\shiweijun
2025-01-09 16:42:10 +08:00
parent 051d19f79f
commit 09d9bfaf7e
5 changed files with 140 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\categoryRequest;
use App\Request\Admin\GoodRequest;
use App\Service\Admin\Good\CategoryService;
use App\Service\Admin\Good\CycleService;
@@ -64,7 +65,7 @@ class GoodController
* @param GoodRequest $request
* @return array
*/
#[RequestMapping(path: "spu", methods: "POST")]
#[RequestMapping(path: "spu", methods: "GET")]
#[Scene(scene: "spu")]
public function spu(GoodRequest $request): array
{
@@ -76,7 +77,7 @@ class GoodController
* @param GoodRequest $request
* @return array
*/
#[RequestMapping(path: "list_spu", methods: "POST")]
#[RequestMapping(path: "list_spu", methods: "GET")]
#[Scene(scene: "list_spu")]
public function spuList(GoodRequest $request): array
{
@@ -124,7 +125,7 @@ class GoodController
* @param GoodRequest $request
* @return array
*/
#[RequestMapping(path: "sku", methods: "POST")]
#[RequestMapping(path: "sku", methods: "GET")]
#[Scene(scene: "sku")]
public function sku(GoodRequest $request): array
{
@@ -136,7 +137,7 @@ class GoodController
* @param GoodRequest $request
* @return array
*/
#[RequestMapping(path: "list_sku", methods: "POST")]
#[RequestMapping(path: "list_sku", methods: "GET")]
#[Scene(scene: "list_sku")]
public function skuList(GoodRequest $request): array
{
@@ -153,12 +154,25 @@ class GoodController
return (new CycleService)->handle();
}
public function categoryList()
/**
* category 列表
* @return array
*/
#[RequestMapping(path: "list_category", methods: "GET")]
#[Scene(scene: "list_category")]
public function categoryList(categoryRequest $request): array
{
return (new CategoryService)->handle();
}
public function categoryAdd()
/**
* category 添加
* @param categoryRequest $request
* @return array
*/
#[RequestMapping(path: "add_category", methods: "POST")]
#[Scene(scene: "add_category")]
public function categoryAdd(categoryRequest $request)
{
return (new CategoryService)->add();
}

37
app/Model/Category.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $name
* @property int $image_id
* @property string $create_time
* @property string $update_time
*/
class Category extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'category';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'image_id' => 'integer'];
const string CREATED_AT = 'create_time';
const string UPDATED_AT = 'update_time';
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Request\Admin;
use Hyperf\Validation\Request\FormRequest;
class categoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name' => 'required|string',
'image_id' => 'required|integer',
];
}
protected array $scenes = [
'list_category' => [],
'add_category' => ['name', 'image_id'],
];
}

View File

@@ -10,17 +10,53 @@ declare(strict_types=1);
namespace App\Service\Admin\Good;
use App\Exception\ErrException;
use App\Model\Category;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Common\OssTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
class CategoryService extends BaseService
{
use OssTrait;
/**
* @var Category $categoryModel
*/
#[Inject]
protected Category $categoryModel;
/**
* @return array
*/
public function handle()
{
$list = $this->categoryModel->get(['category.id','category.name','category.image_id']);
if (empty($list)) return $this->return->success('success',['list' => []]);
return $this->return->success('success',['list' => $list->toArray()]);
}
/**
* 添加
* @return array
* @throws Exception
*/
public function add()
{
$name = $this->request->input('name');
$imageId = $this->request->input('image_id');
$this->updateOssObjects([$imageId]);
$category = new Category();
$category->name = $name;
$category->image_id = $imageId;
if (!$category->save()) throw new ErrException('商品种类添加失败');
return $this->return->success();
}
}

View File

@@ -258,3 +258,15 @@ content-type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
user_id=3&profile=擅长煮煮煮煮&specialties=川菜
### 商品种类列表
GET {{host}}/admin/good/list_category
content-type: application/json
Authorization: Bearer {{admin_token}}
### 部门添加
POST {{host}}/admin/good/add_category
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
name=主食&image_id=4