同步更改

This commit is contained in:
2025-08-09 23:49:50 +08:00
parent 8e96995e1c
commit 5a5f64b8c8
243 changed files with 1536 additions and 445 deletions
+269 -20
View File
@@ -28,7 +28,10 @@ use app\admin\model\AdminUser;
use app\admin\model\User\Users;
use app\admin\model\User\UsersGroup;
use app\admin\model\Banner;
use app\admin\model\ContentPush;
use app\admin\model\ContentPush\ContentPush;
use app\admin\model\ContentPush\ContentPushSetting;
use app\admin\model\Resource\Resource;
use app\admin\model\Article\Articles;
class YunzeradminController extends Base
@@ -684,32 +687,57 @@ class YunzeradminController extends Base
public function contentpushlist()
{
if (Request::isGet()) {
$page = intval(input('post.page', 1));
$limit = intval(input('post.limit', 10));
// 获取分页参数
$page = (int) input('get.page', 1);
$limit = (int) input('get.limit', 10);
$query = ContentPush::where('delete_time', null)
->field('id, title, type, status, sort, create_time, update_time');
// 获取总记录数
$count = $query->count();
// 获取分页数据
$lists = $query->order(['sort DESC', 'id DESC'])
->page($page, $limit)
// 1. 获取 Articles 表(delete_time为空,status=2
$articles = Articles::where('delete_time', null)
->where('status', 2)
->field('id, title, push, create_time')
->select()
->toArray();
// 处理数据
foreach ($lists as &$item) {
$item['create_time'] = is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : $item['create_time'];
$item['update_time'] = is_numeric($item['update_time']) ? date('Y-m-d H:i:s', $item['update_time']) : $item['update_time'];
foreach ($articles as &$a) {
$a['type'] = 'article';
$a['create_time'] = is_numeric($a['create_time']) ? date('Y-m-d H:i:s', $a['create_time']) : $a['create_time'];
}
unset($a);
// 2. 获取 Resource 表(delete_time为空,status=1
$resources = Resource::where('delete_time', null)
->where('status', 1)
->field('id, title, push, create_time')
->select()
->toArray();
foreach ($resources as &$r) {
$r['type'] = 'resource';
$r['create_time'] = is_numeric($r['create_time']) ? date('Y-m-d H:i:s', $r['create_time']) : $r['create_time'];
}
unset($r);
// 3. 合并
$lists = array_merge($articles, $resources);
// 4. 优先显示push为0的数据,再按create_time倒序排序
usort($lists, function ($a, $b) {
// push为0的排在前面
if ($a['push'] != $b['push']) {
return $a['push'] - $b['push'];
}
// push相同则按create_time倒序
return strtotime($b['create_time']) <=> strtotime($a['create_time']);
});
// 5. 分页
$total = count($lists);
$offset = ($page - 1) * $limit;
$data = array_slice($lists, $offset, $limit);
return json([
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $lists
'count' => $total,
'data' => $data
]);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
@@ -737,8 +765,9 @@ class YunzeradminController extends Base
}
Log::record('添加内容推送', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '添加成功']);
} else {
return View::fetch();
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 编辑内容推送
@@ -817,6 +846,137 @@ class YunzeradminController extends Base
return json(['code' => 1, 'msg' => '请求方法无效']);
}
//推送配置列表(渲染列表)
public function contentpushsetting()
{
if (Request::isAjax() || Request::isPost()) {
$page = intval(input('get.page', 1));
$limit = intval(input('get.limit', 10));
$query = ContentPushSetting::where('delete_time', null)
->field('id, title, value, platformType, status, sort, create_time');
$count = $query->count();
$lists = $query->order(['sort' => 'DESC', 'id' => 'DESC'])
->page($page, $limit)
->select()
->toArray();
foreach ($lists as &$item) {
$item['create_time'] = is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : $item['create_time'];
}
return json([
'code' => 0,
'msg' => '获取成功',
'count' => $count,
'data' => $lists
]);
} else {
return View::fetch();
}
}
//选择列出推送内容列表
public function selectpushcontent()
{
$pushcate = strtolower(trim(input('get.pushcate', '')));
$where = ['push' => 0];
$modelMap = [
'article' => Articles::class,
'resource' => Resource::class,
];
if (!isset($modelMap[$pushcate])) {
return json(['code' => 1, 'msg' => '参数错误: pushcate']);
}
$model = $modelMap[$pushcate];
$query = $model::where($where)->field('title,id');
$count = $query->count();
$list = $query->order('id', 'desc')->select()->toArray();
// 判断是接口请求还是页面请求
if (request()->isAjax() || request()->isJson()) {
return json([
'code' => 0,
'msg' => '获取成功',
'count' => $count,
'data' => $list
]);
} else {
// 这里传递变量到模板
return View::fetch('', [
'data' => $list
]);
}
}
//推送配置添加和编辑通用方法
public function contentpushsettingadd()
{
if (Request::isPost()) {
$params = input('post.');
$id = isset($params['id']) ? intval($params['id']) : 0;
if ($id > 0) {
// 编辑
$res = ContentPushSetting::update($params, ['id' => $id]);
if ($res === false) {
Log::record('编辑推送配置', 0, '编辑推送配置失败', '推送配置管理');
return json(['code' => 1, 'msg' => '编辑推送配置失败']);
}
Log::record('编辑推送配置', 1, '', '推送配置管理');
return json(['code' => 0, 'msg' => '编辑成功']);
} else {
// 添加
$res = ContentPushSetting::create($params);
if (!$res) {
Log::record('添加推送配置', 0, '添加推送配置失败', '推送配置管理');
return json(['code' => 1, 'msg' => '添加推送配置失败']);
}
Log::record('添加推送配置', 1, '', '推送配置管理');
return json(['code' => 0, 'msg' => '添加成功']);
}
} else {
$id = input('get.id', 0);
$info = [];
if ($id) {
$info = ContentPushSetting::where('id', $id)->find();
if ($info) {
$info = $info->toArray();
}
}
return View::fetch('', ['info' => $info]);
}
}
//推送配置软删除
public function contentpushsettingdel()
{
if (Request::isPost()) {
$id = intval(input('post.id', 0));
if (!$id) {
return json(['code' => 1, 'msg' => '参数错误']);
}
$setting = ContentPushSetting::where('id', $id)->find();
if (!$setting) {
return json(['code' => 1, 'msg' => '配置不存在']);
}
$res = ContentPushSetting::where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]);
if ($res === false) {
Log::record('删除推送配置', 0, '删除失败', '推送配置管理');
return json(['code' => 1, 'msg' => '删除失败']);
}
Log::record('删除推送配置', 1, '', '推送配置管理');
return json(['code' => 0, 'msg' => '删除成功']);
}
return json(['code' => 1, 'msg' => '请求方式错误']);
}
//素材中心
public function materialcenter()
{
@@ -957,4 +1117,93 @@ class YunzeradminController extends Base
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
public function gopush()
{
$platformType = input('post.platformType');
$urls = input('post.urls/a', []);
if (empty($platformType)) {
return json(['code' => 1, 'msg' => '平台参数缺失']);
}
if (empty($urls)) {
return json(['code' => 1, 'msg' => '推送内容url为空']);
}
// 查找推送平台配置
$setting = ContentPushSetting::where('platformType', $platformType)->find();
if (!$setting) {
return json(['code' => 1, 'msg' => '未找到该平台的推送配置']);
}
if ($setting['status'] == 0) {
return json(['code' => 1, 'msg' => '该平台推送已禁用']);
}
$api = $setting['value'];
if (empty($api)) {
return json(['code' => 1, 'msg' => '推送API地址未配置']);
}
// 推送
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls), // 多行文本,每行一个url
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return json(['code' => 1, 'msg' => $error, 'result' => $result]);
}
// 解析推送返回结果
$resultArr = json_decode($result, true);
// 判断返回是否有error字段
if (is_array($resultArr) && isset($resultArr['error'])) {
return json([
'code' => 1,
'msg' => '推送失败: ' . (isset($resultArr['message']) ? $resultArr['message'] : '未知错误'),
'result' => $resultArr
]);
}
// 判断是否有success字段,只有有success字段才认为推送成功
if (is_array($resultArr) && isset($resultArr['success'])) {
// 推送成功后,更新对应数据的push字段为1
// 解析urls,分别更新Articles或Resource表
foreach ($urls as $url) {
// 匹配文章
if (preg_match('/\/index\/articles\/detail\?id=(\d+)/', $url, $matches)) {
$id = intval($matches[1]);
if ($id > 0) {
Articles::where('id', $id)->update(['push' => 1]);
}
}
// 匹配资源
elseif (preg_match('/\/index\/resources\/detail\?id=(\d+)/', $url, $matches)) {
$id = intval($matches[1]);
if ($id > 0) {
Resource::where('id', $id)->update(['push' => 1]);
}
}
}
return json(['code' => 0, 'msg' => '推送成功', 'result' => $resultArr]);
} else {
// 没有success字段,视为推送失败,把失败的result反馈给前端
return json([
'code' => 1,
'msg' => '推送失败',
'result' => $resultArr !== null ? $resultArr : $result
]);
}
}
}