商品库

This commit is contained in:
2025-12-17 10:40:58 +08:00
parent a02eddb0d0
commit 51b11b3437
13 changed files with 523 additions and 8 deletions
+18
View File
@@ -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());
}
}
/**
* 列表页数据格式化
*
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace app\admin\controller;
use app\common\controller\BaseController;
use app\common\model\PostCategory;
use app\common\service\OssService;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\SkuService;
use support\Request;
class SkuController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->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;
}
}
+18
View File
@@ -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());
}
}
/**
* 获取列表
*
@@ -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());
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace app\common\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\SkuService;
use support\Request;
class SkuController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->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;
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace app\common\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\Sku;
class SkuLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = Sku::class;
}
+6
View File
@@ -51,4 +51,10 @@ class PostFavorite extends Model
{
return $this->hasOne('app\common\model\Post', 'id', 'post_id');
}
protected function serializeDate(\DateTimeInterface $date)
{
// 此方法决定了模型日期字段序列化时的默认格式
return $date->format('Y-m-d H:i:s');
}
}
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace app\common\model;
use support\Model;
class Sku extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'sku';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'code', 'product_name', 'model', 'brand', 'category_id', 'main_image', 'image', 'unit', 'weight', 'size', 'spec_json', 'status', 'sort', 'price', 'desc', 'user_id', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+10
View File
@@ -2,7 +2,9 @@
namespace app\common\service;
use app\common\model\PostCategory;
use app\common\model\PostMedia;
use app\common\model\Sku;
use app\common\model\User;
use app\common\validate\PostValidate;
use app\common\traits\crud\CrudServiceTrait;
@@ -115,6 +117,14 @@ class PostService
redis::set('PostMedia_'.$data['user_id'],$media,30);
unset($data['media']);
$category = PostCategory::where('id',$data['category_id'])->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;
}
+234
View File
@@ -0,0 +1,234 @@
<?php
namespace app\common\service;
use app\common\validate\SkuValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\Sku;
use support\Redis;
use support\Request;
class SkuService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = Sku::class;
/**
* 验证器
*/
protected $validator = SkuValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
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':
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;
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace app\common\validate;
use think\Validate;
class SkuValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
+7 -7
View File
@@ -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();
+15
View File
@@ -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']);
});
});