Browse Source

feat(admin): 实现媒体圈资源同步功能

- 新增 ApiController 控制器用于调用媒体圈接口
- 创建 ApiService 服务类处理资源同步逻辑
- 实现网媒资源数据封装与存储
- 添加 AES 加密与签名验证机制
- 支持分页获取并递归同步多页数据
- 创建 OnlineMedias 模型与 DAO 数据访问层
- 调整代理端路由分组路径及结构
master
zhangf@suq.cn 23 hours ago
parent
commit
f4725f9bbf
  1. 5
      app/controller/admin/ApiController.php
  2. 15
      app/dao/admin/OnlineMediasDao.php
  3. 33
      app/model/admin/OnlineMedias.php
  4. 8
      app/route/proxy.php
  5. 125
      app/service/admin/ApiService.php
  6. 43
      app/service/meijianquan/ApiService.php

5
app/controller/admin/ApiController.php

@ -1,9 +1,12 @@
<?php
namespace app\controller\admin;
use app\service\meijianquan\ApiService;
use app\service\admin\ApiService;
use support\Response;
/**
* 调用媒体圈接口控制器
*/
class ApiController
{
public function syncResource(ApiService $service):Response

15
app/dao/admin/OnlineMediasDao.php

@ -0,0 +1,15 @@
<?php
namespace app\dao\admin;
use app\model\admin\OnlineMedias;
use plugin\piadmin\app\base\BaseModel;
class OnlineMediasDao extends BaseModel
{
protected function setModel(): string
{
return OnlineMedias::class;
}
}

33
app/model/admin/OnlineMedias.php

@ -0,0 +1,33 @@
<?php
namespace app\model\admin;
use plugin\piadmin\app\base\BaseModel;
/**
* AI创作指令模型
*/
class OnlineMedias extends BaseModel
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'geo_online_medias';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
}

8
app/route/proxy.php

@ -6,7 +6,13 @@ use Webman\Route;
/**
* 代理端后台定制路由
*/
Route::group('/service/v1', function () {
Route::group('/service/v1/proxy', function () {
//代理组织架构
//代理管理用户
Route::group('user', function () {
});
})->middleware([

125
app/service/admin/ApiService.php

@ -0,0 +1,125 @@
<?php
namespace app\service\admin;
use app\dao\admin\OnlineMediasDao;
use plugin\piadmin\app\base\BaseService;
use plugin\piadmin\app\exception\ApiException;
use support\Db;
use think\Exception;
class ApiService extends BaseService
{
//同步资源接口
protected $url = 'http://www.meijiequan.cn/api/companyResource/syncResource';
protected $appkey = 'de06270f3515bad109aa1fd2c023dd41';
protected $dao;
public function __construct()
{
$this->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']
];
}
}

43
app/service/meijianquan/ApiService.php

@ -1,43 +0,0 @@
<?php
namespace app\service\meijianquan;
use plugin\piadmin\app\base\BaseService;
class ApiService extends BaseService
{
public function syncResource()
{
$url = "http://www.meijiequan.cn/api/companyResource/syncResource";
$data = array (
"resource_id"=>"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;
}
}
Loading…
Cancel
Save