122 lines
3.7 KiB
PHP
122 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use app\common\controller\Backend;
|
|
use think\Db;
|
|
|
|
/**
|
|
* 一言管理
|
|
* @date 2026-04-27
|
|
* @desc 管理一言句子库,支持从官方源更新
|
|
* @icon fa fa-quote-left
|
|
*/
|
|
class Hitokoto extends Backend
|
|
{
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = Db::name('hitokoto');
|
|
}
|
|
|
|
/**
|
|
* 句子列表
|
|
*/
|
|
public function index()
|
|
{
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
if ($this->request->isAjax()) {
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
$list = $this->model
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->paginate($limit);
|
|
$result = array("total" => $list->total(), "rows" => $list->items());
|
|
return json($result);
|
|
}
|
|
$stats = $this->model->group('type,type_name')
|
|
->field('type,type_name,count(*) as cnt')
|
|
->order('cnt desc')
|
|
->select();
|
|
$total = $this->model->count();
|
|
$this->view->assign('stats', $stats);
|
|
$this->view->assign('total', $total);
|
|
return $this->view->fetch();
|
|
}
|
|
|
|
/**
|
|
* 从官方源更新句子
|
|
*/
|
|
public function update_from_source()
|
|
{
|
|
if (!$this->request->isPost()) {
|
|
$this->error('请求方式错误');
|
|
}
|
|
set_time_limit(300);
|
|
|
|
$categories = [
|
|
'a' => '动画', 'b' => '漫画', 'c' => '游戏', 'd' => '文学',
|
|
'e' => '原创', 'f' => '来自网络', 'g' => '其他', 'h' => '影视',
|
|
'i' => '诗词', 'j' => '网易云', 'k' => '哲学', 'l' => '抖机灵',
|
|
];
|
|
|
|
$total_new = 0;
|
|
$total_dup = 0;
|
|
$now = time();
|
|
|
|
foreach ($categories as $code => $name) {
|
|
$url = "https://cdn.jsdelivr.net/gh/hitokoto-osc/sentences-bundle@latest/sentences/{$code}.json";
|
|
$json = @file_get_contents($url);
|
|
if ($json === false) {
|
|
continue;
|
|
}
|
|
$data = json_decode($json, true);
|
|
if (!is_array($data)) {
|
|
continue;
|
|
}
|
|
foreach ($data as $item) {
|
|
$uuid = isset($item['uuid']) ? $item['uuid'] : '';
|
|
$exists = Db::name('hitokoto')->where('uuid', $uuid)->find();
|
|
if ($exists) {
|
|
$total_dup++;
|
|
continue;
|
|
}
|
|
Db::name('hitokoto')->insert([
|
|
'uuid' => $uuid,
|
|
'hitokoto' => isset($item['hitokoto']) ? $item['hitokoto'] : '',
|
|
'type' => $code,
|
|
'type_name' => $name,
|
|
'from_source'=> isset($item['from']) ? mb_substr($item['from'], 0, 200) : '',
|
|
'from_who' => isset($item['from_who']) ? mb_substr($item['from_who'], 0, 200) : '',
|
|
'created_at' => isset($item['created_at']) ? intval($item['created_at']) : 0,
|
|
'local_time' => $now,
|
|
'switch' => 1,
|
|
]);
|
|
$total_new++;
|
|
}
|
|
}
|
|
|
|
$this->success('更新完成', null, [
|
|
'new' => $total_new,
|
|
'dup' => $total_dup,
|
|
'total' => Db::name('hitokoto')->count(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 切换开关
|
|
*/
|
|
public function toggle_switch()
|
|
{
|
|
$id = intval(input('id', 0));
|
|
$val = intval(input('switch', 0));
|
|
if (!$id) {
|
|
$this->error('参数错误');
|
|
}
|
|
Db::name('hitokoto')->where('id', $id)->update(['switch' => $val ? 1 : 0]);
|
|
$this->success('操作成功');
|
|
}
|
|
}
|