Files
xianyan/docs/toolsapi/application/api/controller/Batch.php
Developer b6441a8919 api
2026-04-27 23:47:18 +08:00

107 lines
3.9 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;
use QL\QueryList;
/**
* 示例接口
*/
class Batch extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['*'];
// 无需鉴权的接口,*表示全部
protected $noNeedRight = ['*'];
public function test()
{
$db = Db::name('word')->where('wlsy',null)->orderRaw('rand()')->field('id,word')->find();
$url = 'https://danci.bi0.cn/'.$db['word'].'.html';
$data = QueryList::get($url, null, [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'Accept-Encoding' => 'gzip, deflate, br',
]
])->rules([
'wlsy' => ['.box__bd:eq(0) > .text__bd', 'text'],
])->queryData();
if(!empty($data)){
$res = Db::name('word')->where('id',$db['id'])->update($data);
if($res){
$this->success('成功',$db['word']);
}
$this->error('失败',$db['word']);
}else{
$this->error('未采集到数据',$db['word']);
}
}
public function daoru($calss)
{
if(empty($calss) || !isset($calss)){
$this->error('参数错误');
}
// 读取JSON文件内容为字符串
$dataArray = json_decode(file_get_contents('./file.json'), true);
try {
// 循环处理每个数组元素,将数据插入到数据库中
foreach ($dataArray as $data) {
$check = Db::name('story')->where('title',trim($data['title']))->value('id');
if(!$check && !empty(trim($data['content'])) && strlen(trim($data['content'])) > 240){
Db::name('story')->insert([
'title' => trim($data['title']),
'content' => trim($data['content']),
'category_id' => (int)$calss,
'updatetime' => time(),
'createtime' => time(),
]);
}
}
$this->success('成功');
} catch (\Exception $e) {
// 处理异常,例如记录日志或者给出错误提示
$this->error('导入过程中出现异常', $e->getMessage());
}
}
public function gushi()
{
$data = Db::name('story')->where('content','like','%style%')->limit(500)->field('id, content')->select();
if(empty($data)){
$this->error('没有了');
}
foreach ($data as $v) {
$content = preg_replace('/(class|style)="[^"]*"/i', '', $v['content']);
DB::name('story')->where('id', $v['id'])->update(['content' => trim($content)]);
}
$this->success('修改完成',$data);
}
public function testA()
{
$data = '<p>&#21521;&#38451;&#30340;&#23665;&#22369';
return html_entity_decode($data, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
// public function ipLookup($ip)
// {
// $url = 'http://ip-api.com/json/' . $ip.'?lang=zh-CN';
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// $response = curl_exec($ch);
// $ipInfo = [];
// if ($response) {
// $data = json_decode($response, true);
// if ($data && $data['status'] == 'success') {
// $ipInfo['ip'] = $data['query'];
// $ipInfo['country'] = $data['country'];
// $ipInfo['region'] = $data['regionName'];
// $ipInfo['city'] = $data['city'];
// $ipInfo['isp'] = $data['isp'];
// }
// }
// curl_close($ch);
// $this->success('成功',$ipInfo);
// }
}