2026-02-04 17:03:24 +08:00

160 lines
4.7 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* +----------------------------------------------------------------------
* | ThinkAdmin Plugin for ThinkAdmin
* +----------------------------------------------------------------------
* | 版权所有 2014~2026 ThinkAdmin [ thinkadmin.top ]
* +----------------------------------------------------------------------
* | 官方网站: https://thinkadmin.top
* +----------------------------------------------------------------------
* | 开源协议 ( https://mit-license.org )
* | 免责声明 ( https://thinkadmin.top/disclaimer )
* | 会员特权 ( https://thinkadmin.top/vip-introduce )
* +----------------------------------------------------------------------
* | gitee 代码仓库https://gitee.com/zoujingli/ThinkAdmin
* | github 代码仓库https://github.com/zoujingli/ThinkAdmin
* +----------------------------------------------------------------------
*/
namespace app\wechat\controller;
use app\wechat\model\WechatNews;
use app\wechat\model\WechatNewsArticle;
use app\wechat\service\MediaService;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\service\AdminService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* 微信图文管理.
* @class News
*/
class News extends Controller
{
/**
* 微信图文管理.
* @auth true
* @menu true
*/
public function index()
{
$this->title = '微信图文列表';
WechatNews::mQuery(null, static function (QueryHelper $query) {
$query->where(['is_deleted' => 0])->order('id desc')->page();
});
}
/**
* 图文选择器.
* @auth true
*/
public function select()
{
$this->index();
}
/**
* 添加微信图文.
* @auth true
*/
public function add()
{
if ($this->request->isGet()) {
$this->title = '新建微信图文';
$this->fetch('form');
} else {
$update = [
'create_by' => AdminService::getUserId(),
'article_id' => $this->_buildArticle($this->request->post('data', [])),
];
if (WechatNews::mk()->save($update) !== false) {
$this->success('图文添加成功!', 'javascript:history.back()');
} else {
$this->error('图文添加失败,请稍候再试!');
}
}
}
/**
* 编辑微信图文.
* @auth true
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function edit()
{
$this->id = $this->request->get('id');
if (empty($this->id)) {
$this->error('参数错误,请稍候再试!');
}
if ($this->request->isGet()) {
if ($this->request->get('output') === 'json') {
$this->success('获取数据成功!', MediaService::news($this->id));
} else {
$this->title = '编辑微信图文';
$this->fetch('form');
}
} else {
$ids = $this->_buildArticle($this->request->post('data', []));
[$map, $data] = [['id' => $this->id], ['article_id' => $ids]];
if (WechatNews::mk()->where($map)->update($data) !== false) {
$this->success('图文更新成功!', 'javascript:history.back()');
} else {
$this->error('更新失败,请稍候再试!');
}
}
}
/**
* 删除微信图文
* auth true.
*/
public function remove()
{
WechatNews::mDelete();
}
/**
* 图文列表数据处理.
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
protected function _page_filter(array &$data)
{
foreach ($data as &$vo) {
$vo = MediaService::news($vo['id']);
}
}
/**
* 图文更新操作.
*/
private function _buildArticle(array $data): string
{
$ids = [];
foreach ($data as $vo) {
if (empty($vo['digest'])) {
$vo['digest'] = mb_substr(strip_tags(preg_replace('#(\s+| )#', '', $vo['content'])), 0, 120);
}
$vo['create_at'] = date('Y-m-d H:i:s');
if (empty($vo['id'])) {
$result = $id = WechatNewsArticle::mk()->insertGetId($vo);
} else {
$id = intval($vo['id']);
$result = WechatNewsArticle::mk()->where('id', $id)->update($vo);
}
if ($result) {
$ids[] = $id;
}
}
return join(',', $ids);
}
}