CollectorController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\JsonRpc\CollectorServiceInterface;
  5. use App\Tools\PublicData;
  6. use Hyperf\Di\Annotation\Inject;
  7. use Hyperf\HttpServer\Annotation\AutoController;
  8. use App\Tools\Result;
  9. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  10. use App\Constants\ErrorCode;
  11. use Hyperf\Context\Context;
  12. //use App\Service\GatherQueueService;
  13. use App\Service\ImportQueueService;
  14. /**
  15. * ClassCollectorController
  16. * @package App\Controller
  17. */
  18. class CollectorController extends AbstractController
  19. {
  20. #[Inject]
  21. protected ValidatorFactoryInterface $validationFactory;
  22. // #[Inject]
  23. // protected GatherQueueService $Gservice;
  24. #[Inject]
  25. protected ImportQueueService $Iservice;
  26. /**
  27. * @var CollectorServiceInterface
  28. */
  29. #[Inject]
  30. private $collectorServiceClient;
  31. /**
  32. * 添加网站
  33. * @return array
  34. */
  35. public function addWeb()
  36. {
  37. $requireData = $this->request->all();
  38. $validator = $this->validationFactory->make(
  39. $requireData,
  40. [
  41. 'name'=> 'required',
  42. 'url'=> 'required',
  43. ],
  44. [
  45. 'name.required' => '网站名称不能为空',
  46. 'url.required' => '网站地址不能为空',
  47. ]
  48. );
  49. if ($validator->fails()) {
  50. $errorMessage = $validator->errors()->first();
  51. return Result::error($errorMessage);
  52. }
  53. $result = $this->collectorServiceClient->addWeb($requireData);
  54. if ($result['code'] != ErrorCode::SUCCESS) {
  55. return Result::error($result['message'],0,[]);
  56. }
  57. return Result::success($result['data']);
  58. }
  59. /**
  60. * 获取网站
  61. * @return array
  62. */
  63. public function getWeb()
  64. {
  65. $requireData = $this->request->all();
  66. $validator = $this->validationFactory->make(
  67. $requireData,
  68. [
  69. 'page'=> 'required',
  70. 'pageSize'=> 'required',
  71. ],
  72. [
  73. 'page.required' => '第几页不能为空',
  74. 'pageSize.required' => '每页显示条数不能为空',
  75. ]
  76. );
  77. if(isset($requireData['keyWord'])){
  78. $validator = $this->validationFactory->make(
  79. $requireData,
  80. [
  81. 'keyWord'=> 'required'
  82. ],
  83. [
  84. 'keyWord.required' => '搜索词不能为空'
  85. ]
  86. );
  87. }
  88. if ($validator->fails()) {
  89. $errorMessage = $validator->errors()->first();
  90. return Result::error($errorMessage);
  91. }
  92. $result = $this->collectorServiceClient->getWeb($requireData);
  93. if ($result['code'] != ErrorCode::SUCCESS) {
  94. return Result::error($result['message'],0,[]);
  95. }
  96. return Result::success($result['data']);
  97. }
  98. /**
  99. * 采集动作
  100. * @return array
  101. */
  102. public function sendCrawler()
  103. {
  104. $data = [];
  105. $data['admin_user_id'] = Context::get("UserId");
  106. $data['time'] = microtime();
  107. $requireData = $this->request->all();
  108. $validator = $this->validationFactory->make(
  109. $requireData,
  110. [
  111. 'id'=> 'required',
  112. ],
  113. [
  114. 'id.required' => '规则id不能为空',
  115. ]
  116. );
  117. if ($validator->fails()) {
  118. $errorMessage = $validator->errors()->first();
  119. return Result::error($errorMessage);
  120. }
  121. // $b = $this->Iservice->push($data,1);
  122. $data['id'] = $requireData['id'];
  123. var_dump("发送数据:",$data);
  124. $result = $this->collectorServiceClient->sendCrawler($data);
  125. return $result['code']==200?Result::success($result['data']):Result::error('采集失败');
  126. }
  127. /**
  128. * 发送请求
  129. * @param $data
  130. * @return array
  131. */
  132. // public function goSendCrawler($data=[])
  133. // {
  134. // var_dump("我要开始采集了!!!");
  135. // $this->collectorServiceClient->sendCrawler($data);
  136. // return Result::success([]);
  137. // }
  138. public function zhipu()
  139. {
  140. $requireData = $this->request->all();
  141. // 接口URL
  142. $apiUrl = 'https://open.bigmodel.cn/api/paas/v4/chat/completions';
  143. // API密钥
  144. $apiKey = 'be1856920c54ac537b530d69bc2eda73.gOO2BMq9NXavzEMq';
  145. // 请求参数
  146. $params = [
  147. "model"=>"glm-4",
  148. "messages"=>[
  149. [
  150. "role"=>"user",
  151. "content"=>$requireData['message']
  152. ]
  153. ]
  154. ];
  155. // 构建请求头部
  156. $headers = [
  157. 'Content-Type: application/json',
  158. 'Authorization: Bearer ' . $apiKey
  159. ];
  160. // 将参数转换为JSON格式
  161. $jsonParams = json_encode($params);
  162. var_dump("看看:",$jsonParams);
  163. // 初始化cURL会话
  164. $ch = curl_init($apiUrl);
  165. // 设置cURL选项
  166. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  167. curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
  168. curl_setopt($ch, CURLOPT_POST, true);
  169. curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonParams);
  170. // 执行cURL会话
  171. $response = curl_exec($ch);
  172. // 检查是否有错误发生
  173. if (curl_errno($ch)) {
  174. echo 'cURL error: ' . curl_error($ch);
  175. } else {
  176. // 处理响应
  177. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  178. if ($httpCode == 200) {
  179. // 解码JSON响应
  180. $responseData = json_decode($response, true);
  181. Result::success($responseData);
  182. } else {
  183. echo "HTTP error: $httpCode\n";
  184. echo "Response: $response\n";
  185. }
  186. }
  187. // 关闭cURL会话
  188. curl_close($ch);
  189. }
  190. }