manghe/app/admin/controller/FFProductsController.php
2025-06-25 18:26:26 +08:00

250 lines
7.2 KiB
PHP

<?php
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\common\model\FFCategories;
use app\common\model\FFProducts;
class FFProductsController extends Base
{
/**
* 商品列表
*/
public function index()
{
if (request()->isAjax()) {
$limit = input('param.limit', 15);
$page = input('param.page', 1);
$offset = ($page - 1) * $limit;
$product_name = input('param.product_name', '');
$status = input('param.status', '');
$is_hot = input('param.is_hot', '');
$is_new = input('param.is_new', '');
$is_recommend = input('param.is_recommend', '');
$category_name = input('param.category_name', '');
$where = [];
if ($product_name) {
$where[] = ['product_name', 'like', "%{$product_name}%"];
}
if ($status !== '') {
$where[] = ['status', '=', $status];
}
if ($is_hot !== '') {
$where[] = ['is_hot', '=', $is_hot];
}
if ($is_new !== '') {
$where[] = ['is_new', '=', $is_new];
}
if ($is_recommend !== '') {
$where[] = ['is_recommend', '=', $is_recommend];
}
if ($category_name) {
$where[] = ['category_name', 'like', "%{$category_name}%"];
}
$res = FFProducts::where($where)
->order('id', 'desc')
->limit($offset, $limit)
->select();
$total = FFProducts::where($where)->count();
return $this->renderTable('获取成功', $total, $res, 0);
}
return View::fetch('FFProducts/index');
}
/**
* 添加商品
*/
public function add()
{
if (request()->isPost()) {
$param = input('post.');
$validate = $this->validate($param, [
'product_name' => 'require',
'price' => 'require|float',
'stock' => 'require|number',
]);
if (true !== $validate) {
return $this->renderError($validate);
}
// 处理多选的分类,转换为逗号分隔的字符串
if (isset($param['categories']) && is_array($param['categories'])) {
$param['category_name'] = implode(',', $param['categories']);
unset($param['categories']);
}
// 设置默认值
if (!isset($param['status'])) {
$param['status'] = 1;
}
if (!isset($param['sales'])) {
$param['sales'] = 0;
}
if (!isset($param['is_hot'])) {
$param['is_hot'] = 0;
}
if (!isset($param['is_new'])) {
$param['is_new'] = 0;
}
if (!isset($param['is_recommend'])) {
$param['is_recommend'] = 0;
}
$res = FFProducts::create($param);
if ($res) {
return $this->renderSuccess('添加成功', ['url' => (string) url('ff_products')]);
} else {
return $this->renderError('添加失败');
}
}
// 获取分类列表
$categories = FFCategories::where('status', 1)
->order('sort_order', 'asc')
->select();
View::assign('categories', $categories);
return View::fetch('FFProducts/add');
}
/**
* 编辑商品
*/
public function edit()
{
$id = input('param.id', 0);
$info = FFProducts::where('id', $id)->find();
if (empty($info)) {
return $this->renderError('数据不存在');
}
if (request()->isPost()) {
$param = input('post.');
$validate = $this->validate($param, [
'product_name' => 'require',
'price' => 'require|float',
'stock' => 'require|number',
]);
if (true !== $validate) {
return $this->renderError($validate);
}
// 处理多选的分类,转换为逗号分隔的字符串
if (isset($param['categories']) && is_array($param['categories'])) {
$param['category_name'] = implode(',', $param['categories']);
unset($param['categories']);
}
$res = FFProducts::update($param, ['id' => $id]);
if ($res) {
return $this->renderSuccess('编辑成功', ['url' => (string) url('ff_products')]);
} else {
return $this->renderError('编辑失败');
}
}
// 获取分类列表
$categories = FFCategories::where('status', 1)
->order('sort_order', 'asc')
->select()->toArray();
// 将已有的分类名称转为数组
$selectedCategories = [];
if ($info['category_name']) {
$selectedCategories = explode(',', $info['category_name']);
}
View::assign('categories', $categories);
View::assign('selectedCategories', $selectedCategories);
View::assign('info', $info);
return View::fetch('FFProducts/edit');
}
/**
* 修改商品状态(上架/下架)
*/
public function status()
{
$id = input('param.id', 0);
$status = input('param.status', 0);
$res = FFProducts::update(['status' => $status], ['id' => $id]);
if ($res) {
return $this->renderSuccess('操作成功');
} else {
return $this->renderError('操作失败');
}
}
/**
* 修改热销状态
*/
public function hot()
{
$id = input('param.id', 0);
$is_hot = input('param.is_hot', 0);
$res = FFProducts::update(['is_hot' => $is_hot], ['id' => $id]);
if ($res) {
return $this->renderSuccess('操作成功');
} else {
return $this->renderError('操作失败');
}
}
/**
* 修改新品状态
*/
public function new()
{
$id = input('param.id', 0);
$is_new = input('param.is_new', 0);
$res = FFProducts::update(['is_new' => $is_new], ['id' => $id]);
if ($res) {
return $this->renderSuccess('操作成功');
} else {
return $this->renderError('操作失败');
}
}
/**
* 修改推荐状态
*/
public function recommend()
{
$id = input('param.id', 0);
$is_recommend = input('param.is_recommend', 0);
$res = FFProducts::update(['is_recommend' => $is_recommend], ['id' => $id]);
if ($res) {
return $this->renderSuccess('操作成功');
} else {
return $this->renderError('操作失败');
}
}
/**
* 删除商品
*/
public function del()
{
$id = input('param.id', 0);
$res = FFProducts::destroy($id);
if ($res) {
return $this->renderSuccess('删除成功');
} else {
return $this->renderError('删除失败');
}
}
}