From a5424617650be2d8b334c4607f87fa148c8f7764 Mon Sep 17 00:00:00 2001 From: "zhangf@suq.cn" Date: Tue, 16 Dec 2025 13:26:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E6=96=B0=E5=A2=9E=E7=BD=91?= =?UTF-8?q?=E5=AA=92=E9=85=8D=E7=BD=AE=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加网媒配置控制器、服务类和数据访问对象 - 实现配置项的增删改查接口 - 配置路由组并应用权限中间件 - 支持系统配置项保护机制 - 优化媒体数据同步逻辑,支持分页处理 - 增加自媒体数据结构与操作支持 - 调整数据库模型继承关系以提高代码复用性 - 扩展API服务能力,适配多种媒体类型同步需求 --- .../admin/OnlineMediasConfigController.php | 60 ++++++++++ app/dao/admin/OnlineMediasConfigDao.php | 15 +++ app/dao/admin/OnlineMediasDao.php | 4 +- app/dao/admin/SelfMediasDao.php | 15 +++ app/model/admin/OnlineMediasConfig.php | 33 ++++++ app/model/admin/SelfMedias.php | 33 ++++++ app/route/admin.php | 20 +++- app/service/admin/ApiService.php | 127 +++++++++++++-------- app/service/admin/OnlineMediasConfigService.php | 106 +++++++++++++++++ 9 files changed, 365 insertions(+), 48 deletions(-) create mode 100644 app/controller/admin/OnlineMediasConfigController.php create mode 100644 app/dao/admin/OnlineMediasConfigDao.php create mode 100644 app/dao/admin/SelfMediasDao.php create mode 100644 app/model/admin/OnlineMediasConfig.php create mode 100644 app/model/admin/SelfMedias.php create mode 100644 app/service/admin/OnlineMediasConfigService.php diff --git a/app/controller/admin/OnlineMediasConfigController.php b/app/controller/admin/OnlineMediasConfigController.php new file mode 100644 index 0000000..cb52188 --- /dev/null +++ b/app/controller/admin/OnlineMediasConfigController.php @@ -0,0 +1,60 @@ + 1, + 'code' => '', + 'name' => '', + 'value' => '', + 'pid' => '', + 'status' => 1 + ]); + +// validate(SystemDeptValidate::class)->check($params, 'save'); + + return success($service->saveData($params)); + } + + public function update(OnlineMediasConfigService $service): Response + { + $params = requestOnly([ + 'id' => '', + 'module_id' => 1, + 'code' => '', + 'name' => 1000, + 'value' => '', + 'pid' => '', + 'status' => 1 + ]); + +// validate(SystemDeptValidate::class)->check($params, 'update'); + + return success($service->updateData(ArrayUtils::filterNotEmpty($params))); + } + + public function index(OnlineMediasConfigService $service): Response + { + return success($service->listData()); + } + + public function read(OnlineMediasConfigService $service): Response + { + $id = input('id'); + return success($service->readData($id)); + } + + public function delete(OnlineMediasConfigService $service): Response + { + $ids = input('ids'); + return success($service->deleteData($ids)); + } +} \ No newline at end of file diff --git a/app/dao/admin/OnlineMediasConfigDao.php b/app/dao/admin/OnlineMediasConfigDao.php new file mode 100644 index 0000000..d974277 --- /dev/null +++ b/app/dao/admin/OnlineMediasConfigDao.php @@ -0,0 +1,15 @@ +setParams(['perm' => ['meijiequanSyncResource']]); }); + //网媒选项配置 + Route::group('/onlineMediasConfig', function () { + Route::post('/save', [OnlineMediasConfigController::class, 'save'])->setParams(['perm' => ['onlineMediasConfigSave']]); + Route::post('/update', [OnlineMediasConfigController::class, 'update'])->setParams(['perm' => ['onlineMediasConfigUpdate']]); + Route::get('/index', [OnlineMediasConfigController::class, 'index'])->setParams(['perm' => ['onlineMediasConfigIndex']]); + Route::get('/read', [OnlineMediasConfigController::class, 'read'])->setParams(['perm' => ['onlineMediasConfigRead']]); + Route::post('/delete', [OnlineMediasConfigController::class, 'delete'])->setParams(['perm' => ['onlineMediasConfigDelete']]); + }); + + //媒体圈媒体数据 + Route::group('/medias', function () { + //网媒 +// Route::get('/online', [OnlineMediasCont::class, 'online']); + //自媒体 + }); + })->middleware([ AdminAuthorizationMiddleware::class, -// PermissionsMiddleware::class + PermissionsMiddleware::class ]); \ No newline at end of file diff --git a/app/service/admin/ApiService.php b/app/service/admin/ApiService.php index 8c12ba9..2e728de 100644 --- a/app/service/admin/ApiService.php +++ b/app/service/admin/ApiService.php @@ -3,6 +3,7 @@ namespace app\service\admin; use app\dao\admin\OnlineMediasDao; +use app\dao\admin\SelfMediasDao; use plugin\piadmin\app\base\BaseService; use plugin\piadmin\app\exception\ApiException; use support\Db; @@ -14,11 +15,13 @@ class ApiService extends BaseService protected $url = 'http://www.meijiequan.cn/api/companyResource/syncResource'; protected $appkey = 'de06270f3515bad109aa1fd2c023dd41'; - protected $dao; + protected $onlineMediasDao; + protected $selfMediasDao; public function __construct() { - $this->dao = app()->make(OnlineMediasDao::class); + $this->onlineMediasDao = app()->make(OnlineMediasDao::class); + $this->selfMediasDao = app()->make(SelfMediasDao::class); } /** @@ -28,14 +31,18 @@ class ApiService extends BaseService * @param $type * @throws \Random\RandomException */ - public function syncResource($type = 35075, $resourceData = [], $nexturl = null) + public function syncResource($model_id = 2, $resourceData = [], $nexturl = null, $currentPage = null) { + set_time_limit(-1); + $sum = 0; + $total = 0; + $page = $currentPage ?? 1; $url = $nexturl ?? $this->url; $requestData = array ( - "resource_id"=>$type, - "model_id"=>"1", - "limit"=>"300", - "page"=>"1" +// "resource_id"=>$type, + "model_id"=>$model_id, + "limit"=>"1000", + "page"=>$page ); //加密 $data_info = serialize($requestData); @@ -60,61 +67,91 @@ class ApiService extends BaseService curl_close($ch); $result = json_decode($output, true); if ($result['errorCode'] == '') { + $total = $result['data']['total']; //封装当前数据 foreach ($result['data']['data'] as $item) { //网媒 - if ($type == '35075') { + if ($model_id == 1) { //封装数据 $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'], + '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') { + } else if ($model_id == 2) { //自媒体 - throw new \Exception('暂不支持'); + //封装数据 + $resourceData[] = [ + 'id' => $item['id'] ?? '', + 'model_id' => $item['model_id'] ?? '', + 'name' => $item['name'] ?? '', + 'price_assign' => $item['price_assign'] ?? '', + 'pingtai' => $item['pingtai'] ?? '', + 'hangyefenlei' => $item['hangyefenlei'] ?? '', + 'meitianli' => $item['meitianli'] ?? '', + 'cankaofensishu' => $item['cankaofensishu'] ?? '', + 'huangv' => $item['huangv'] ?? '', + 'meitihao' => $item['meitihao'] ?? '', + 'shipinleixing' => $item['shipinleixing'] ?? '', + 'wanshangkechu' => $item['wanshangkechu'] ?? '', + 'jierikefa' => $item['jierikefa'] ?? '', + 'avg_delivery_speed' => $item['avg_delivery_speed'] ?? '', + 'publish_rate' => $item['publish_rate'] ?? '', + 'meitibeizhu' => $item['meitibeizhu'] ?? '', + 'status' => $item['status'] ?? '', + ]; } else { throw new \Exception('未知资源类型'); } } + $sum += sizeof($resourceData); Db::beginTransaction(); try { //保存当前页码数据 - $this->dao->insertAll($resourceData); + if ($model_id == 1) { + $this->onlineMediasDao->batchInsertOrUpdate($resourceData); + } else if ($model_id == 2) { + $this->selfMediasDao->batchInsertOrUpdate($resourceData); + } else { + throw new \Exception('未知资源类型'); + } 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']); +// Db::rollBack(); +// throw new ApiException($exception->getMessage()); } +// if (!empty($result['data']['next_url'])) { + if ($sum < $total) { + $page = $page + 1; + //获取下一页数据 + $this->syncResource($model_id, [], $this->url . '?page=' . $page, $page); + } +// } } return [ 'code' => $result['errorCode'], diff --git a/app/service/admin/OnlineMediasConfigService.php b/app/service/admin/OnlineMediasConfigService.php new file mode 100644 index 0000000..1b0682f --- /dev/null +++ b/app/service/admin/OnlineMediasConfigService.php @@ -0,0 +1,106 @@ +dao = app()->make(OnlineMediasConfigDao::class); + } + + /** + * 保存数据 + * @param array $params + * @return \plugin\piadmin\app\base\BaseModel + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function saveData(array $params) + { + return $this->dao->save($params); + } + + /** + * 更新数据 + * @param array $params + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function updateData(array $params) + { + $config = $this->dao->get(['id' => $params['id']]); + if (empty($config)) { + throw new ApiException('配置项不存在'); + } + if ($config['is_system'] == 1) { + throw new ApiException('系统配置项不允许修改'); + } + // 落库 + Db::startTrans(); + try { + $this->dao->update(['id' => $config['id']], $params); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return $params; + } + + /** + * 列表数据 + * @param array $params + * @return array + */ + public function listData(): array + { + $query = [ + 'delete_time' => 0 + ]; + $list = $this->dao->selectList($query)->toArray(); + return TreeUtils::toTree($list); + } + + /** + * 详情数据 + * @param mixed $id + * @return array + */ + public function readData(mixed $id): array + { + $config = $this->dao->get(['id' => $id]); + if (empty($config)) { + return []; + } + if ($config['is_system'] == 1) { + throw new ApiException('系统配置项不允许查看'); + } + return $config->toArray(); + } + + public function deleteData($ids) + { + // 落库 + Db::startTrans(); + try { + foreach ($ids as $id) { + $this->dao->update(['id' => $id, 'is_system' => 2], ['delete_time' => time()]); + } + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return ['id' => $ids]; + } + +} \ No newline at end of file