feat : banner
This commit is contained in:
114
app/Service/Admin/BannerService.php
Normal file
114
app/Service/Admin/BannerService.php
Normal 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();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user