79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
namespace app\admin\controller;
|
|
|
|
use app\common\controller\BaseController;
|
|
use app\common\traits\crud\CrudControllerTrait;
|
|
use app\common\service\SkuAttributeService;
|
|
use support\Request;
|
|
|
|
class SkuAttributeController extends BaseController
|
|
{
|
|
use CrudControllerTrait;
|
|
|
|
protected $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new SkuAttributeService();
|
|
}
|
|
|
|
|
|
/**
|
|
* 分类树
|
|
* @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());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
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']);
|
|
if (isset($data['options_json'])){
|
|
$data['options_json'] = json_decode($data['options_json'], true);
|
|
// foreach ($data['options_json'] as $k => $v){
|
|
// $data['options_json'][$k] = ['value' => $v];
|
|
// }
|
|
}
|
|
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());
|
|
}
|
|
}
|
|
} |