Files
bsh_sample/app/admin/controller/SkuAttributeController.php
T

82 lines
2.4 KiB
PHP
Raw Normal View History

2025-12-08 17:37:34 +08:00
<?php
namespace app\admin\controller;
use app\common\controller\BaseController;
2025-12-11 17:25:10 +08:00
use app\common\model\PostCategory;
2025-12-08 17:37:34 +08:00
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\SkuAttributeService;
2025-12-11 16:11:38 +08:00
use support\Request;
2025-12-08 17:37:34 +08:00
class SkuAttributeController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new SkuAttributeService();
}
2025-12-11 16:11:38 +08:00
/**
* 分类树
* @param Request $request
* @return \support\Response
*/
public function preset(Request $request)
{
try {
$params = $request->all();
$result = $this->service->preset($params);
foreach ($result as $k => $r){
unset($r['created_at']);
unset($r['updated_at']);
$result[$k] = $this->indexFormat($r);
}
return $this->success($result, '获取分类下拉');
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
2025-12-08 17:37:34 +08:00
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
2025-12-11 16:11:38 +08:00
protected function indexFormat($data)
{
if (isset($data['created_at']))
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
if (isset($data['updated_at']))
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
2025-12-11 17:25:10 +08:00
if (isset($data['category_id']))
$data['category_name'] = PostCategory::where('id', $data['category_id'])->value('name');
2025-12-11 16:11:38 +08:00
if (isset($data['options_json'])){
$data['options_json'] = json_decode($data['options_json'], true);
2025-12-11 16:26:00 +08:00
foreach ($data['options_json'] as $k => $v){
$data['options_json'][$k] = ['value' => $v];
}
2025-12-11 16:11:38 +08:00
}
return $data;
}
/**
* 创建
*
* @param Request $request 请求对象
* @return mixed 返回创建成功的数据或信息
*/
public function store(Request $request)
{
try {
$data = $this->validate($this->service->getValidator(), $request->post(), 'store');
$res = $this->service->serviceStore($request, $data);
return $this->success($res, '创建成功');
} catch (\Throwable $e) {
return $this->error($e->getMessage().$e->getFile().$e->getLine());
}
}
2025-12-08 17:37:34 +08:00
}