diff --git a/app/admin/controller/Base.php b/app/admin/controller/Base.php index 1a8627f..6af5dfa 100755 --- a/app/admin/controller/Base.php +++ b/app/admin/controller/Base.php @@ -21,6 +21,7 @@ class Base extends MyController public $page_num = '10'; public $admin_id = 0; + public $config = []; /** * 后台初始化 */ @@ -277,5 +278,44 @@ class Base extends MyController return $uid; // 如果未找到或未配置,返回原值 } + /** + * 生成不带连字符的UUID(可选前缀) + * @param string $prefix 4位前缀(字母或数字) + * @param bool $withHyphens 是否包含连字符(默认false) + * @return string + */ + protected function generateUUIDWithPrefix(string $prefix = '', bool $withHyphens = false) + { + // 验证前缀 + if (!empty($prefix)) { + if (strlen($prefix) !== 4 || !ctype_alnum($prefix)) { + throw new \InvalidArgumentException('前缀必须是4位字母或数字'); + } + $prefix = strtoupper($prefix); + } + // 生成UUID(不带连字符的原始数据) + $uuid = sprintf( + '%04x%04x%04x%04x%04x%04x%04x%04x', + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), + mt_rand(0, 0x0fff) | 0x4000, + mt_rand(0, 0x3fff) | 0x8000, + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), + mt_rand(0, 0xffff) + ); + + // 可选:重新添加连字符(标准UUID格式) + if ($withHyphens) { + $uuid = substr($uuid, 0, 8) . '-' . + substr($uuid, 8, 4) . '-' . + substr($uuid, 12, 4) . '-' . + substr($uuid, 16, 4) . '-' . + substr($uuid, 20, 12); + } + + return $prefix ? $prefix . ($withHyphens ? '-' : '') . $uuid : $uuid; + } } diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 02de763..0158a52 100755 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -20,7 +20,7 @@ class Goods extends Base */ public function goods(Request $request) { - + return View::fetch("Goods/goods"); } @@ -34,7 +34,7 @@ class Goods extends Base $title = $request->param('title/s', ''); $status = $request->param('status/s', ''); $type = $request->param('type/s', ''); - + // 构建查询条件 $where = []; if (!empty($title)) { @@ -46,12 +46,12 @@ class Goods extends Base if ($type !== '') { $where[] = ['type', '=', $type]; } - + $query = GoodsModel::where($where)->order('id desc'); $count = $query->count(); - + $list = $query->page($page, $limit)->select()->toArray(); - + // 处理图片路径 foreach ($list as &$item) { $item['imgurl'] = imageUrl($item['imgurl']); @@ -61,10 +61,10 @@ class Goods extends Base // 添加格式化时间 $item['addtime_text'] = date('Y-m-d H:i', $item['addtime']); } - + return $this->renderTable('获取成功', $count, $list); } - + /** * 获取盒子类型列表(前后端分离接口) */ @@ -77,7 +77,7 @@ class Goods extends Base ->order('sort_order') ->select() ->toArray(); - + return $this->renderTable('获取成功', count($goodsTypeList), $goodsTypeList); } @@ -89,7 +89,7 @@ class Goods extends Base if (!$request->isPost()) { $item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray(); $shang = Shang::where('id', 'between', [34, 38])->select()->toArray(); - + // 查询可用的盒子类型 $goodsTypeList = Db::name('goods_type') ->field('value,sort_order,remark,is_fenlei,fl_name') @@ -97,7 +97,7 @@ class Goods extends Base ->order('sort_order') ->select() ->toArray(); - + View::assign('goodsTypeList', $goodsTypeList); View::assign('shang', $shang); View::assign('item_card', $item_card); @@ -121,7 +121,7 @@ class Goods extends Base } else { $data['daily_xiangou'] = intval($data['daily_xiangou']); } - if ($data['type'] == 1||$data['type'] == 11) { + if ($data['type'] == 1 || $data['type'] == 11) { if (RegInt($data['stock'])) { return $this->renderError("库存输入不规范,请设置大于0的整数"); } @@ -278,7 +278,7 @@ class Goods extends Base $type = $info['type']; $item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray(); $shang = Shang::where('id', 'between', [34, 38])->select()->toArray(); - + // 查询可用的盒子类型 $goodsTypeList = Db::name('goods_type') ->field('value,sort_order,remark,is_fenlei,fl_name') @@ -286,18 +286,18 @@ class Goods extends Base ->order('sort_order') ->select() ->toArray(); - + View::assign('goodsTypeList', $goodsTypeList); View::assign('shang', $shang); View::assign('item_card', $item_card); View::assign('type', $type); View::assign('info', $info); - + // 确保daily_xiangou字段存在 if (!isset($info['daily_xiangou'])) { $info['daily_xiangou'] = 0; } - + return View::fetch("Goods/goods_edit"); } else { $data = input('post.'); @@ -326,7 +326,7 @@ class Goods extends Base $data['daily_xiangou'] = intval($data['daily_xiangou']); } $type = $info['type']; - if ($type == 1||$type == 11) { + if ($type == 1 || $type == 11) { if (RegInt($data['stock'])) { return $this->renderError("库存输入不规范,请设置大于0的整数"); } @@ -434,7 +434,7 @@ class Goods extends Base Db::startTrans(); $res = []; #添加套 - if ($type == 1 || $type == 11 || $type == 5 ||$type == 10 || $type == 6) { + if ($type == 1 || $type == 11 || $type == 5 || $type == 10 || $type == 6) { if (($data['stock'] > $info['stock'])) { #赏品 $goods_list = GoodsList::where(['goods_id' => $info['id']]) @@ -460,9 +460,9 @@ class Goods extends Base } $res[] = $info->allowField([])->update($data); #添加日志 - $info['update_time'] = date('Y-m-d H:i:i:s',$info['update_time']); - $data['update_time'] = date('Y-m-d H:i:i:s',$data['update_time']); - $res[] = AdminGoodsLog::add_goods_log(session('admin_id'),$info['id'],0,json_encode($info),json_encode($data)); + $info['update_time'] = date('Y-m-d H:i:i:s', $info['update_time']); + $data['update_time'] = date('Y-m-d H:i:i:s', $data['update_time']); + $res[] = AdminGoodsLog::add_goods_log(session('admin_id'), $info['id'], 0, json_encode($info), json_encode($data)); if (resCheck($res)) { Db::commit(); return $this->renderSuccess("编辑成功"); @@ -489,7 +489,7 @@ class Goods extends Base $result = GoodsModel::where(['id' => $id])->update(['status' => $status]); } elseif ($status == 3) { $result = GoodsModel::where(['id' => $id])->update(['delete_time' => time(), 'status' => 2]); -// $goods = GoodsList::field('id')->where(['goods_id' => $id])->find(); + // $goods = GoodsList::field('id')->where(['goods_id' => $id])->find(); // if ($goods) { // GoodsList::field('id')->where(['goods_id' => $id])->delete(); // } @@ -538,7 +538,7 @@ class Goods extends Base $real_pro = 0; foreach ($data['list'] as &$value) { $value['shang'] = Shang::where(['id' => $value['shang_id']])->value('title'); -// if ($value['goods_type'] == 2) { + // if ($value['goods_type'] == 2) { // $value['sale_time'] = date('Y-m-d', $value['sale_time']); // } else { // $value['sale_time'] = ''; @@ -566,7 +566,7 @@ class Goods extends Base if (!$info) { return $this->renderError('请求参数错误'); } - if ($info['type'] == 1 || $info['type'] == 5 || $info['type'] == 10 || $info['type'] == 11|| $info['type'] == 6) { + if ($info['type'] == 1 || $info['type'] == 5 || $info['type'] == 10 || $info['type'] == 11 || $info['type'] == 6) { $shang = Shang::where('id', '<=', 33)->where('id', '<>', 5)->select()->toArray(); } elseif ($info['type'] == 2 || $info['type'] == 8 || $info['type'] == 9) { $shang = Shang::where('id', 'between', [34, 38])->select()->toArray(); @@ -642,8 +642,8 @@ class Goods extends Base $data['prize_code'] = $prize_code; $save_sports_data[] = $data; } - } elseif (in_array($type,[2,8,9])) { - if (RegMoney($data['real_pro']*100)) { + } elseif (in_array($type, [2, 8, 9])) { + if (RegMoney($data['real_pro'] * 100)) { return $this->renderError('真实概率设置错误,最多保留两位小数1'); } @@ -678,14 +678,14 @@ class Goods extends Base if (RegZero($data['sort'])) { return $this->renderError('排序请输入整数'); } -// if ($data['card_no']) { + // if ($data['card_no']) { // $card_no_info = GoodsList::field('id')->where('card_no', '=', $data['card_no'])->find(); // if ($card_no_info) { // return $this->renderError('赠送编号已存在'); // } // } else { - $data['card_no'] = NULL; -// } + $data['card_no'] = NULL; + // } if (empty($data['imgurl'])) { return $this->renderError('请上传图片'); @@ -734,13 +734,13 @@ class Goods extends Base } else { return $this->renderError('请求参数错误1'); } - + View::assign('goods', $goods); View::assign('shang', $shang); View::assign('type', $info['type']); return View::fetch('Goods/goodslist_edit'); } else { - + $data = input('post.'); if (empty($data['id'])) { return $this->renderError('请求参数错误1'); @@ -791,7 +791,7 @@ class Goods extends Base if ($type == 1 || $type == 5 || $type == 10 || $type == 6 || $type == 11) { } elseif ($type == 2 || $type == 8 || $type == 9) { - if (RegMoney($data['real_pro']*1000)) { + if (RegMoney($data['real_pro'] * 1000)) { return $this->renderError('真实概率设置错误,最多保留两位小数'); } } else if ($type == 3) { @@ -804,7 +804,7 @@ class Goods extends Base if (RegZero($data['sort'])) { return $this->renderError('排序请输入整数'); } -// if ($data['card_no']) { + // if ($data['card_no']) { // $card_no_info = GoodsList::field('id') // ->where('prize_code', '<>', $goods['prize_code']) // ->where('card_no', '=', trim($data['card_no'])) @@ -813,8 +813,8 @@ class Goods extends Base // return $this->renderError('赠送编号已存在'); // } // } else { - $data['card_no'] = NULL; -// } + $data['card_no'] = NULL; + // } if (empty($data['imgurl'])) { return $this->renderError('请上传图片'); @@ -822,12 +822,12 @@ class Goods extends Base $data['update_time'] = time(); unset($data['id']); $res1 = GoodsList::where(['prize_code' => $goods['prize_code']])->update($data); - + if ($res1) { #添加日志 - $goods['update_time'] = date('Y-m-d H:i:i:s',$goods['update_time']); - $data['update_time'] = date('Y-m-d H:i:i:s',$data['update_time']); - $res2 = AdminGoodsLog::add_goods_log(session('admin_id'),$info['id'],$goods['id'],json_encode($goods),json_encode($data)); + $goods['update_time'] = date('Y-m-d H:i:i:s', $goods['update_time']); + $data['update_time'] = date('Y-m-d H:i:i:s', $data['update_time']); + $res2 = AdminGoodsLog::add_goods_log(session('admin_id'), $info['id'], $goods['id'], json_encode($goods), json_encode($data)); return $this->renderSuccess('编辑成功'); } else { return $this->renderError('编辑失败'); @@ -1173,10 +1173,10 @@ class Goods extends Base public function get_sync_addresses() { $config = getConfig('systemconfig'); - $syncAddresses = isset($config['sync_address']) && is_array($config['sync_address']) - ? $config['sync_address'] + $syncAddresses = isset($config['sync_address']) && is_array($config['sync_address']) + ? $config['sync_address'] : []; - + return $this->renderSuccess('获取成功', $syncAddresses); } @@ -1188,13 +1188,13 @@ class Goods extends Base $goods_id = trim(input('get.goods_id')); $start_time = trim(input('get.start_time')); $end_time = trim(input('get.end_time')); - + $whe = []; - + if ($goods_id) { $whe[] = ['goods_id', '=', $goods_id]; } - + if ($start_time && $end_time) { $start = strtotime($start_time); $end = strtotime($end_time . ' 23:59:59'); @@ -1206,7 +1206,7 @@ class Goods extends Base $end = strtotime($end_time . ' 23:59:59'); $whe[] = ['create_time', '<=', $end]; } - + // 获取日志数据 $list = Db::name('goods_offshelf_log') ->where($whe) @@ -1215,18 +1215,18 @@ class Goods extends Base 'list_rows' => $this->page, 'query' => request()->param(), ]); - + // 获取所有相关的盒子信息 $goodsIds = array_column($list->items(), 'goods_id'); $goodsInfo = []; - + if (!empty($goodsIds)) { $goods = Db::name('goods') ->field('id, title, status') ->whereIn('id', $goodsIds) ->select() ->toArray(); - + foreach ($goods as $item) { $goodsInfo[$item['id']] = [ 'title' => $item['title'], @@ -1234,7 +1234,7 @@ class Goods extends Base ]; } } - + // 转换时间戳并处理数据 $listItems = $list->items(); foreach ($listItems as &$item) { @@ -1242,7 +1242,7 @@ class Goods extends Base $item['goods_title'] = isset($goodsInfo[$item['goods_id']]) ? $goodsInfo[$item['goods_id']]['title'] : '未知盒子'; $item['goods_status'] = isset($goodsInfo[$item['goods_id']]) ? $goodsInfo[$item['goods_id']]['status'] : 2; // 默认下架状态 } - + View::assign('list', $listItems); View::assign('page', $list->render()); View::assign('count', $list->total()); @@ -1256,21 +1256,21 @@ class Goods extends Base { $goods_id = $this->request->post('goods_id/d', 0); $targets = $this->request->post('targets/a', []); - + if (empty($goods_id)) { return $this->renderError('盒子ID不能为空'); } - + if (empty($targets)) { return $this->renderError('同步目标不能为空'); } - + # 获取盒子数据 $goods = \app\common\model\Goods::find($goods_id); if (!$goods) { return $this->renderError('盒子不存在'); } - + # 检查是否有async_code,没有则生成 if (empty($goods['async_code'])) { $async_code = $this->generateUUID(); @@ -1280,10 +1280,10 @@ class Goods extends Base } else { $async_code = $goods['async_code']; } - + # 获取盒子奖品数据 $goodsList = \app\common\model\GoodsList::where('goods_id', $goods_id)->select()->toArray(); - + # 准备同步数据 $syncData = [ 'goods' => $goods->toArray(), @@ -1291,12 +1291,12 @@ class Goods extends Base 'async_code' => $async_code, 'sync_time' => time() ]; - + # 发送到所有目标 $successCount = 0; $failCount = 0; $errorMessages = []; - + foreach ($targets as $target) { $result = $this->sendSyncData($target, $syncData); if ($result['success']) { @@ -1306,11 +1306,11 @@ class Goods extends Base $errorMessages[] = $target . ': ' . $result['message']; } } - + # 更新同步状态 $goods->async_date = date('Y-m-d H:i:s'); $goods->save(); - + if ($failCount == 0) { return $this->renderSuccess("同步成功,已成功同步到 {$successCount} 个目标"); } else if ($successCount > 0) { @@ -1340,13 +1340,13 @@ class Goods extends Base try { // 清空订单表 $orderCount = Db::name('order')->where('goods_id', $id)->delete(); - + // 清空订单详情表 $orderListCount = Db::name('order_list')->where('goods_id', $id)->delete(); - + // 提交事务 Db::commit(); - + // 记录管理员操作日志 AdminGoodsLog::add_goods_log( session('admin_id'), @@ -1359,7 +1359,7 @@ class Goods extends Base 'clear_order_list_count' => $orderListCount ]) ); - + return $this->renderSuccess("操作成功,共清空订单 {$orderCount} 条,订单详情 {$orderListCount} 条"); } catch (\Exception $e) { // 回滚事务 @@ -1371,17 +1371,22 @@ class Goods extends Base /** * 生成UUID */ - private function generateUUID() + private function generateUUID() { - return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', - mt_rand(0, 0xffff), mt_rand(0, 0xffff), + return sprintf( + '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) + mt_rand(0, 0xffff), + mt_rand(0, 0xffff), + mt_rand(0, 0xffff) ); } + /** * 发送同步数据到目标 */ @@ -1390,7 +1395,7 @@ class Goods extends Base try { # 准备请求数据 $jsonData = json_encode($data); - + # 初始化CURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $target . "/api/goods/receive_sync"); @@ -1402,30 +1407,30 @@ class Goods extends Base 'Content-Length: ' . strlen($jsonData) ]); curl_setopt($ch, CURLOPT_TIMEOUT, 30); # 30秒超时 - + # 执行请求 $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); - + if ($error) { return ['success' => false, 'message' => "CURL错误: $error"]; } - + if ($httpCode != 200) { return ['success' => false, 'message' => "HTTP错误码: $httpCode"]; } - + $result = json_decode($response, true); if (!$result || !isset($result['status'])) { return ['success' => false, 'message' => "无效的响应: $response"]; } - + if ($result['status'] != 1) { return ['success' => false, 'message' => $result['msg'] ?? '同步失败']; } - + return ['success' => true]; } catch (\Exception $e) { return ['success' => false, 'message' => "异常: " . $e->getMessage()]; diff --git a/app/admin/controller/Reward.php b/app/admin/controller/Reward.php new file mode 100644 index 0000000..12dba13 --- /dev/null +++ b/app/admin/controller/Reward.php @@ -0,0 +1,233 @@ +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' => [] + ]); + } + } +} \ No newline at end of file diff --git a/app/admin/controller/SignConfig.php b/app/admin/controller/SignConfig.php new file mode 100644 index 0000000..c7b7f50 --- /dev/null +++ b/app/admin/controller/SignConfig.php @@ -0,0 +1,386 @@ +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()); + } + } + } +} \ No newline at end of file diff --git a/app/admin/route/app.php b/app/admin/route/app.php index dd1ad6f..cd1bb6b 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -342,4 +342,40 @@ Route::rule('welfare_house', 'WelfareHouse/index', 'GET|POST'); Route::rule('welfare_house_add', 'WelfareHouse/add', 'GET|POST'); Route::rule('welfare_house_edit', 'WelfareHouse/edit', 'GET|POST'); Route::rule('welfare_house_del', 'WelfareHouse/del', 'POST'); -Route::rule('welfare_house_status', 'WelfareHouse/status', 'POST'); \ No newline at end of file +Route::rule('welfare_house_status', 'WelfareHouse/status', 'POST'); + +#============================ +#SignConfig.php签到配置管理 +#============================ +Route::rule('sign_config', 'SignConfig/index', 'GET|POST'); +Route::rule('sign_config_list', 'SignConfig/getSignConfigList', 'GET|POST'); +Route::rule('sign_config_add', 'SignConfig/add', 'GET|POST'); +Route::rule('sign_config_edit', 'SignConfig/edit', 'GET|POST'); +Route::rule('sign_config_delete', 'SignConfig/delete', 'POST'); +Route::rule('sign_config_sort', 'SignConfig/sort', 'POST'); +Route::rule('sign_config_status', 'SignConfig/status', 'POST'); +Route::rule('get_coupons', 'SignConfig/getCoupons', 'GET'); + +#============================ +#Reward.php奖励管理 +#============================ +Route::rule('reward', 'Reward/index', 'GET|POST'); +Route::rule('reward_add', 'Reward/add', 'GET|POST'); +Route::rule('reward_edit', 'Reward/edit', 'GET|POST'); +Route::rule('reward_delete', 'Reward/delete', 'POST'); +Route::rule('reward_status', 'Reward/status', 'POST'); +Route::rule('get_rewards_by_id', 'Reward/getRewardsByRewardId', 'GET'); + +// 签到配置 +Route::get('sign_config', 'SignConfig/index'); +Route::get('sign_config_list', 'SignConfig/getSignConfigList'); +Route::get('sign_config_add', 'SignConfig/add'); +Route::post('sign_config_add', 'SignConfig/add'); +Route::get('sign_config_edit', 'SignConfig/edit'); +Route::post('sign_config_update', 'SignConfig/update'); +Route::get('sign_config_reward_edit', 'SignConfig/rewardEdit'); +Route::post('sign_config_reward_edit', 'SignConfig/rewardEdit'); +Route::post('sign_config_delete', 'SignConfig/delete'); +Route::post('sign_config_sort', 'SignConfig/sort'); +Route::post('sign_config_status', 'SignConfig/status'); +Route::get('sign_config_coupons', 'SignConfig/getCoupons'); \ No newline at end of file diff --git a/app/admin/view/Public/footer.html b/app/admin/view/Public/footer.html index f1290b4..5e569c3 100755 --- a/app/admin/view/Public/footer.html +++ b/app/admin/view/Public/footer.html @@ -1,2 +1,7 @@ + - \ No newline at end of file + + + + + diff --git a/app/admin/view/Public/header2.html b/app/admin/view/Public/header2.html index 075d32b..222062b 100755 --- a/app/admin/view/Public/header2.html +++ b/app/admin/view/Public/header2.html @@ -8,9 +8,11 @@ + + \ No newline at end of file diff --git a/app/admin/view/Reward/add.html b/app/admin/view/Reward/add.html new file mode 100644 index 0000000..21334b8 --- /dev/null +++ b/app/admin/view/Reward/add.html @@ -0,0 +1,184 @@ +{include file="Public:header2"/} +
+ + +