排版相关正文内容调整
This commit is contained in:
358
application/api/controller/Articlemain.php
Normal file
358
application/api/controller/Articlemain.php
Normal file
@@ -0,0 +1,358 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
use app\api\controller\Base;
|
||||
use think\Db;
|
||||
/**
|
||||
* @title 文章正文内容处理
|
||||
*/
|
||||
class Articlemain extends Base
|
||||
{
|
||||
|
||||
|
||||
|
||||
public function __construct(\think\Request $request = null) {
|
||||
parent::__construct($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传表格图片
|
||||
*/
|
||||
public function uploadTableImage(){
|
||||
|
||||
//获取参数
|
||||
$aParam = $this->request->post();
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
||||
}
|
||||
//上传文件域
|
||||
$file = $this->request->file('file_name');
|
||||
if (empty($file)) {
|
||||
return json_encode(array('status' => 2,'msg' => 'No uploaded file was obtained'));
|
||||
}
|
||||
//查询文章信息
|
||||
$aWhere = ['article_id' => $iArticleId];
|
||||
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
||||
if(empty($aArticle)){
|
||||
return json_encode(array('status' => 3,'msg' => 'The article does not exist' ));
|
||||
}
|
||||
//验证文件(类型/大小)
|
||||
$validate = [
|
||||
'size' => 1024000, // 限制1M以内
|
||||
// 'ext' => 'jpg,jpeg,png,gif' // 允许的后缀
|
||||
];
|
||||
$fileValid = $file->validate($validate);
|
||||
if (!$fileValid) {
|
||||
$errorMsg = $file->getError();
|
||||
return json_encode(['status' => 3, 'msg' => 'Format/size validation failed:' . $errorMsg]);
|
||||
}
|
||||
|
||||
//图片名后缀
|
||||
$ext = $file->getExtension();
|
||||
if (empty($ext)) {
|
||||
// 获取文件真实MIME类型(复制粘贴的图片也能识别)
|
||||
$mime = $file->getMime();
|
||||
// MIME类型与后缀的映射表(覆盖常见图片类型)
|
||||
$mimeExtMap = [
|
||||
// 基础图片格式
|
||||
'image/jpeg' => 'jpg',
|
||||
'image/jpg' => 'jpg', // 兼容简写
|
||||
'image/png' => 'png',
|
||||
'image/gif' => 'gif',
|
||||
'image/bmp' => 'bmp',
|
||||
'image/x-bmp' => 'bmp', // 兼容Windows bmp
|
||||
// 扩展图片格式
|
||||
'image/webp' => 'webp', // 网页常用
|
||||
'image/tiff' => 'tiff',
|
||||
'image/x-tiff' => 'tiff',
|
||||
'image/svg+xml' => 'svg', // 矢量图
|
||||
'image/heic' => 'heic', // 苹果图片格式
|
||||
'image/heif' => 'heif',
|
||||
'image/avif' => 'avif', // 高效压缩图
|
||||
// 特殊图片格式
|
||||
'image/x-icon' => 'ico', // 图标
|
||||
'image/vnd.microsoft.icon' => 'ico',
|
||||
];
|
||||
$ext = $mimeExtMap[$mime] ?? '';
|
||||
}
|
||||
$ext = empty($ext) ? 'png' : $ext;
|
||||
//组装新的图片名
|
||||
$sFileName = md5(uniqid(mt_rand(), true)) . '.' . $ext;
|
||||
|
||||
//保存文件
|
||||
$sImagePath = rtrim(ROOT_PATH,'/') . '/public' . DS . 'articleTableImage' . DS . $iArticleId;
|
||||
if (!is_dir($sImagePath)) {
|
||||
mkdir($sImagePath, 0777, true);
|
||||
}
|
||||
$sImageName = $fileValid->move($sImagePath,$sFileName);
|
||||
if (!$sImageName) {
|
||||
return json_encode(['status' => 4, 'msg' => $fileValid->getError()]);
|
||||
}
|
||||
|
||||
//返回图片路径
|
||||
return json_encode(['status' => 1, 'msg' => 'Upload successful','data' => $iArticleId .DS.$sFileName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取正文内容里删除图片
|
||||
*/
|
||||
public function removeMainImage(){
|
||||
|
||||
//获取参数
|
||||
$aParam = $this->request->post();
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
||||
}
|
||||
//图片ID
|
||||
$iAmiId = empty($aParam['ami_id']) ? 0 : $aParam['ami_id'];
|
||||
if(empty($iAmiId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select the image ID to query'));
|
||||
}
|
||||
//查询文章信息
|
||||
$aWhere = ['article_id' => $iArticleId];
|
||||
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
||||
if(empty($aArticle)){
|
||||
return json_encode(array('status' => 3,'msg' => 'The article does not exist' ));
|
||||
}
|
||||
//查询图片信息
|
||||
$aWhere = ['ami_id' => $iAmiId,'state' => 0,'article_id' => $iArticleId];
|
||||
$aMainImage = Db::name('article_main_image')->field('ami_id')->where($aWhere)->find();
|
||||
if(empty($aMainImage)){
|
||||
return json_encode(array('status' => 4,'msg' => 'Article image information does not exist' ));
|
||||
}
|
||||
//获取正文内容
|
||||
$aWhere = ['article_id' => $iArticleId,'type' => ['in',[0,1]],'state' => 0,'is_h1' => ['<>',1],'is_h2' => ['<>',1],'is_h3' => ['<>',1]];
|
||||
$aArticleMain = Db::name('article_main')->field('am_id,content,type,ami_id')->where($aWhere)->order('sort asc')->select();
|
||||
|
||||
$iBindImage = 2;
|
||||
if(!empty($aArticleMain)){//验证正文内容是否绑定该图片
|
||||
//数据处理
|
||||
foreach ($aArticleMain as $key => $value) {
|
||||
$sContent = empty($value['content']) ? '' : trim($value['content']);
|
||||
if(empty($sContent)){
|
||||
continue;
|
||||
}
|
||||
if(!empty($iAmiId)){
|
||||
if($value['type'] == 1 && $value['ami_id'] == $iAmiId){
|
||||
$iBindImage = 1;
|
||||
break;
|
||||
}
|
||||
if($value['type'] == 0){
|
||||
$result = $this->hasProcessedTagWithId($value['content'],$iAmiId);
|
||||
if($result == 1){
|
||||
$iBindImage = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if($iBindImage == 1){
|
||||
return json_encode(array('status' => 5,'msg' => 'The main content has been bound to this image and cannot be deleted' ));
|
||||
}
|
||||
//删除图片
|
||||
$aWhere = ['ami_id' => $iAmiId];
|
||||
$aUpdate = ['state' => 1];
|
||||
$result = Db::name('article_main_image')->where($aWhere)->limit(1)->update($aUpdate);
|
||||
if($result === false){
|
||||
return json_encode(array('status' => 6,'msg' => 'Image deletion failed' ));
|
||||
}
|
||||
//返回数据
|
||||
return json_encode(array('status' => 1,'msg' => 'Image deleted successfully'));
|
||||
}
|
||||
/**
|
||||
* 获取正文内容里删除表格
|
||||
*/
|
||||
public function removeMainTable(){
|
||||
|
||||
//获取参数
|
||||
$aParam = $this->request->post();
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
||||
}
|
||||
//表格ID
|
||||
$iAmtId = empty($aParam['amt_id']) ? 0 : $aParam['amt_id'];
|
||||
if(empty($iAmtId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select the table ID to query'));
|
||||
}
|
||||
//查询文章信息
|
||||
$aWhere = ['article_id' => $iArticleId];
|
||||
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
||||
if(empty($aArticle)){
|
||||
return json_encode(array('status' => 3,'msg' => 'The article does not exist' ));
|
||||
}
|
||||
//查询表格信息
|
||||
$aWhere = ['amt_id' => $iAmtId,'state' => 0,'article_id' => $iArticleId];
|
||||
$aMainTable = Db::name('article_main_table')->field('amt_id')->where($aWhere)->find();
|
||||
if(empty($aMainTable)){
|
||||
return json_encode(array('status' => 4,'msg' => 'Article table information does not exist' ));
|
||||
}
|
||||
|
||||
//获取正文内容
|
||||
$aWhere = ['article_id' => $iArticleId,'type' => ['in',[0,2]],'state' => 0,'is_h1' => ['<>',1],'is_h2' => ['<>',1],'is_h3' => ['<>',1]];
|
||||
$aArticleMain = Db::name('article_main')->field('am_id,content,type,amt_id')->where($aWhere)->order('sort asc')->select();
|
||||
//数据处理-验证正文是否绑定表格
|
||||
$iBindTable = 2;
|
||||
if(!empty($aArticleMain)){
|
||||
foreach ($aArticleMain as $key => $value) {
|
||||
$sContent = empty($value['content']) ? '' : trim($value['content']);
|
||||
if(empty($sContent)){
|
||||
continue;
|
||||
}
|
||||
if($value['type'] == 2 && $value['amt_id'] == $iAmtId){
|
||||
$iBindTable = 1;
|
||||
break;
|
||||
}
|
||||
if($value['type'] == 0){
|
||||
$result = $this->hasProcessedTagWithId($value['content'],$iAmtId,'mytable');
|
||||
if($result == 1){
|
||||
$iBindTable = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if($iBindTable == 1){
|
||||
return json_encode(array('status' => 5,'msg' => 'The main content is already bound to this table and cannot be deleted' ));
|
||||
}
|
||||
//删除表格
|
||||
$aWhere = ['amt_id' => $iAmtId];
|
||||
$aUpdate = ['state' => 1];
|
||||
$result = Db::name('article_main_table')->where($aWhere)->limit(1)->update($aUpdate);
|
||||
if($result === false){
|
||||
return json_encode(array('status' => 6,'msg' => 'Table deletion failed' ));
|
||||
}
|
||||
//返回数据
|
||||
return json_encode(array('status' => 1,'msg' => 'Table deleted successfully'));
|
||||
}
|
||||
/**
|
||||
* 验证是否存在
|
||||
*/
|
||||
public function hasProcessedTagWithId($content = '', $primaryId = 0, $sLable = 'myfigure') {
|
||||
if(empty($content) || empty($primaryId)){
|
||||
return 2;
|
||||
}
|
||||
$escapedTagName = preg_quote($sLable, '/');
|
||||
$escapedId = preg_quote($primaryId, '/');
|
||||
|
||||
// 优化后的正则表达式
|
||||
$pattern = "/<{$escapedTagName}\s+data-id\s*=\s*['\"]{$escapedId}['\"]\s*>(.*?)<\/{$escapedTagName}>/i";
|
||||
|
||||
// 执行匹配
|
||||
if (!preg_match($pattern, $content, $matches)) {
|
||||
return 3;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/**
|
||||
* 获取文章关联的图片
|
||||
*/
|
||||
public function getArticleMainImage(){
|
||||
//获取参数
|
||||
$aParam = $this->request->post();
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
||||
}
|
||||
//查询文章信息
|
||||
$aWhere = ['article_id' => $iArticleId];
|
||||
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
||||
if(empty($aArticle)){
|
||||
return json_encode(array('status' => 3,'msg' => 'The article does not exist' ));
|
||||
}
|
||||
//查询图片信息
|
||||
$aWhere = ['state' => 0,'article_id' => $iArticleId];
|
||||
$aMainImage = Db::name('article_main_image')->field('ami_id,title')->where($aWhere)->select();
|
||||
return json_encode(array('status' => 1,'msg' => 'success','data' => $aMainImage));
|
||||
}
|
||||
/**
|
||||
* 获取文章关联的表格
|
||||
*/
|
||||
public function getArticleMainTable(){
|
||||
//获取参数
|
||||
$aParam = $this->request->post();
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
||||
}
|
||||
//查询文章信息
|
||||
$aWhere = ['article_id' => $iArticleId];
|
||||
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
||||
if(empty($aArticle)){
|
||||
return json_encode(array('status' => 3,'msg' => 'The article does not exist' ));
|
||||
}
|
||||
//查询表格信息
|
||||
$aWhere = ['state' => 0,'article_id' => $iArticleId];
|
||||
$aMainTable = Db::name('article_main_table')->field('amt_id,title')->where($aWhere)->order('amt_id asc')->select();
|
||||
return json_encode(array('status' => 1,'msg' => 'success','data' => $aMainTable));
|
||||
}
|
||||
/**
|
||||
* 获取文章关联图片的详细信息
|
||||
*/
|
||||
public function getMainImageInfo(){
|
||||
//获取参数
|
||||
$aParam = $this->request->post();
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
||||
}
|
||||
//图片ID
|
||||
$iAmiId = empty($aParam['ami_id']) ? 0 : $aParam['ami_id'];
|
||||
if(empty($iAmiId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select the image ID to query'));
|
||||
}
|
||||
//查询文章信息
|
||||
$aWhere = ['article_id' => $iArticleId];
|
||||
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
||||
if(empty($aArticle)){
|
||||
return json_encode(array('status' => 3,'msg' => 'The article does not exist' ));
|
||||
}
|
||||
//查询图片信息
|
||||
$aWhere = ['state' => 0,'article_id' => $iArticleId,'ami_id' => $iAmiId];
|
||||
$aMainImage = Db::name('article_main_image')->field('ami_id,title,url,note')->where($aWhere)->find();
|
||||
return json_encode(array('status' => 1,'msg' => 'success','data' => $aMainImage));
|
||||
}
|
||||
/**
|
||||
* 获取文章关联表格的详细信息
|
||||
*/
|
||||
public function getMainTableInfo(){
|
||||
//获取参数
|
||||
$aParam = $this->request->post();
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
||||
}
|
||||
//表格ID
|
||||
$iAmtId = empty($aParam['amt_id']) ? 0 : $aParam['amt_id'];
|
||||
if(empty($iAmtId)){
|
||||
return json_encode(array('status' => 2,'msg' => 'Please select the table ID to query'));
|
||||
}
|
||||
//查询文章信息
|
||||
$aWhere = ['article_id' => $iArticleId];
|
||||
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
||||
if(empty($aArticle)){
|
||||
return json_encode(array('status' => 3,'msg' => 'The article does not exist' ));
|
||||
}
|
||||
//查询表格信息
|
||||
$aWhere = ['state' => 0,'article_id' => $iArticleId,'amt_id' => $iAmtId];
|
||||
$aMainTable = Db::name('article_main_table')->field('amt_id,type,table_data,url,title,note')->where($aWhere)->find();
|
||||
return json_encode(array('status' => 1,'msg' => 'success','data' => $aMainTable));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user