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([ 'status' => 0, '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([ 'status' => 1, 'msg' => '获取成功', 'data' => $rewards ]); } catch (\Exception $e) { return json([ 'status' => 0, 'msg' => '获取奖励失败: ' . $e->getMessage(), 'data' => [] ]); } } /** * 添加奖励 */ public function addReward(Request $request) { $data = input('post.'); //签到奖励 $reward = json_decode($data['reward'], true); $reward_id = $data['reward_id']; $reward_id_pre = $data['reward_id_pre']; if (!$reward_id_pre) { $reward_id_pre = "MHCC"; } if (!$reward_id || $reward_id == '') { $reward_id = $reward_id_pre . date('YmdHis') . mt_rand(1000, 9999); } // 开启事务 \think\facade\Db::startTrans(); try { RewardModel::where('reward_id', $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' => $reward_id ]; RewardModel::insert($rewardData); } } \think\facade\Db::commit(); return $this->renderSuccess("更新成功", ['reward_id' => $reward_id]); } catch (\Exception $e) { \think\facade\Db::rollback(); return $this->renderError("更新失败: " . $e->getMessage()); } } }