request->post(); $list = SystemConfig::column('value','name'); foreach ($post_data as $key => $value) { if(!is_string($value)){ $value = serialize($value); } if(\in_array($key,$upload_files_config)){ $old_save_name = get_system_config($key); AppUploadFiles::use($value); if($old_save_name != $value){ AppUploadFiles::delete($old_save_name); } } if(isset($list[$key])){ SystemConfig::where('name',$key)->update(['value'=>$value]); }else{ $model_sysconfig = new SystemConfig(); $model_sysconfig->name = $key; $model_sysconfig->value = $value; $model_sysconfig->save(); } $list[$key] = $value; } Cache::set('system_config',$list); return $this->success(); } public function clearCache() { Cache::clear(); return $this->success('清楚成功'); } /** * AI 设置页面. */ public function ai() { return View::fetch(); } /** * 同步 models.dev 数据. * * GET: 返回缓存的供应商列表(不触发远程同步) * POST: 强制从 models.dev 远程同步 */ public function syncModelsDev() { $sync = new \app\common\tools\ai\ModelsDevSync(); if ($this->request->isGet()) { $result = [ 'providers' => $sync->getProviders(), 'models' => [], ]; return json(['code' => 0, 'msg' => 'ok', 'data' => $result]); } try { $result = $sync->syncProviders(); $count = count($result['providers'] ?? []); return json(['code' => 0, 'msg' => "同步成功, 共{$count}个供应商", 'data' => $result]); } catch (\Exception $e) { return json(['code' => 500, 'msg' => '同步失败: ' . $e->getMessage()]); } } /** * 测试AI渠道连接. */ public function testAiChannel() { $providerId = $this->request->param('provider_id', ''); if (empty($providerId)) { return json(['code' => 500, 'msg' => '缺少供应商ID']); } $manager = new \app\common\tools\ai\AiChannelManager(); $provider = $manager->getProvider($providerId); if (!$provider) { return json(['code' => 500, 'msg' => '供应商未配置或缺少API Key']); } $ok = $provider->testConnection(); return json(['code' => $ok ? 0 : 500, 'msg' => $ok ? '连接成功' : '连接失败,请检查配置']); } /** * 获取指定供应商的模型列表. */ public function getModelsByProvider() { $providerId = $this->request->param('provider_id', ''); $sync = new \app\common\tools\ai\ModelsDevSync(); $models = $sync->getModelsByProvider($providerId); return json(['code' => 0, 'data' => $models]); } /** * 获取 models.dev 缓存状态. */ public function getAiCacheStatus() { $sync = new \app\common\tools\ai\ModelsDevSync(); $status = $sync->getCacheStatus(); return json(['code' => 0, 'data' => $status]); } /** * 保存AI渠道配置. */ public function saveAiChannel() { $data = $this->request->post(); $manager = new \app\common\tools\ai\AiChannelManager(); $ok = $manager->saveChannelConfig($data); return json(['code' => $ok ? 0 : 500, 'msg' => $ok ? '保存成功' : '保存失败']); } /** * 删除AI渠道配置. */ public function deleteAiChannel() { $providerId = $this->request->param('provider_id', ''); if (empty($providerId)) { return json(['code' => 500, 'msg' => '缺少供应商ID']); } $manager = new \app\common\tools\ai\AiChannelManager(); $ok = $manager->deleteChannelConfig($providerId); return json(['code' => $ok ? 0 : 500, 'msg' => $ok ? '删除成功' : '删除失败']); } }