158 lines
4.1 KiB
PHP
158 lines
4.1 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\controller\v1;
|
|
|
|
use app\admin\model\v1\ProductModel;
|
|
use app\admin\validate\v1\ProductValidate;
|
|
|
|
class Product
|
|
{
|
|
// 分页列表
|
|
public function index()
|
|
{
|
|
$param = request()->get([
|
|
'name',
|
|
'spu',
|
|
'category_id',
|
|
'created_at',
|
|
'is_sale',
|
|
'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',
|
|
'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' => $param['created_at']??null,
|
|
])
|
|
->categoryNullable($param['category_id']??null)
|
|
->isSaleNullable(isset($param['is_sale']) ? (bool)$param['is_sale'] : 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('产品不存在');
|
|
}
|
|
|
|
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',
|
|
'sort',
|
|
'detail',
|
|
'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('操作失败');
|
|
}
|
|
|
|
return success('操作成功');
|
|
}
|
|
|
|
// 上下架操作
|
|
public function updownShelves()
|
|
{
|
|
$id = request()->param('id');
|
|
$product = ProductModel::bypk($id )->find();
|
|
if (empty($product)) {
|
|
return error('请确认操作对象是否存在');
|
|
}
|
|
|
|
$product->is_sale = (int)!$product->is_sale;
|
|
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('操作成功');
|
|
}
|
|
}
|