diff --git a/app/admin/controller/SkuController.php b/app/admin/controller/SkuController.php index 73e3f09..1815c9a 100644 --- a/app/admin/controller/SkuController.php +++ b/app/admin/controller/SkuController.php @@ -4,6 +4,7 @@ namespace app\admin\controller; use app\common\controller\BaseController; use app\common\traits\crud\CrudControllerTrait; use app\common\service\SkuService; +use support\Request; class SkuController extends BaseController { @@ -16,18 +17,35 @@ class SkuController extends BaseController $this->service = new SkuService(); } + /** + * 创建 + * + * @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()); + } + } + /** * 列表页数据格式化 * * @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']); - // return $data; - // } + 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']); + return $data; + } } \ No newline at end of file diff --git a/app/common/model/SkuAttribute.php b/app/common/model/SkuAttribute.php index 5120c6a..568cb0f 100644 --- a/app/common/model/SkuAttribute.php +++ b/app/common/model/SkuAttribute.php @@ -45,5 +45,5 @@ class SkuAttribute extends Model /** * 不允许自动填充模型字段 */ - protected $guarded = []; + protected $guarded = ['updated_at']; } \ No newline at end of file diff --git a/app/common/service/SkuAttributeService.php b/app/common/service/SkuAttributeService.php index 4b1a5bf..bd9a3ab 100644 --- a/app/common/service/SkuAttributeService.php +++ b/app/common/service/SkuAttributeService.php @@ -5,6 +5,7 @@ namespace app\common\service; use app\common\validate\SkuAttributeValidate; use app\common\traits\crud\CrudServiceTrait; use app\common\model\SkuAttribute; +use support\Request; class SkuAttributeService { @@ -28,4 +29,33 @@ class SkuAttributeService { return new $this->validator; } + + /** + * 构造 CrudUtil 查询配置 + */ + protected function buildQueryConfig(Request $request, string $scene = 'default'): array + { + $p = $request->all(); + + $config = []; + $config['model'] = $this->modelClass; + switch ($scene) { + case 'default': + case 'index': + $config['order']['sort'] = 'desc'; + $config['order']['id'] = 'desc'; + break; + case 'show': + $config['where']['id'] = $p['id']; + break; + case 'store': + break; + case 'update': + case 'destroy': + $config['where']['id'] = $p['id']; + break; + } + + return $config; + } } \ No newline at end of file diff --git a/app/common/service/SkuService.php b/app/common/service/SkuService.php index 9941dbc..a343c16 100644 --- a/app/common/service/SkuService.php +++ b/app/common/service/SkuService.php @@ -2,9 +2,12 @@ namespace app\common\service; +use app\common\model\SkuAttributeValue; use app\common\validate\SkuValidate; use app\common\traits\crud\CrudServiceTrait; use app\common\model\Sku; +use support\Redis; +use support\Request; class SkuService { @@ -28,4 +31,85 @@ class SkuService { return new $this->validator; } + + + /** + * 添加前置处理 + */ + public function beforeServiceStore($data, Request $request) + { + $spec = []; + foreach ($data['spec'] as $key => $value) { + $specItem = []; + $specItem['category_id'] = $data['category_id']; + $specItem['attr_id'] = $value['attr_id']; + $specItem['attr_name'] = $value['attr_name']; + $specItem['attr_value'] = $value['attr_value']; + $spec[] = $specItem; + } + $spec_json = json_encode($spec,JSON_UNESCAPED_UNICODE); + Redis::set('sku_attr_'.$request->user->uid, $spec_json,30); + + unset($data['spec']); + $data['spec_json'] = $spec_json; + $data['user_id'] = $request->user->uid; + $data['code'] = $this->generateUniqueCode(); + return $data; + } + + + /** + * 添加后置处理 + */ + public function afterServiceStore($res) + { + $specs = Redis::get('sku_attr_'.$res['user_id']); + $specs= json_decode($specs,true); + var_dump($specs); + $spec = []; + foreach ($specs as $key => $value) { + $spec['category_id'] = $res['category_id']; + $spec['sku_id'] = $res['id']; + $spec['attr_id'] = $value['attr_id']; + $spec['attr_name'] = $value['attr_name']; + $spec['attr_value'] = $value['attr_value']; + SkuAttributeValue::create($spec); + } + return $res; + } + + + + /** + * 生成唯一的7位颜色编码 + * 格式:C + (O/I) + 5位字母数字组合 + * + * @param int $type 类型:2=I,其他=O + * @return string 7位唯一编码 + */ + public function generateUniqueCode() + { + $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $maxAttempts = 10; // 防止无限循环 + + for ($attempt = 0; $attempt < $maxAttempts; $attempt++) { + // 生成后5位随机字符 + $suffix = ''; + for ($i = 0; $i < 5; $i++) { + $suffix .= $chars[rand(0, strlen($chars) - 1)]; + } + + $colorCode = 'PN' . $suffix; + + // 检查是否已存在 + $existing = Sku::where('code', $colorCode)->first(); + if (!$existing) { + return $colorCode; + } + } + + // 如果1000次尝试后仍然重复,使用时间戳确保唯一性 + $timestamp = substr(time(), -5); + return 'PN' . $timestamp; + } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index b846ddc..85813d6 100644 --- a/config/route.php +++ b/config/route.php @@ -183,6 +183,24 @@ Route::group('/admin', function () { }); }); + // sku属性 + Route::group('/sku/attribute', function () { + Route::any('/list', [app\admin\controller\SkuAttributeController::class, 'index']); + Route::any('/show', [app\admin\controller\SkuAttributeController::class, 'show']); + Route::any('/store', [app\admin\controller\SkuAttributeController::class, 'store']); + Route::any('/update', [app\admin\controller\SkuAttributeController::class, 'update']); + Route::any('/destroy', [app\admin\controller\SkuAttributeController::class, 'destroy']); + }); + + // sku + Route::group('/sku', function () { + Route::any('/list', [app\admin\controller\SkuController::class, 'index']); + Route::any('/show', [app\admin\controller\SkuController::class, 'show']); + Route::any('/store', [app\admin\controller\SkuController::class, 'store']); + Route::any('/update', [app\admin\controller\SkuController::class, 'update']); + Route::any('/destroy', [app\admin\controller\SkuController::class, 'destroy']); + }); + // // 分类下内容 // Route::group('/nav/item', function () { // Route::any('/list', [app\admin\controller\NavItemsController::class, 'index']);