HaniBlindBox/server/php/app/admin/controller/Diamond.php
2026-01-01 20:46:07 +08:00

233 lines
7.3 KiB
PHP

<?php
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\common\model\DiamondProducts;
class Diamond extends Base
{
/**
* 钻石商品列表
*/
public function index()
{
if (request()->isAjax()) {
$param = input('param.');
$limit = $param['limit'] ?? 15;
$page = $param['page'] ?? 1;
$name = $param['name'] ?? '';
$status = $param['status'] ?? '';
$where = [];
if ($name) {
$where[] = ['name', 'like', "%{$name}%"];
}
if ($status !== '') {
$where[] = ['status', '=', $status];
}
$data = DiamondProducts::getList($where, $page, $limit);
return json([
'code' => 0,
'msg' => '',
'count' => $data['count'],
'data' => $data['list']
]);
}
// View::fetch("Goods/goods");
return View::fetch('Diamond/index');
}
/**
* 添加钻石商品
*/
public function add()
{
if (request()->isPost()) {
$param = input('post.');
// 验证数据
$validate = [
'name' => 'require',
'products_id' => 'require',
'products_type' => 'require',
'base_reward' => 'require',
'price' => 'require|float',
'is_first' => 'require|in:0,1',
'sort_order' => 'require|number',
'status' => 'require|in:0,1'
];
$message = [
'name.require' => '商品名称不能为空',
'products_id.require' => '商品ID不能为空',
'products_type.require' => '商品类型不能为空',
'base_reward.require' => '基础钻石数量不能为空',
'price.require' => '价格不能为空',
'price.float' => '价格必须为数字',
'is_first.require' => '请选择是否为首充',
'is_first.in' => '是否为首充值无效',
'sort_order.require' => '排序值不能为空',
'sort_order.number' => '排序值必须为数字',
'status.require' => '请选择状态',
'status.in' => '状态值无效'
];
$result = $this->validate($param, $validate, $message);
if ($result !== true) {
return $this->err($result);
}
// 处理图片路径
$fields = ['first_charge_image', 'first_select_charge_image', 'normal_image', 'normal_select_image'];
foreach ($fields as $field) {
if (isset($param[$field]) && $param[$field]) {
$param[$field] = str_replace('/www/wwwroot/test.zfunbox.cn/public', '', $param[$field]);
}
}
// 保存数据
$id = DiamondProducts::add($param);
if ($id) {
return $this->succ('添加成功');
} else {
return $this->err('添加失败');
}
}
return View::fetch('Diamond/add');
}
/**
* 编辑钻石商品
*/
public function edit()
{
$id = input('id/d', 0);
if (!$id) {
return $this->err('参数错误');
}
$info = DiamondProducts::getInfo($id);
if (!$info) {
return $this->err('数据不存在');
}
if (request()->isPost()) {
$param = input('post.');
// 验证数据
$validate = [
'name' => 'require',
'products_id' => 'require',
'products_type' => 'require',
'base_reward' => 'require',
'price' => 'require|float',
'is_first' => 'require|in:0,1',
'sort_order' => 'require|number',
'status' => 'require|in:0,1'
];
$message = [
'name.require' => '商品名称不能为空',
'products_id.require' => '商品ID不能为空',
'products_type.require' => '商品类型不能为空',
'base_reward.require' => '基础钻石数量不能为空',
'price.require' => '价格不能为空',
'price.float' => '价格必须为数字',
'is_first.require' => '请选择是否为首充',
'is_first.in' => '是否为首充值无效',
'sort_order.require' => '排序值不能为空',
'sort_order.number' => '排序值必须为数字',
'status.require' => '请选择状态',
'status.in' => '状态值无效'
];
$result = $this->validate($param, $validate, $message);
if ($result !== true) {
return $this->err($result);
}
// 处理图片路径
$fields = ['first_charge_image', 'first_select_charge_image', 'normal_image', 'normal_select_image'];
foreach ($fields as $field) {
if (isset($param[$field]) && $param[$field]) {
$param[$field] = str_replace('/www/wwwroot/test.zfunbox.cn/public', '', $param[$field]);
}
}
// 更新数据
$result = DiamondProducts::edit($id, $param);
if ($result !== false) {
return $this->succ('修改成功');
} else {
return $this->err('修改失败');
}
}
View::assign('info', $info);
return View::fetch('Diamond/edit');
}
/**
* 删除钻石商品
*/
public function del()
{
$id = input('id/d', 0);
if (!$id) {
return $this->err('参数错误');
}
$result = DiamondProducts::del($id);
if ($result) {
return $this->succ('删除成功');
} else {
return $this->err('删除失败');
}
}
/**
* 更改钻石商品状态
*/
public function status()
{
if (!request()->isPost()) {
return $this->err('请求方式错误');
}
$id = input('post.id/d', 0);
$status = input('post.status/d', 0);
if (!$id) {
return $this->err('参数错误');
}
$info = DiamondProducts::getInfo($id);
if (!$info) {
return $this->err('商品不存在');
}
// 更新状态
$result = DiamondProducts::where('id', $id)->update(['status' => $status, 'updated_at' => date('Y-m-d H:i:s')]);
if ($result) {
return $this->succ('状态更新成功');
} else {
return $this->err('状态更新失败');
}
}
/**
* 获取钻石商品最大排序值
*/
public function get_max_sort()
{
$maxSort = DiamondProducts::max('sort_order');
return json([
'status' => 1,
'msg' => '获取成功',
'data' => intval($maxSort)
]);
}
}