feat : banner

This commit is contained in:
2025-03-27 15:08:44 +08:00
parent 08397f9ea8
commit 9a4e1dcf48
3 changed files with 221 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Service\Admin\BannerService;
use Exception;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: "admin/banner")]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class BannerController extends AbstractController
{
/**
* banner
* @return array
*/
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]
public function add()
{
return (new BannerService)->add();
}
/**
* banner
* @return array
*/
#[RequestMapping(path: "edit", methods: "POST")]
#[Scene(scene: "edit")]
public function edit()
{
return (new BannerService)->edit();
}
/**
* banner
* @return array
*/
#[RequestMapping(path: "list", methods: "GET")]
#[Scene(scene: "list")]
public function list()
{
return (new BannerService)->handle();
}
/**
* banner
* @return array
* @throws Exception
*/
#[RequestMapping(path: "del", methods: "GET")]
#[Scene(scene: "del")]
public function del(): array
{
return (new BannerService)->del();
}
}

41
app/Model/Banner.php Normal file
View File

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

View File

@@ -0,0 +1,114 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin;
use App\Exception\ErrException;
use App\Model\Banner;
use App\Service\ServiceTrait\Common\OssTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
class BannerService extends BaseService
{
use OssTrait;
/**
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$cityId = (int)$this->request->input('city_id');
$list = $this->bannerModel
->where('city_id',$cityId)
->paginate($limit)
->toArray();
if (empty($list['data'])) return $this->return->success('success',$list);
$imageIds = array_column($list['data'],'image_id');
$imageArr = $this->getOssObjects($imageIds);
foreach ($list['data'] as &$v){
$v['url'] = $imageArr[$v['image_id']]['url'] ?? '';
}
return $this->return->success('success',$list);
}
/**
* @return array
*/
public function add(): array
{
$insertModel = new Banner();
$insertModel->city_id = $this->request->input('city_id');
$insertModel->title = $this->request->input('title');
$insertModel->image_id = $this->request->input('image_id');
$insertModel->sort = $this->request->input('sort');
$insertModel->status = $this->request->input('status');
$insertModel->type = $this->request->input('type');
$insertModel->value = $this->request->input('value');
if (!$insertModel->save()) throw new ErrException('添加错误');
return $this->return->success();
}
/**
* @var Banner
*/
#[Inject]
protected Banner $bannerModel;
/**
* @return array
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$info = $this->bannerModel = Banner::find($id);
if (empty($info)) throw new ErrException('无数据');
$info->title = $this->request->input('title');
$info->image_id = $this->request->input('image_id');
$info->sort = $this->request->input('sort');
$info->status = $this->request->input('status');
$info->type = $this->request->input('type');
$info->value = $this->request->input('value');
if (!$info->save()) throw new ErrException('修改错误');
return $this->return->success();
}
/**
* @return array
* @throws Exception
*/
public function del(): array
{
$id = (int)$this->request->input('id');
$info = $this->bannerModel = Banner::find($id);
if (empty($info)) throw new ErrException('无数据');
if (!$info->delete()) throw new ErrException('删除错误');
return $this->return->success();
}
}