manghe/app/admin/controller/Reward.php
2025-04-01 15:28:42 +00:00

233 lines
6.7 KiB
PHP
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\Reward as RewardModel;
use app\common\model\Coupon;
class Reward extends Base
{
/**
* 奖励列表
*/
public function index(Request $request)
{
$reward_type = trim(input('get.reward_type', ''));
$keyword = trim(input('get.keyword'));
$where = [];
if (!empty($keyword)) {
$where[] = ['title', 'like', '%' . $keyword . '%'];
}
if ($reward_type !== '') {
$where[] = ['reward_type', '=', $reward_type];
}
$field = "*";
$order = "id desc";
$data = RewardModel::getList($where, $field, $order, $this->page);
// 获取关联的优惠券信息
foreach ($data['list'] as $key => &$item) {
if ($item['reward_type'] == 1 && !empty($item['reward_id'])) {
$coupon = Coupon::find($item['reward_id']);
$item['coupon'] = $coupon;
}
}
View::assign('list', $data['list']);
View::assign('count', $data['count']);
View::assign('page', $data['page']);
View::assign('reward_type', $reward_type);
return View::fetch("Reward/index");
}
/**
* 添加奖励
*/
public function add(Request $request)
{
if (!$request->isPost()) {
// 获取优惠券列表供选择
$coupons = Coupon::where('status', 0)->select();
View::assign('coupons', $coupons);
return View::fetch("Reward/add");
} else {
$data = input('post.');
// 处理优惠券类型的特殊情况
if ($data['reward_type'] == 1) {
if (empty($data['reward_id'])) {
return $this->renderError("请选择优惠券");
}
// 获取优惠券信息,设置默认标题
if (empty($data['title'])) {
$coupon = Coupon::find($data['reward_id']);
if ($coupon) {
$data['title'] = $coupon['title'];
}
}
} else {
// 非优惠券类型reward_id设为0
$data['reward_id'] = 0;
}
$data['create_time'] = time();
$data['update_time'] = time();
$result = RewardModel::insertGetId($data);
if ($result) {
return $this->renderSuccess("添加成功");
} else {
return $this->renderError("添加失败");
}
}
}
/**
* 编辑奖励
*/
public function edit(Request $request)
{
if (!$request->isPost()) {
$id = input('get.id/d', 0);
if (empty($id)) {
return $this->renderError("参数错误");
}
$info = RewardModel::find($id);
if (empty($info)) {
return $this->renderError("奖励不存在");
}
// 获取优惠券列表供选择
$coupons = Coupon::where('status', 0)->select();
View::assign('info', $info);
View::assign('coupons', $coupons);
return View::fetch("Reward/edit");
} else {
$data = input('post.');
$id = isset($data['id']) ? intval($data['id']) : 0;
if (empty($id)) {
return $this->renderError("参数错误");
}
// 处理优惠券类型的特殊情况
if ($data['reward_type'] == 1) {
if (empty($data['reward_id'])) {
return $this->renderError("请选择优惠券");
}
} else {
// 非优惠券类型reward_id设为0
$data['reward_id'] = 0;
}
$data['update_time'] = time();
$result = RewardModel::where('id', $id)->update($data);
if ($result !== false) {
return $this->renderSuccess("编辑成功");
} else {
return $this->renderError("编辑失败");
}
}
}
/**
* 删除奖励
*/
public function delete(Request $request)
{
$id = input('post.id/d', 0);
if (empty($id)) {
return $this->renderError("参数错误");
}
$result = RewardModel::destroy($id);
if ($result) {
return $this->renderSuccess("删除成功");
} else {
return $this->renderError("删除失败");
}
}
/**
* 修改状态
*/
public function status(Request $request)
{
$id = input('post.id/d', 0);
$status = input('post.status/d', 0);
if (empty($id)) {
return $this->renderError("参数错误");
}
$result = RewardModel::where('id', $id)->update(['status' => $status, 'update_time' => time()]);
if ($result !== false) {
return $this->renderSuccess("状态更新成功");
} else {
return $this->renderError("状态更新失败");
}
}
/**
* 根据reward_id获取奖励列表
*/
public function getRewardsByRewardId(Request $request)
{
$reward_id = trim(input('get.reward_id', ''));
if (empty($reward_id)) {
return json([
'code' => 1,
'msg' => '参数错误',
'data' => []
]);
}
try {
// 查询对应的奖励列表
$rewards = RewardModel::where('reward_id', $reward_id)->select()->toArray();
// 处理优惠券数据
foreach ($rewards as &$reward) {
if ($reward['reward_type'] == 1) { // 优惠券类型
// 查询优惠券信息
$coupon = Coupon::find($reward['reward_extend']);
if ($coupon) {
$reward['coupon'] = $coupon;
}
}
}
return json([
'code' => 0,
'msg' => '获取成功',
'data' => $rewards
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取奖励失败: ' . $e->getMessage(),
'data' => []
]);
}
}
}