From 51b11b3437d4f29670769c7ce299a14c548a0560 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 17 Dec 2025 10:40:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/PostController.php | 18 ++ app/admin/controller/SkuController.php | 60 +++++ app/common/controller/PostController.php | 18 ++ .../controller/PostFavoriteController.php | 2 +- app/common/controller/SkuController.php | 59 +++++ app/common/logic/SkuLogic.php | 17 ++ app/common/model/PostFavorite.php | 6 + app/common/model/Sku.php | 49 ++++ app/common/service/PostService.php | 10 + app/common/service/SkuService.php | 234 ++++++++++++++++++ app/common/validate/SkuValidate.php | 29 +++ app/controller/IndexController.php | 14 +- config/route.php | 15 ++ 13 files changed, 523 insertions(+), 8 deletions(-) create mode 100644 app/admin/controller/SkuController.php create mode 100644 app/common/controller/SkuController.php create mode 100644 app/common/logic/SkuLogic.php create mode 100644 app/common/model/Sku.php create mode 100644 app/common/service/SkuService.php create mode 100644 app/common/validate/SkuValidate.php diff --git a/app/admin/controller/PostController.php b/app/admin/controller/PostController.php index 9c0cd46..4ba1eaa 100644 --- a/app/admin/controller/PostController.php +++ b/app/admin/controller/PostController.php @@ -8,6 +8,7 @@ use app\common\model\User; use app\common\service\OssService; use app\common\traits\crud\CrudControllerTrait; use app\common\service\PostService; +use support\Request; class PostController extends BaseController { @@ -20,6 +21,23 @@ class PostController extends BaseController $this->service = new PostService(); } + /** + * 创建 + * + * @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->getLine().$e->getFile()); + } + } + /** * 列表页数据格式化 * diff --git a/app/admin/controller/SkuController.php b/app/admin/controller/SkuController.php new file mode 100644 index 0000000..6c16339 --- /dev/null +++ b/app/admin/controller/SkuController.php @@ -0,0 +1,60 @@ +service = new SkuService(); + } + 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->getLine().$e->getFile()); + } + } + /** + * 列表页数据格式化 + * + * @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['spec_json'])){ + $data['spec'] = json_decode($data['spec_json'], true); + } + if (isset($data['category_id'])) + $data['category_name'] = PostCategory::where('id', $data['category_id'])->value('name'); + $ossservice = new OssService(); + if (!empty($data['image'])){ + $data['image'] = explode(',',$data['image']); + foreach ($data['image'] as $key => $image){ + $data['image_pull'][$key] = $ossservice->generatePrivateUrl($image); + } + } + if (!empty($data['main_image'])){ + $data['main_image_pull'] = $ossservice->generatePrivateUrl($data['main_image']); + } + return $data; + } +} \ No newline at end of file diff --git a/app/common/controller/PostController.php b/app/common/controller/PostController.php index dfadd96..062325b 100644 --- a/app/common/controller/PostController.php +++ b/app/common/controller/PostController.php @@ -27,6 +27,24 @@ class PostController extends BaseController { $this->service = new PostService(); } + + /** + * 创建 + * + * @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->getLine().$e->getFile()); + } + } + /** * 获取列表 * diff --git a/app/common/controller/PostFavoriteController.php b/app/common/controller/PostFavoriteController.php index 80055d6..4692f41 100644 --- a/app/common/controller/PostFavoriteController.php +++ b/app/common/controller/PostFavoriteController.php @@ -43,7 +43,7 @@ class PostFavoriteController extends BaseController $raw['records'] = $data; return $this->success($raw); } catch (\Throwable $e) { - return $this->error($e->getMessage()); + return $this->error($e->getMessage().$e->getFile().$e->getLine()); } } diff --git a/app/common/controller/SkuController.php b/app/common/controller/SkuController.php new file mode 100644 index 0000000..7e44d5a --- /dev/null +++ b/app/common/controller/SkuController.php @@ -0,0 +1,59 @@ +service = new SkuService(); + } + + + /** + * 获取列表 + * + * @param Request $request 请求对象 + * @return mixed 返回分页数据及总数 + */ + public function preset(Request $request) + { + try { + $raw = $this->service->serviceListAll($request); + $data = []; + foreach ($raw as $item) { + unset($item['created_at']); + unset($item['updated_at']); + $data[] = $this->indexFormat($item); + } + return $this->success($data); + } 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['spec_json'])) + $data['spec_json'] = json_decode($data['spec_json'],true); + return $data; + } +} \ No newline at end of file diff --git a/app/common/logic/SkuLogic.php b/app/common/logic/SkuLogic.php new file mode 100644 index 0000000..64bfe3c --- /dev/null +++ b/app/common/logic/SkuLogic.php @@ -0,0 +1,17 @@ +hasOne('app\common\model\Post', 'id', 'post_id'); } + + protected function serializeDate(\DateTimeInterface $date) + { + // 此方法决定了模型日期字段序列化时的默认格式 + return $date->format('Y-m-d H:i:s'); + } } \ No newline at end of file diff --git a/app/common/model/Sku.php b/app/common/model/Sku.php new file mode 100644 index 0000000..91cf572 --- /dev/null +++ b/app/common/model/Sku.php @@ -0,0 +1,49 @@ +first(); + foreach ($data['model'] as $key => $model) { + $code = (new SkuService)->generateUniqueCode(); + Sku::firstOrCreate(['model' => $model], ['category_id' => $data['category_id'], + 'product_name' => $category['name'],'code' => $code]); + } + $data['model'] = implode(',',$data['model']); + return $data; } diff --git a/app/common/service/SkuService.php b/app/common/service/SkuService.php new file mode 100644 index 0000000..ee15671 --- /dev/null +++ b/app/common/service/SkuService.php @@ -0,0 +1,234 @@ +validator; + } + + /** + * 构造 CrudUtil 查询配置 + */ + protected function buildQueryConfig(Request $request, string $scene = 'default'): array + { + $p = $request->all(); + + $config = []; + $config['model'] = $this->modelClass; + switch ($scene) { + case 'default': + if (isset($p['keyword']) && $p['keyword']) { + $k = '%' . $p['keyword'] . '%'; + foreach (['model','content'] as $col) { + $config['orWhere'][] = [$col, 'like', $k]; + } + } + $config['where']['status'] = 1; + $config['order']['sort'] = 'desc'; + $config['order']['id'] = 'desc'; + break; + case 'index': + $config['where']['status'] = 1; + $config['order']['sort'] = 'desc'; + $config['order']['id'] = 'desc'; + break; + case 'show': + $config['where']['id'] = $p['id']; + $config['where']['status'] = 1; + break; + case 'store': + break; + case 'update': + $config['where']['id'] = $p['id']; + break; + case 'destroy': + $config['where']['id'] = $p['id']; + break; + } + + return $config; + } + + /** + * 添加前置处理 + */ + public function beforeServiceStore($data, Request $request) + { + $protocol = $request->header('x-forwarded-proto', + isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'); + $domain = $protocol . '://' . $request->host(); + +// $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(); + $data['image'] = implode(',',$data['image']); + + // 将OSS直连地址替换为代理地址 +// $ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/'; +// $proxyPath = '/proxy/ossurl?file='; +// +// $data['desc'] = str_replace( +// $ossDomain, +// $proxyPath, +// $data['desc'] +// ); + // 将OSS直连地址替换为代理地址 + $ossDomain = 'https://static.hnzhwlkj.cn/'; + $proxyPath = $domain.'/proxy/ossurl?file='; + $data['desc'] = str_replace( + $ossDomain, + $proxyPath, + $data['desc'] + ); + $data['desc'] = str_replace( + '?Expires=', + '&Expires=', + $data['desc'] + ); + + return $data; + } + + + /** + * 添加后置处理 + */ + public function afterServiceStore($res) + { +// $specs = Redis::get('sku_attr_'.$res['user_id']); +// $specs= json_decode($specs,true); +// $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']; +// } + return $res; + } + + /** + * 添加前置处理 + */ + public function beforeServiceUpdate($data, Request $request) + { + $protocol = $request->header('x-forwarded-proto', + isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'); + $domain = $protocol . '://' . $request->host(); + +// $spec = []; +// foreach ($data['spec'] as $key => $value) { +// $specItem = []; +// $specItem['category_id'] = $data['category_id']; +// $specItem['sku_id'] = $data['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); + + unset($data['spec']); +// $data['spec_json'] = $spec_json; + $data['user_id'] = $request->user->uid; + $data['image'] = implode(',',$data['image']); + + // 将OSS直连地址替换为代理地址 +// $ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/'; +// $proxyPath = '/proxy/ossurl?file='; +// $data['desc'] = str_replace( +// $ossDomain, +// $proxyPath, +// $data['desc'] +// ); + // 将OSS直连地址替换为代理地址 + $ossDomain = 'https://static.hnzhwlkj.cn/'; + $proxyPath = $domain.'/proxy/ossurl?file='; + $data['desc'] = str_replace( + $ossDomain, + $proxyPath, + $data['desc'] + ); + + $data['desc'] = str_replace( + '?Expires=', + '&Expires=', + $data['desc'] + ); + + return $data; + } + + + /** + * 生成唯一的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/app/common/validate/SkuValidate.php b/app/common/validate/SkuValidate.php new file mode 100644 index 0000000..013357c --- /dev/null +++ b/app/common/validate/SkuValidate.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index b71dae0..e1e69f3 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -26,26 +26,26 @@ class IndexController { $options = [ 'controller' => [ - 'module' => 'admin', - 'name' => 'message_notice' + 'module' => 'common', + 'name' => 'sku' ], 'model' => [ 'module' => 'common', - 'name' => 'message_notice' + 'name' => 'sku' ], 'service' => [ 'module' => 'common', - 'name' => 'message_notice' + 'name' => 'sku' ], 'logic' => [ 'module' => 'common', - 'name' => 'message_notice' + 'name' => 'sku' ], 'validate' => [ 'module' => 'common', - 'name' => 'message_notice' + 'name' => 'sku' ], - 'table' => 'message_notice' + 'table' => 'sku' ]; $result = (new Generate($options))->doGenerate(); diff --git a/config/route.php b/config/route.php index eb5a6d7..0ef5f2b 100644 --- a/config/route.php +++ b/config/route.php @@ -100,6 +100,12 @@ Route::group('/common', function () { Route::group('/banner', function () { Route::any('/list', [app\common\controller\BannerController::class, 'getAll']); }); + + // sku + Route::group('/sku', function () { + Route::any('/list', [app\common\controller\SkuController::class, 'index']); + Route::any('/preset', [app\common\controller\SkuController::class, 'preset']); + }); }); @@ -211,6 +217,15 @@ Route::group('/admin', function () { Route::any('/update', [app\admin\controller\AgreementController::class, 'update']); Route::any('/destroy', [app\admin\controller\AgreementController::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']); + }); });