From f4725f9bbfab2678414c0fb45ecb7dadb1258852 Mon Sep 17 00:00:00 2001 From: "zhangf@suq.cn" Date: Tue, 16 Dec 2025 10:43:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E5=AE=9E=E7=8E=B0=E5=AA=92?= =?UTF-8?q?=E4=BD=93=E5=9C=88=E8=B5=84=E6=BA=90=E5=90=8C=E6=AD=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 ApiController 控制器用于调用媒体圈接口 - 创建 ApiService 服务类处理资源同步逻辑 - 实现网媒资源数据封装与存储 - 添加 AES 加密与签名验证机制 - 支持分页获取并递归同步多页数据 - 创建 OnlineMedias 模型与 DAO 数据访问层 - 调整代理端路由分组路径及结构 --- app/controller/admin/ApiController.php | 5 +- app/dao/admin/OnlineMediasDao.php | 15 ++++ app/model/admin/OnlineMedias.php | 33 +++++++++ app/route/proxy.php | 8 ++- app/service/admin/ApiService.php | 125 +++++++++++++++++++++++++++++++++ app/service/meijianquan/ApiService.php | 43 ------------ 6 files changed, 184 insertions(+), 45 deletions(-) create mode 100644 app/dao/admin/OnlineMediasDao.php create mode 100644 app/model/admin/OnlineMedias.php create mode 100644 app/service/admin/ApiService.php delete mode 100644 app/service/meijianquan/ApiService.php diff --git a/app/controller/admin/ApiController.php b/app/controller/admin/ApiController.php index 4016184..e4103f9 100644 --- a/app/controller/admin/ApiController.php +++ b/app/controller/admin/ApiController.php @@ -1,9 +1,12 @@ middleware([ diff --git a/app/service/admin/ApiService.php b/app/service/admin/ApiService.php new file mode 100644 index 0000000..8c12ba9 --- /dev/null +++ b/app/service/admin/ApiService.php @@ -0,0 +1,125 @@ +dao = app()->make(OnlineMediasDao::class); + } + + /** + * 网媒 ID: 35075 资源名: 网媒测试GEO + * 自媒体 ID: 90063 资源名: 自媒测试GEO + * @param $nexturl + * @param $type + * @throws \Random\RandomException + */ + public function syncResource($type = 35075, $resourceData = [], $nexturl = null) + { + $url = $nexturl ?? $this->url; + $requestData = array ( + "resource_id"=>$type, + "model_id"=>"1", + "limit"=>"300", + "page"=>"1" + ); + //加密 + $data_info = serialize($requestData); + $time_out = 60; + $iv = random_bytes(16); + $info['iv'] = base64_encode($iv); + $info['value'] = openssl_encrypt($data_info, 'AES-256-CBC', $this->appkey, 0, + base64_decode($info['iv'])); + $encrypt = base64_encode(json_encode($info)); + $sign = md5($encrypt); + $result = ['response' => $encrypt, 'sign' => $sign, 'app_key' => $this->appkey]; + //curl + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); + curl_setopt($ch, CURLOPT_TIMEOUT, $time_out); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $result); + $output = curl_exec($ch); + curl_close($ch); + $result = json_decode($output, true); + if ($result['errorCode'] == '') { + //封装当前数据 + foreach ($result['data']['data'] as $item) { + //网媒 + if ($type == '35075') { + //封装数据 + $resourceData[] = [ + 'id' => $item['id'], + 'model_id' => $item['model_id'], + 'name' => $item['name'], + 'price_assign' => $item['price_assign'], + 'pindaoleixing' => $item['pindaoleixing'], + 'zonghemenhu' => $item['zonghemenhu'], + 'quyu' => $item['quyu'], + 'meitianli' => $item['meitianli'], + 'lianjieleixing' => $item['lianjieleixing'], + 'shouluxiaoguo' => $item['shouluxiaoguo'], + 'weekend_active' => $item['weekend_active'], + 'meitiquanzhong' => $item['meitiquanzhong'], + 'tebiehangye' => $item['tebiehangye'], + 'jiegaoshijian' => $item['jiegaoshijian'], + 'baikelaiyuan' => $item['baikelaiyuan'], + 'wanshangkechu' => $item['wanshangkechu'], + 'kedaishipin' => $item['kedaishipin'], + 'jierikefa' => $item['jierikefa'], + 'anlijietu' => $item['anlijietu'], + 'no_disclaimer' => $item['no_disclaimer'], + 'can_sign' => $item['can_sign'], + 'wechat_open' => $item['wechat_open'], + 'has_read_count' => $item['has_read_count'], + 'avg_delivery_speed' => $item['avg_delivery_speed'], + 'publish_rate' => $item['publish_rate'], + 'ai_include' => $item['ai_include'], + 'meitibeizhu' => $item['meitibeizhu'], + 'status' => $item['status'], + ]; + } else if ($type == '90063') { + //自媒体 + throw new \Exception('暂不支持'); + } else { + throw new \Exception('未知资源类型'); + } + } + Db::beginTransaction(); + try { + //保存当前页码数据 + $this->dao->insertAll($resourceData); + Db::commit(); + } catch (Exception $exception) { + Db::rollBack(); + throw new ApiException($exception->getMessage()); + } + if (!empty($result['data']['next_url'])) { + //获取下一页数据 + $this->syncResource($type, [], $result['data']['next_url']); + } + } + return [ + 'code' => $result['errorCode'], + 'msg' => $result['message'], + 't' => $result['t'] + ]; + } +} \ No newline at end of file diff --git a/app/service/meijianquan/ApiService.php b/app/service/meijianquan/ApiService.php deleted file mode 100644 index 80d6b46..0000000 --- a/app/service/meijianquan/ApiService.php +++ /dev/null @@ -1,43 +0,0 @@ -"35075", - "model_id"=>"1", - "limit"=>"300", - "page"=>"1" - ); - //加密 - $app_key = "de06270f3515bad109aa1fd2c023dd41"; - $data_info = serialize($data ); - $time_out = 60; - $iv = random_bytes(16); - $info['iv'] = base64_encode($iv); - $info['value'] = openssl_encrypt($data_info, 'AES-256-CBC', $app_key, 0, - base64_decode($info['iv'])); - $encrypt = base64_encode(json_encode($info)); - $sign = md5($encrypt); - $result = ['response' => $encrypt, 'sign' => $sign, 'app_key' => $app_key]; - //curl - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); - curl_setopt($ch, CURLOPT_TIMEOUT, $time_out); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, $result); - $output = curl_exec($ch); - curl_close($ch); - return $output; - } -} \ No newline at end of file