218 lines
6.4 KiB
PHP
218 lines
6.4 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\controller\v1;
|
|
|
|
use app\admin\model\v1\ProductModel;
|
|
use app\admin\model\v1\ProductParamsModel;
|
|
use app\admin\model\v1\ProductRelatedModel;
|
|
use app\admin\validate\v1\ProductValidate;
|
|
|
|
class Product
|
|
{
|
|
// 分页列表
|
|
public function index()
|
|
{
|
|
$param = request()->get([
|
|
'name',
|
|
'spu',
|
|
'category_id',
|
|
'created_at',
|
|
'is_show',
|
|
'page/d' => 1,
|
|
'size/d' => 10
|
|
]);
|
|
|
|
$validate = new ProductValidate();
|
|
if (!$validate->scene('index')->check($param)) {
|
|
return error($validate->getError());
|
|
}
|
|
|
|
$products = ProductModel::field([
|
|
'id',
|
|
'name',
|
|
'cover_image',
|
|
'spu',
|
|
'category_id',
|
|
'sort',
|
|
'is_new',
|
|
'is_hot',
|
|
'is_sale',
|
|
'is_show',
|
|
'stock_qty',
|
|
'status',
|
|
'created_at'
|
|
])
|
|
->with('category')
|
|
->language(request()->lang_id)
|
|
->withSearch(['name_nullable', 'spu_nullable', 'created_at_nullable'], [
|
|
'name_nullable' => $param['name']??null,
|
|
'spu_nullable' => $param['spu']??null,
|
|
'created_at_nullable' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null,
|
|
])
|
|
->categoryNullable($param['category_id']??null)
|
|
->isShowNullable(isset($param['is_show']) ? (bool)$param['is_show'] : null)
|
|
->order(['sort' => 'asc', 'id' => 'desc'])
|
|
->paginate([
|
|
'list_rows' => $param['size'],
|
|
'page' => $param['page'],
|
|
])
|
|
->bindAttr('category', ['category_name' => 'name'])
|
|
->hidden(['category_id', 'category']);
|
|
|
|
return success('获取成功', $products);
|
|
}
|
|
|
|
// 产品详情
|
|
public function read()
|
|
{
|
|
$product = ProductModel::withoutField([
|
|
'language_id',
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at'
|
|
])
|
|
->bypk(request()->param('id'))
|
|
->find();
|
|
if (empty($product)) {
|
|
return error('产品不存在');
|
|
}
|
|
|
|
// 获取产品参数
|
|
$params = [];
|
|
$product_params = ProductParamsModel::productId($product->id)->select();
|
|
foreach ($product_params as $val) {
|
|
$params[] = implode(":", [$val->name, $val->value]);
|
|
}
|
|
$product->params = implode(PHP_EOL, $params);
|
|
|
|
// 获取关联产品
|
|
$product->related = ProductRelatedModel::field([
|
|
'related_product_id',
|
|
'sort'
|
|
])
|
|
->with(['product' => function($query) {
|
|
$query->field(['id', 'spu']);
|
|
}])
|
|
->productId($product->id)
|
|
->select()
|
|
->bindAttr('product', ['spu'])
|
|
->hidden(['product']);
|
|
|
|
return success('获取成功', $product);
|
|
}
|
|
|
|
// 更新
|
|
public function update()
|
|
{
|
|
$id = request()->param('id');
|
|
$put = request()->put([
|
|
'category_id',
|
|
'spu',
|
|
'name',
|
|
'short_name',
|
|
'cover_image',
|
|
'desc',
|
|
'video_img',
|
|
'video_url',
|
|
'is_sale',
|
|
'is_new',
|
|
'is_hot',
|
|
'is_show',
|
|
'sort',
|
|
'detail',
|
|
'params' => '',
|
|
'related' => '',
|
|
'status' => 1,
|
|
'seo_title',
|
|
'seo_keywords',
|
|
'seo_desc'
|
|
]);
|
|
|
|
$validate = new ProductValidate();
|
|
$check_data = array_merge($put, [
|
|
'id' => $id,
|
|
'language_id' => request()->lang_id
|
|
]);
|
|
if (!$validate->scene('update')->check($check_data)) {
|
|
return error($validate->getError());
|
|
}
|
|
|
|
$product = ProductModel::bypk($id)->find();
|
|
if (is_null($product)) {
|
|
return error('请确认操作对象是否存在');
|
|
}
|
|
if (!$product->save($put)) {
|
|
return error('操作失败');
|
|
}
|
|
|
|
// 更新产品参数
|
|
if ($put['params'] != "") {
|
|
ProductParamsModel::productId($id)->delete();
|
|
if (preg_match_all('/(\w+):(.[^\n|\r|\r\n]+)/', $put['params'], $match_result)) {
|
|
$params = [];
|
|
for ($i = 0; $i < count($match_result[0]); $i++) {
|
|
$params[] = [
|
|
'product_id' => $id,
|
|
'name' => $match_result[1][$i],
|
|
'value' => $match_result[2][$i]
|
|
];
|
|
}
|
|
if (!empty($params)) ProductParamsModel::insertAll($params);
|
|
}
|
|
}
|
|
|
|
// 更新关联产品
|
|
if ($put['related'] != "") {
|
|
ProductRelatedModel::productId($id)->delete();
|
|
$encode_result = json_decode($put['related'], true);
|
|
if (!empty($encode_result)) {
|
|
$related = [];
|
|
foreach ($encode_result as $val) {
|
|
$related[] = [
|
|
'product_id' => $id,
|
|
'related_product_id' => $val['related_product_id'],
|
|
'sort' => $val['sort']
|
|
];
|
|
}
|
|
if (!empty($related)) ProductRelatedModel::insertAll($related);
|
|
}
|
|
}
|
|
|
|
return success('操作成功');
|
|
}
|
|
|
|
// 上下架操作
|
|
public function updownShelves()
|
|
{
|
|
$id = request()->param('id');
|
|
$product = ProductModel::bypk($id )->find();
|
|
if (empty($product)) {
|
|
return error('请确认操作对象是否存在');
|
|
}
|
|
|
|
$product->is_show = (int)!$product->is_show;
|
|
if (!$product->save()) {
|
|
return error('操作失败');
|
|
}
|
|
|
|
return success('操作成功');
|
|
}
|
|
|
|
// 删除
|
|
public function delete()
|
|
{
|
|
$product = ProductModel::bypk(request()->param('id'))->find();
|
|
if (empty($product)) {
|
|
return error('请确认操作对象是否存在');
|
|
}
|
|
|
|
// 软删除
|
|
if (!$product->delete()) {
|
|
return error('操作失败');
|
|
}
|
|
|
|
return success('操作成功');
|
|
}
|
|
}
|