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"/} + +
+