From f1de9f5680ab13e13b248427fee3e376ab980db7 Mon Sep 17 00:00:00 2001 From: manghe Date: Fri, 21 Mar 2025 11:54:53 +0000 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Base.php | 15 + app/admin/controller/Config.php | 33 ++ app/admin/controller/Goods.php | 151 +++++- app/admin/route/app.php | 3 + app/admin/view/Config/systemconfig.html | 128 +++++ app/admin/view/Goods/goods.html | 598 ++++++++++++++---------- app/api/controller/Goods.php | 71 +++ app/api/route/app.php | 5 +- config/menu.php | 4 + 9 files changed, 762 insertions(+), 246 deletions(-) create mode 100644 app/admin/view/Config/systemconfig.html diff --git a/app/admin/controller/Base.php b/app/admin/controller/Base.php index 1d76e42..e7687ed 100755 --- a/app/admin/controller/Base.php +++ b/app/admin/controller/Base.php @@ -69,6 +69,21 @@ class Base extends MyController } + /** + * 获取指定配置项的特定键值 + * + * @param string $configName 配置项名称,如 'systemconfig' + * @param string $key 需要获取的配置键 + * @param mixed $default 如果配置不存在时的默认值 + * @return mixed 配置值或默认值 + */ + protected function getConfigValue($configName, $key, $default = null) + { + $config = getConfig($configName); + return isset($config[$key]) ? $config[$key] : $default; + } + + #获取菜单 public function getMyMenuList() diff --git a/app/admin/controller/Config.php b/app/admin/controller/Config.php index 363355c..106af99 100755 --- a/app/admin/controller/Config.php +++ b/app/admin/controller/Config.php @@ -54,12 +54,45 @@ class Config extends Base return View::fetch('Config/weixinpay'); } + //系统设置 + public function systemconfig(Request $request) + { + $config = getConfig('systemconfig'); + View::assign("key", "systemconfig"); + View::assign("data", $config); + return View::fetch('Config/systemconfig'); + } //修改 public function update() { $data = input("post."); $data['update_time'] = time(); + + // 处理同步地址数据格式 + if ($data['key'] == 'systemconfig') { + $syncAddresses = []; + $addressNames = isset($data['sync_address_names']) ? $data['sync_address_names'] : []; + $addressUrls = isset($data['sync_address_urls']) ? $data['sync_address_urls'] : []; + + // 移除原有数组 + unset($data['sync_address_names']); + unset($data['sync_address_urls']); + + // 构建新的数据格式 + for ($i = 0; $i < count($addressUrls); $i++) { + if (!empty($addressUrls[$i])) { + $syncAddresses[] = [ + 'name' => isset($addressNames[$i]) ? $addressNames[$i] : '', + 'sync_address' => $addressUrls[$i] + ]; + } + } + + // 使用新的格式设置同步地址 + $data['sync_address'] = $syncAddresses; + } + $result = setConfig($data['key'], $data); if ($result) { return $this->renderSuccess('修改成功'); diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 66520c8..481b66e 100755 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -382,7 +382,7 @@ 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)); @@ -747,7 +747,7 @@ class Goods extends Base $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)); @@ -1090,5 +1090,152 @@ 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'] + : []; + + return $this->renderSuccess('获取成功', $syncAddresses); + } + + /** + * 同步盒子数据 + */ + public function sync_goods() + { + $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(); + $goods->async_code = $async_code; + $goods->async_date = date('Y-m-d H:i:s'); + $goods->save(); + } else { + $async_code = $goods['async_code']; + } + + # 获取盒子奖品数据 + $goodsList = \app\common\model\GoodsList::where('goods_id', $goods_id)->select()->toArray(); + + # 准备同步数据 + $syncData = [ + 'goods' => $goods->toArray(), + 'goodsList' => $goodsList, + 'async_code' => $async_code, + 'sync_time' => time() + ]; + + # 发送到所有目标 + $successCount = 0; + $failCount = 0; + $errorMessages = []; + + foreach ($targets as $target) { + $result = $this->sendSyncData($target, $syncData); + if ($result['success']) { + $successCount++; + } else { + $failCount++; + $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) { + return $this->renderSuccess("部分同步成功:{$successCount} 个成功,{$failCount} 个失败。\n失败信息:" . implode("\n", $errorMessages)); + } else { + return $this->renderError("同步失败:" . implode("\n", $errorMessages)); + } + } + + /** + * 生成UUID + */ + private function generateUUID() + { + 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) + ); + } + + /** + * 发送同步数据到目标 + */ + private function sendSyncData($target, $data) + { + try { + # 准备请求数据 + $jsonData = json_encode($data); + + # 初始化CURL + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $target . "/api/goods/receive_sync"); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + '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/route/app.php b/app/admin/route/app.php index 81615c5..3964d6e 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -172,6 +172,8 @@ Route::rule('drawlist_add', 'Draw/drawlist_add', 'GET|POST'); Route::rule('drawlist_edit', 'Draw/drawlist_edit', 'GET|POST'); Route::rule('drawlist_del', 'Draw/drawlist_del', 'GET|POST'); Route::rule('draw_add', 'Draw/draw_add', 'GET|POST'); +Route::rule('get_sync_addresses', 'Goods/get_sync_addresses', 'GET'); +Route::rule('sync_goods', 'Goods/sync_goods', 'POST'); #============================ #盒子扩展 @@ -236,6 +238,7 @@ Route::get('base', 'Config/base'); Route::get('sign', 'Config/sign');//签到设置 Route::get('weixinpay', 'Config/weixinpay'); Route::get('uploadsFile', 'Config/uploads'); //上传设置 +Route::get('systemconfig', 'Config/systemconfig'); //系统设置 Route::post('update', 'Config/update'); Route::get('wechatofficialaccount', 'Config/wechatofficialaccount'); diff --git a/app/admin/view/Config/systemconfig.html b/app/admin/view/Config/systemconfig.html new file mode 100644 index 0000000..d4d1bb5 --- /dev/null +++ b/app/admin/view/Config/systemconfig.html @@ -0,0 +1,128 @@ +{include file="Public:header2"/} + + +
+
+
+
+
系统设置
+
+
+ + +
+ +
+
+ {if isset($data.sync_address) && is_array($data.sync_address)} + {foreach $data.sync_address as $index => $address} +
+
+ +
+
+ +
+ +
+ {/foreach} + {else} +
+
+ +
+
+ +
+ +
+ {/if} +
+
+ +
+
+
+ +
+
+ + +
+
+
+
+
+
+
+
+ + {include file="Public:footer"/} + + + + + + \ No newline at end of file diff --git a/app/admin/view/Goods/goods.html b/app/admin/view/Goods/goods.html index fcb2304..ef2bcec 100755 --- a/app/admin/view/Goods/goods.html +++ b/app/admin/view/Goods/goods.html @@ -1,220 +1,237 @@ {include file="Public:header2"/} + -
-
-
-
-
-
- +
+
+ +
+
+
+ +
-
-
-
- +
+
+ +
+
+
+
+ +
+
+
+
-
-
- -
-
-
- -
-
- + + +
+ +
+ 添加盒子 + 共有数据: {$count}条 +
+
+ + + + + + + + + + + + + + + + + + + + + + + {volist name="list" id="vo"} + + + + + {/if} + + +
盒子ID盒子类型盒子名称盒子价格盒子图片盒子详情图片盒子套数锁箱模式首页显示 + 擂台赏
抽全局赏数量 +
排序状态添加时间操作
+ {if condition="$vo['type'] eq 1"} +
+ {$vo['id']} +
+ {elseif condition="$vo['type'] eq 11"} +
+ {$vo['id']} +
+ + {else } -
- -
- 添加盒子 - 共有数据: {$count}条 -
-
- - - - - - - - - - - - - - - - - - - - - - - {volist name="list" id="vo"} - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - + + - - {/volist} + +
+ + 删除 + + + + {/volist} - {if condition="empty($list)"} - - {/if} - - -
盒子ID盒子类型盒子名称盒子价格盒子图片盒子详情图片盒子套数锁箱模式首页显示 - 擂台赏
抽全局赏数量 -
排序状态添加时间操作
- {if condition="$vo['type'] eq 1"} -
{$vo['id']} -
- {elseif condition="$vo['type'] eq 11"} -
- {$vo['id']} -
- - {else } - - {$vo['id']} - {/if} -
- {if condition="$vo['type'] eq 1"} - - {elseif condition="$vo['type'] eq 2"} - - {elseif condition="$vo['type'] eq 3"} - - {elseif condition="$vo['type'] eq 5"} - - {elseif condition="$vo['type'] eq 6"} - - {elseif condition="$vo['type'] eq 8"} - - {elseif condition="$vo['type'] eq 9"} - - {elseif condition="$vo['type'] eq 10"} - - {elseif condition="$vo['type'] eq 11"} - - {/if} - {$vo['title']}{$vo['price']}{if $vo['imgurl_detail']}{/if}{if $vo['stock'] gt 0}{$vo['stock']}{/if} - {if condition="$vo['lock_is'] eq 1"} - -
- - {/if} -
- {if $vo['show_is'] eq 1} - - {/if} - {if $vo['prize_num'] gt 0}{$vo['prize_num']}{/if}{$vo['sort']} - {if $vo['status'] eq 1} - - - {elseif $vo['status'] eq 2} - - - {elseif $vo['status'] eq 3} + {/if} + + {if condition="$vo['type'] eq 1"} + + {elseif condition="$vo['type'] eq 2"} + + {elseif condition="$vo['type'] eq 3"} + + {elseif condition="$vo['type'] eq 5"} + + {elseif condition="$vo['type'] eq 6"} + + {elseif condition="$vo['type'] eq 8"} + + {elseif condition="$vo['type'] eq 9"} + + {elseif condition="$vo['type'] eq 10"} + + {elseif condition="$vo['type'] eq 11"} + + {/if} + {$vo['title']}{$vo['price']} + {if $vo['imgurl_detail']}{/if}{if $vo['stock'] gt 0}{$vo['stock']}{/if} + {if condition="$vo['lock_is'] eq 1"} + +
+ + {/if} +
+ {if $vo['show_is'] eq 1} + + {/if} + {if $vo['prize_num'] gt 0}{$vo['prize_num']}{/if}{$vo['sort']} + {if $vo['status'] eq 1} + + + {elseif $vo['status'] eq 2} + + + {elseif $vo['status'] eq 3} - {/if} - {$vo['addtime']|date="Y-m-d H:i"} - - 编辑 - -
- 奖品 -
- - - + {/if} +
{$vo['addtime']|date="Y-m-d H:i"} + + 编辑 + +
+ 奖品 +
+ 同步 + + + - -
- - 删除 - -
暂时没有数据!
-
- {$page|raw} + {if condition="empty($list)"} +
暂时没有数据!
+
+ {$page|raw} +
-
-{include file="Public:footer"/} - + + + + + + + - - + \ No newline at end of file diff --git a/app/api/controller/Goods.php b/app/api/controller/Goods.php index 2551a72..392357b 100755 --- a/app/api/controller/Goods.php +++ b/app/api/controller/Goods.php @@ -1232,5 +1232,76 @@ class Goods extends Base return $this->renderSuccess("操作成功", $user_ids); } + + /** + * 接收盒子同步数据 + */ + public function receive_sync() + { + // 获取POST的JSON数据 + $input = file_get_contents('php://input'); + $data = json_decode($input, true); + + if (!$data || !isset($data['goods']) || !isset($data['goodsList']) || !isset($data['async_code'])) { + return $this->renderError('无效的同步数据'); + } + + // 开始事务 + Db::startTrans(); + try { + $goodsData = $data['goods']; + $goodsListData = $data['goodsList']; + $async_code = $data['async_code']; + + // 检查async_code是否存在 + $existingGoods = Goodsmodel::where('async_code', $async_code)->find(); + + // 准备商品数据 + $goods = isset($goodsData['id']) ? unset($goodsData['id']) : $goodsData; + + if ($existingGoods) { + // 更新现有商品 + $goodsId = $existingGoods->id; + + // 更新商品数据 + $goods['async_date'] = date('Y-m-d H:i:s'); + $existingGoods->save($goods); + + // 删除现有的商品列表数据 + GoodsList::where('goods_id', $goodsId)->delete(); + } else { + // 创建新商品 + $goods['async_date'] = date('Y-m-d H:i:s'); + $goods['addtime'] = time(); + $goods['update_time'] = time(); + $goodsModel = new Goodsmodel(); + $goodsModel->save($goods); + $goodsId = $goodsModel->id; + } + + // 处理商品列表数据 + foreach ($goodsListData as $listItem) { + // 处理商品列表项 + if (isset($listItem['id'])) { + unset($listItem['id']); + } + + $listItem['goods_id'] = $goodsId; + + // 创建新的商品列表项 + $goodsListModel = new GoodsList(); + $goodsListModel->save($listItem); + } + + // 提交事务 + Db::commit(); + + return $this->renderSuccess('同步成功'); + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); + return $this->renderError('同步失败: ' . $e->getMessage()); + } + } } diff --git a/app/api/route/app.php b/app/api/route/app.php index 4668a08..a017703 100755 --- a/app/api/route/app.php +++ b/app/api/route/app.php @@ -163,4 +163,7 @@ Route::any('generate_urllinks','Index/generate_urllinks'); // Route::any('mall_ordermoney', 'Mall/mall_ordermoney'); -Route::any('mall_orderbuy', 'Mall/mall_orderbuy'); \ No newline at end of file +Route::any('mall_orderbuy', 'Mall/mall_orderbuy'); + +// 添加API路由 +Route::rule('goods/receive_sync', 'Goods/receive_sync', 'POST'); \ No newline at end of file diff --git a/config/menu.php b/config/menu.php index 1c83981..109b588 100755 --- a/config/menu.php +++ b/config/menu.php @@ -223,6 +223,10 @@ return [ 'url' => '/admin/card_shang', 'name' => '抽奖等级设置', ], + [ + 'url' => '/admin/systemconfig', + 'name' => '系统设置', + ], ], ],