You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.0 KiB
125 lines
4.0 KiB
<?php
|
|
|
|
namespace app\home\controller;
|
|
|
|
use app\model\UserChatLinkModel;
|
|
use Goutte\Client;
|
|
use think\facade\Cache;
|
|
use think\facade\Log;
|
|
use think\Request;
|
|
use think\response\Json;
|
|
|
|
class News extends HomeBaseController
|
|
{
|
|
public function index(): Json
|
|
{
|
|
$key="NEWS:LIST";
|
|
$result=Cache::store('redis')->get($key);
|
|
if(empty($result)){
|
|
$client=new Client();
|
|
// 访问一个网页
|
|
$crawler = $client->request('GET', 'https://www.nikkei.com/news/category/markets/');
|
|
// 提取所有的段落
|
|
$paragraphs = $crawler->filter('li.cardListItem_cq2exak a')->each(function ($node) {
|
|
$url="https://www.nikkei.com".str_ireplace('https://www.nikkei.com','',$node->attr('href'));
|
|
$img_obj=$node->filter('div.image_i66x7bx img');
|
|
$img_url="";
|
|
if($img_obj->count()>0){
|
|
$img_url=$node->filter('div.image_i66x7bx img')->attr('src');
|
|
}
|
|
if(!empty($img_url)){
|
|
return [
|
|
'title'=>$node->filter('span.articleTitle_axocstj')->text(),
|
|
'img_url'=>$img_url,
|
|
'href'=>str_ireplace('\\','',$url)
|
|
];
|
|
}
|
|
});
|
|
$paragraphs=array_filter($paragraphs);
|
|
$titles = array_flip(array_column($paragraphs, 'title'));
|
|
$result = [];
|
|
foreach ($paragraphs as $item) {
|
|
if (isset($titles[$item['title']])) {
|
|
$result[] = $item;
|
|
unset($titles[$item['title']]); //Remove from unique IDs to ensure only first is kept.
|
|
}
|
|
}
|
|
$paragraphs=$result;
|
|
Cache::store('redis')->set($key,$paragraphs,300);
|
|
}else{
|
|
$paragraphs=$result;
|
|
}
|
|
|
|
return json([
|
|
'code' => '0',
|
|
'message' => 'Request successful.',
|
|
'data' => $paragraphs,
|
|
]);
|
|
|
|
}
|
|
public function test(): Json
|
|
{
|
|
$client=new Client();
|
|
// 访问一个网页
|
|
$crawler = $client->request('GET', 'https://www.nikkei.com/news/category/markets/');
|
|
// 提取所有的段落
|
|
$paragraphs = $crawler->filter('li.cardListItem_cq2exak a')->each(function ($node) {
|
|
$url="https://www.nikkei.com".str_ireplace('https://www.nikkei.com','',$node->attr('href'));
|
|
$img_obj=$node->filter('div.image_i66x7bx img');
|
|
$img_url="";
|
|
if($img_obj->count()>0){
|
|
$img_url=$node->filter('div.image_i66x7bx img')->attr('src');
|
|
}
|
|
if(!empty($img_url)){
|
|
return [
|
|
'title'=>$node->filter('span.articleTitle_axocstj')->text(),
|
|
'img_url'=>$img_url,
|
|
'href'=>str_ireplace('\\','',$url)
|
|
];
|
|
}
|
|
});
|
|
|
|
|
|
return json([
|
|
'code' => '0',
|
|
'message' => 'Request successful.',
|
|
'data' => array_filter($paragraphs),
|
|
]);
|
|
|
|
}
|
|
|
|
/**
|
|
* @throws \Exception
|
|
*/
|
|
public function testApi()
|
|
{
|
|
$chatData = [
|
|
'Username' => "ccc333519",
|
|
'Password' => '123456',
|
|
];
|
|
$chatUrl = env('CHAT_SERVER.BASE_URL') . '/user/register';
|
|
$res = (new \app\utility\RequestChatServer())->ReqChatServer($chatUrl, $chatData);
|
|
if (!isset($res['data']['uuid'])) {
|
|
return json([
|
|
'code' => '-1',
|
|
'message' => '注册聊天账号失败',
|
|
]);
|
|
}
|
|
|
|
//将客服账号与聊天账号关联
|
|
UserChatLinkModel::create([
|
|
'user_id' => 999,
|
|
'user_type' => 1,
|
|
'chat_uuid' => $res['data']['uuid'],
|
|
]);
|
|
|
|
return json([
|
|
'code' => '0',
|
|
'message' => 'successful',
|
|
'data' => $res,
|
|
'uuid' => $res['data']['uuid']
|
|
]);
|
|
}
|
|
|
|
|
|
}
|