feat: 添加产品购买链接添加接口

This commit is contained in:
2025-03-26 18:08:26 +08:00
committed by jsasg
parent 24f2779cad
commit 4b27779977
3 changed files with 65 additions and 12 deletions

View File

@@ -261,6 +261,34 @@ class ProductPurchaseLink
->select();
}
/**
* 添加购买链接
*/
public function save()
{
$post = request()->post([
'link',
'product_id',
'platform_id'
]);
$data = [
'link' => $post['link'],
'language_id' => request()->lang_id,
'product_id' => $post['product_id'],
'platform_id' => $post['platform_id']
];
$validate = new ProductPurchaseLinkValidate;
if (!$validate->scene('add')->check($data)) {
return error($validate->getError());
}
$link = ProductPurchaseLinkModel::create($data);
if ($link->isEmpty()) {
return error('操作失败');
}
return success('操作成功');
}
/**
* 更新购买链接
*/
@@ -279,7 +307,7 @@ class ProductPurchaseLink
'platform_id' => $put['platform_id']
];
$validate = new ProductPurchaseLinkValidate;
if (!$validate->scene('update')->check($data)) {
if (!$validate->scene('edit')->check($data)) {
return error($validate->getError());
}

View File

@@ -323,6 +323,9 @@ Route::group('v1', function () {
// 购买链接导出
Route::get('export', 'ProductPurchaseLink/export');
// 购买链接添加
Route::post('save', 'ProductPurchaseLink/save');
// 购买链接更新
Route::put('update/:id', 'ProductPurchaseLink/update');
});

View File

@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace app\admin\validate\v1;
use app\admin\controller\v1\ProductPurchaseLink;
use app\admin\model\v1\ProductPurchaseLinkModel;
use think\Validate;
@@ -19,6 +18,7 @@ class ProductPurchaseLinkValidate extends Validate
protected $rule = [
'id' => 'require|integer',
'link' => 'url|max:255',
'product_id' => 'require|integer',
'platform_id' => 'sameSpuPlatformCheck|integer'
];
@@ -33,6 +33,8 @@ class ProductPurchaseLinkValidate extends Validate
'id.integer' => 'id字段类型错误',
'link.url' => '链接格式不正确',
'link.max' => '链接不能超过255个字符',
'product_id.require' => '产品id不能为空',
'product_id.integer' => '产品id类型错误',
'platform_id.sameSpuPlatformCheck' => '同型号平台不能重复',
'platform_id.integer' => '平台id类型错误'
@@ -43,16 +45,28 @@ class ProductPurchaseLinkValidate extends Validate
*/
protected function sameSpuPlatformCheck($value, $rule, $data = [])
{
$product = 0;
if (!empty($data['id'])) {
$sql = ProductPurchaseLinkModel::alias('s')
->field(['s.product_id'])
->where('s.id', '=', $data['id'])
->limit(1)
->buildSql();
$product = \think\facade\Db::raw($sql);
} else {
if (empty($data['product_id'])) {
return false;
}
$product = $data['product_id'];
}
$platforms = ProductPurchaseLinkModel::alias('p')
->where('p.product_id', '=', \think\facade\Db::raw($sql))
->where('p.id', '<>', $data['id'])
->where('p.language_id', '=', $data['language_id'])
->where(function ($query) use ($data, $product) {
$query->where('p.product_id', '=', $product)->where('p.language_id', '=', $data['language_id']);
if (!empty($data['id'])) {
$query->where('p.id', '<>', $data['id']);
}
})
->column(['p.platform_id']);
if (!empty($platforms) && in_array($value, $platforms)) {
return false;
@@ -61,11 +75,19 @@ class ProductPurchaseLinkValidate extends Validate
return true;
}
/**
* 添加场景
*/
public function sceneAdd()
{
return $this->only(['link', 'product_id', 'platform_id']);
}
/**
* 更新场景
*/
public function sceneUpdate()
public function sceneEdit()
{
return $this->only(['id', 'link', 'platform_id'])->remove('id', 'require');
return $this->only(['id', 'link', 'platform_id']);
}
}