manghe/app/admin/controller/SignConfig.php
2025-04-10 02:46:53 +08:00

386 lines
13 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\SignConfig as SignConfigModel;
use app\common\model\SignConfigReward;
use app\common\model\Reward as RewardModel;
use app\common\model\Coupon;
class SignConfig extends Base
{
/**
* 签到配置列表
*/
public function index(Request $request)
{
$type = trim(input('get.type', 1));
$keyword = trim(input('get.keyword'));
$where = [];
if (!empty($keyword)) {
$where[] = ['title', 'like', '%' . $keyword . '%'];
}
$where[] = ['type', '=', $type];
$field = "*";
$order = "sort asc, id asc";
$data = SignConfigModel::getList($where, $field, $order, $this->page);
// foreach ($data['list'] as $key => &$item) {
// // 获取关联的奖励
// $rewardIds = SignConfigReward::getRewardIds($item['id']);
// $rewards = RewardModel::whereIn('id', $rewardIds)->select()->toArray();
// $item['rewards'] = $rewards;
// }
View::assign('list', $data['list']);
View::assign('count', $data['count']);
View::assign('page', $data['page']);
View::assign('type', $type);
return View::fetch("SignConfig/index");
}
public function getSignConfigList(Request $request)
{
$type = trim(input('get.type', 1));
$keyword = trim(input('get.keyword'));
$limit = trim(input('get.limit', 1));
$where = [];
if (!empty($keyword)) {
$where[] = ['title', 'like', '%' . $keyword . '%'];
}
$where[] = ['type', '=', $type];
$field = "*";
$order = "sort asc, id asc";
$data = SignConfigModel::getList($where, $field, $order, $limit);
// 获取每个配置的奖励信息
foreach ($data['list'] as $key => &$item) {
// 获取关联的奖励
$rewards = RewardModel::where('reward_id', $item['reward_id'])->select()->toArray();
$item['rewards'] = $rewards;
}
return $this->renderTable('获取成功', $data['count'], $data['list']);
}
/**
* 添加签到配置
*/
public function add(Request $request)
{
if (!$request->isPost()) {
$type = input('get.type', 1);
// 获取奖励列表供选择
// $rewards = RewardModel::where('reward_id', 1)->select();
// 获取优惠券列表供选择
$coupons = Coupon::where('status', 0)->select();
View::assign('type', $type);
return View::fetch("SignConfig/add");
} else {
$data = input('post.');
//签到奖励
$reward = json_decode($data['reward'], true);
unset($data['reward']);
$data['create_time'] = time();
$data['update_time'] = time();
//奖励关联表
$data['reward_id'] = 'MHQD' . date('YmdHis') . mt_rand(1000, 9999);
// 开启事务
\think\facade\Db::startTrans();
try {
// 添加签到配置
$configId = SignConfigModel::insertGetId($data);
// 添加奖励表数据
if (!empty($reward) && is_array($reward)) {
foreach ($reward as $item) {
$rewardData = [
'reward_type' => $item['reward_type'],
'reward_value' => isset($item['reward_value']) && !empty($item['reward_value']) ? $item['reward_value'] : 0,
'reward_extend' => isset($item['coupon_id']) && !empty($item['coupon_id']) ? $item['coupon_id'] : 0,
'description' => '',
'create_time' => time(),
'update_time' => time(),
'reward_id' => $data['reward_id']
];
RewardModel::insert($rewardData);
}
}
\think\facade\Db::commit();
return $this->renderSuccess("添加成功");
} catch (\Exception $e) {
\think\facade\Db::rollback();
return $this->renderError("添加失败: " . $e->getMessage());
}
}
}
/**
* 删除签到配置
*/
public function delete(Request $request)
{
$id = input('post.id/d', 0);
if (empty($id)) {
return $this->renderError("参数错误");
}
// 先查询配置信息获取reward_id
$info = SignConfigModel::find($id);
if (empty($info)) {
return $this->renderError("配置不存在");
}
// 开启事务
\think\facade\Db::startTrans();
try {
// 删除配置
SignConfigModel::destroy($id);
// 删除奖励数据使用reward_id关联
// if (!empty($info['reward_id'])) {
// RewardModel::where('reward_id', $info['reward_id'])->delete();
// }
\think\facade\Db::commit();
return $this->renderSuccess("删除成功");
} catch (\Exception $e) {
\think\facade\Db::rollback();
return $this->renderError("删除失败: " . $e->getMessage());
}
}
/**
* 修改排序
*/
public function sort(Request $request)
{
$id = input('post.id/d', 0);
$sort = input('post.sort/d', 0);
if (empty($id)) {
return $this->renderError("参数错误");
}
try {
SignConfigModel::where('id', $id)->update(['sort' => $sort, 'update_time' => time()]);
return $this->renderSuccess("排序更新成功");
} catch (\Exception $e) {
return $this->renderError("排序更新失败: " . $e->getMessage());
}
}
/**
* 修改状态
*/
public function status(Request $request)
{
$id = input('post.id/d', 0);
$status = input('post.status/d', 0);
if (empty($id)) {
return $this->renderError("参数错误");
}
try {
SignConfigModel::where('id', $id)->update(['status' => $status, 'update_time' => time()]);
return $this->renderSuccess("状态更新成功");
} catch (\Exception $e) {
return $this->renderError("状态更新失败: " . $e->getMessage());
}
}
/**
* 获取优惠券列表API
*/
public function getCoupons(Request $request)
{
try {
// 获取有效的优惠券列表
$coupons = Coupon::where('status', 0)->select()->toArray();
return json([
'code' => 0,
'msg' => '获取成功',
'data' => $coupons
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取优惠券失败: ' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 编辑签到配置
*/
public function edit(Request $request)
{
if (!$request->isPost()) {
$id = input('get.id/d', 0);
if (empty($id)) {
return $this->renderError("参数错误");
}
// 获取配置信息
$info = SignConfigModel::find($id);
if (empty($info)) {
return $this->renderError("配置不存在");
}
// 获取关联的奖励
$rewards = RewardModel::where('reward_id', $info['reward_id'])->select()->toArray();
View::assign('info', $info);
View::assign('rewards', $rewards);
return View::fetch("SignConfig/edit");
}
}
/**
* 更新签到配置
*/
public function update(Request $request)
{
$data = input('post.');
$id = $data['id'] ?? 0;
if (empty($id)) {
return $this->renderError("参数错误");
}
// 获取配置信息
$info = SignConfigModel::find($id);
if (empty($info)) {
return $this->renderError("配置不存在");
}
// 奖励信息
$reward = json_decode($data['reward'] ?? '[]', true);
unset($data['reward']);
$data['update_time'] = time();
$data['reward_id'] = 'MHQD' . date('YmdHis') . mt_rand(1000, 9999);//$reward_id;
// 开启事务
\think\facade\Db::startTrans();
try {
// 更新配置
SignConfigModel::update($data, ['id' => $id]);
// 删除旧的奖励
// RewardModel::where('reward_id', $info['reward_id'])->delete();
// 添加新的奖励
if (!empty($reward) && is_array($reward)) {
foreach ($reward as $item) {
$rewardData = [
'reward_type' => $item['reward_type'],
'reward_value' => isset($item['reward_value']) && !empty($item['reward_value']) ? $item['reward_value'] : 0,
'reward_extend' => isset($item['coupon_id']) && !empty($item['coupon_id']) ? $item['coupon_id'] : 0,
'description' => '',
'create_time' => time(),
'update_time' => time(),
'reward_id' => $data['reward_id']
];
RewardModel::insert($rewardData);
}
}
\think\facade\Db::commit();
return $this->renderSuccess("更新成功");
} catch (\Exception $e) {
\think\facade\Db::rollback();
return $this->renderError("更新失败: " . $e->getMessage());
}
}
/**
* 编辑签到奖励
*/
public function rewardEdit(Request $request)
{
if (!$request->isPost()) {
$id = input('get.id/d', 0);
$reward_id = input('get.reward_id', '');
if (empty($id) || empty($reward_id)) {
return $this->renderError("参数错误");
}
// 获取配置信息
$info = SignConfigModel::find($id);
if (empty($info)) {
return $this->renderError("配置不存在");
}
// 获取关联的奖励
$rewards = RewardModel::where('reward_id', $reward_id)->select()->toArray();
View::assign('info', $info);
View::assign('reward_id', $reward_id);
View::assign('rewards', $rewards);
return View::fetch("SignConfig/reward_edit");
} else {
$id = input('post.id/d', 0);
$reward_id = input('post.reward_id', '');
$reward = json_decode(input('post.reward', '[]'), true);
if (empty($id) || empty($reward_id)) {
return $this->renderError("参数错误");
}
// 获取配置信息
$info = SignConfigModel::find($id);
if (empty($info)) {
return $this->renderError("配置不存在");
}
$info['reward_id'] = $reward_id = 'MHQD' . date('YmdHis') . mt_rand(1000, 9999);//$reward_id;
// 开启事务
\think\facade\Db::startTrans();
try {
// 删除旧的奖励
//RewardModel::where('reward_id', $reward_id)->delete();
$info->save();
// 添加新的奖励
if (!empty($reward) && is_array($reward)) {
foreach ($reward as $item) {
$rewardData = [
'reward_type' => $item['reward_type'],
'reward_value' => isset($item['reward_value']) && !empty($item['reward_value']) ? $item['reward_value'] : 0,
'reward_extend' => isset($item['coupon_id']) && !empty($item['coupon_id']) ? $item['coupon_id'] : 0,
'description' => '',
'create_time' => time(),
'update_time' => time(),
'reward_id' => $reward_id
];
RewardModel::insert($rewardData);
}
}
\think\facade\Db::commit();
return $this->renderSuccess("奖励更新成功");
} catch (\Exception $e) {
\think\facade\Db::rollback();
return $this->renderError("奖励更新失败: " . $e->getMessage());
}
}
}
}