147 lines
4.2 KiB
PHP
147 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace app\console\controller;
|
|
|
|
use support\Request;
|
|
use support\Response;
|
|
use app\console\model\BrandModel;
|
|
use app\console\service\BrandService;
|
|
use app\console\validate\BrandValidator;
|
|
use app\mzl\common\controller\BaseController;
|
|
|
|
class BrandController extends BaseController
|
|
{
|
|
|
|
/**
|
|
* 不需要验证token的接口
|
|
* @var array
|
|
*/
|
|
protected $noNeedJwt = [];
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected mixed $validator = BrandValidator::class;
|
|
|
|
/**
|
|
* 返回业务 Service 实例
|
|
*/
|
|
protected function getService()
|
|
{
|
|
return new BrandService(new BrandModel());
|
|
}
|
|
|
|
/**
|
|
* 额外字段处理 (根据场景处理)
|
|
* @param array $item
|
|
* @param string $scene
|
|
* @return array
|
|
*/
|
|
protected function postProcessItem(array $item, string $scene): array
|
|
{
|
|
|
|
if ($scene === 'default') {
|
|
$result = [
|
|
'id' => $item['id'],
|
|
'status' => $item['status'],
|
|
'sort' => $item['sort'],
|
|
'toppic' => $item['toppic'],
|
|
'descpic' => $item['descpic'],
|
|
'logo' => $item['logo'],
|
|
'name' => isset($item['translation']) ? $item['translation']['name'] : '',
|
|
'desc' => isset($item['translation']) ? $item['translation']['desc'] : '',
|
|
'toppic_alt' => isset($item['translation']) ? $item['translation']['toppic_alt'] : '',
|
|
'descpic_alt' => isset($item['translation']) ? $item['translation']['descpic_alt'] : '',
|
|
'logo_alt' => isset($item['translation']) ? $item['translation']['logo_alt'] : '',
|
|
'created_at' => date('Y-m-d H:i:s', $item['created_at']),
|
|
'updated_at' => date('Y-m-d H:i:s', $item['updated_at']),
|
|
];
|
|
foreach ($item['banner'] as $k => $v) {
|
|
$result['banner'][$k] = $v;
|
|
}
|
|
foreach ($item['trait'] as $k => $v) {
|
|
$result['trait'][$k] = $v;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
if ($scene === 'preset') {
|
|
return [
|
|
'id' => $item['id'],
|
|
'name' => isset($item['translation']) ? $item['translation']['name'] : ''
|
|
];
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
/**
|
|
* 参数预处理 (根据场景处理)
|
|
* @param Request $request
|
|
* @param string $scene
|
|
* @return array
|
|
*/
|
|
protected function prepareParams(Request $request, string $scene): array
|
|
{
|
|
$params = parent::prepareParams($request, $scene);
|
|
|
|
if ($scene === 'default') {
|
|
|
|
}
|
|
|
|
return $params;
|
|
}
|
|
|
|
/**
|
|
* 分页
|
|
*
|
|
* @param Request $request 请求对象
|
|
* @return Response 响应对象
|
|
*/
|
|
public function index(Request $request, string $scene = 'default'): Response
|
|
{
|
|
// $Brand = BrandModel::with('banner')->with('trait')->where('id',314)->first()->toArray();
|
|
// var_dump($Brand);
|
|
// return json($Brand);
|
|
try {
|
|
return parent::index($request, $scene);
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 预设
|
|
*/
|
|
public function presets(Request $request, string $scene = 'preset'): Response
|
|
{
|
|
try {
|
|
return parent::all($request, $scene);
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function add(Request $request): Response
|
|
{
|
|
try {
|
|
$id = $this->getService()->add($request->all());
|
|
return $id ? $this->successResponse(['id' => $id])
|
|
: $this->errorResponse();
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function edit(Request $request, int $id): Response
|
|
{
|
|
try {
|
|
$request->setPost('id',$id);
|
|
return $this->getService()->edit($request->all(), $id)
|
|
? $this->successResponse()
|
|
: $this->errorResponse();
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage().$th->getFile().$th->getLine());
|
|
}
|
|
}
|
|
} |