增加产品模块
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\Cms\Products;
|
||||
|
||||
use app\admin\BaseController;
|
||||
use think\response\Json;
|
||||
use app\model\Cms\Products;
|
||||
use think\exception\ValidateException;
|
||||
|
||||
/**
|
||||
* 特色产品管理控制器
|
||||
*/
|
||||
class ProductsController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取特色产品列表
|
||||
* @return Json
|
||||
*/
|
||||
public function productsList(): Json
|
||||
{
|
||||
try {
|
||||
$page = (int)$this->request->param('page', 1);
|
||||
$limit = (int)$this->request->param('limit', 10);
|
||||
$keyword = $this->request->param('keyword', '');
|
||||
|
||||
$query = Products::where('delete_time', null)
|
||||
->where('tid', $this->getTenantId());
|
||||
|
||||
// 关键词搜索
|
||||
if (!empty($keyword)) {
|
||||
$query->where('title', 'like', '%' . $keyword . '%');
|
||||
}
|
||||
|
||||
$total = $query->count();
|
||||
$list = $query->order('sort', 'asc')
|
||||
->order('id', 'desc')
|
||||
->page($page, $limit)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => [
|
||||
'list' => $list,
|
||||
'total' => $total
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage(),
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加特色产品
|
||||
* @return Json
|
||||
*/
|
||||
public function addProducts(): Json
|
||||
{
|
||||
try {
|
||||
$data = $this->request->param();
|
||||
|
||||
$product = new Products();
|
||||
$product->tid = $this->getTenantId();
|
||||
$product->title = $data['title'];
|
||||
$product->url = $data['url'];
|
||||
$product->thumb = $data['thumb'] ?? '';
|
||||
$product->desc = $data['desc'] ?? '';
|
||||
$product->content = $data['content'] ?? '';
|
||||
$product->sort = $data['sort'] ?? 0;
|
||||
$product->create_time = date('Y-m-d H:i:s');
|
||||
$product->save();
|
||||
|
||||
$this->logSuccess('特色产品', '添加产品', ['id' => $product->id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '添加成功',
|
||||
'data' => $product->toArray()
|
||||
]);
|
||||
} catch (ValidateException $e) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => $e->getError()
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('特色产品', '添加产品', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '添加失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新特色产品
|
||||
* @param int $id
|
||||
* @return Json
|
||||
*/
|
||||
public function editProducts(int $id): Json
|
||||
{
|
||||
try {
|
||||
$data = $this->request->param();
|
||||
|
||||
$product = Products::where('id', $id)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$product) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '产品不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
if (isset($data['title'])) $product->title = $data['title'];
|
||||
if (isset($data['url'])) $product->url = $data['url'];
|
||||
if (isset($data['thumb'])) $product->thumb = $data['thumb'];
|
||||
if (isset($data['desc'])) $product->desc = $data['desc'];
|
||||
if (isset($data['sort'])) $product->sort = $data['sort'];
|
||||
$product->update_time = date('Y-m-d H:i:s');
|
||||
$product->save();
|
||||
|
||||
$this->logSuccess('特色产品', '更新产品', ['id' => $id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新成功',
|
||||
'data' => $product->toArray()
|
||||
]);
|
||||
} catch (ValidateException $e) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => $e->getError()
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('特色产品', '更新产品', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '更新失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除特色产品
|
||||
* @param int $id
|
||||
* @return Json
|
||||
*/
|
||||
public function deleteProducts(int $id): Json
|
||||
{
|
||||
try {
|
||||
$product = Products::where('id', $id)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$product) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '产品不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
$product->delete();
|
||||
|
||||
$this->logSuccess('特色产品', '删除产品', ['id' => $id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('特色产品', '删除产品', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '删除失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user