| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/11/02
- */
- namespace app\admin\model\article;
- use app\admin\model\store\StoreProduct;
- use app\admin\model\system\SystemAdmin;
- use app\models\article\ArticleContent;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- /**
- * 图文管理 Model
- * Class WechatNews
- * @package app\admin\model\wechat
- */
- class Article extends BaseModel
- {
- use ModelTrait;
- protected $pk = 'id';
- protected $name = 'article';
- public function profile()
- {
- return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field('store_name');
- }
- /**
- * 获取配置分类
- * @param array $where
- * @return array
- */
- public static function getAll($where = [])
- {
- $model = new self;
- // if($where['status'] !== '') $model = $model->where('status',$where['status']);
- // if($where['access'] !== '') $model = $model->where('access',$where['access']);
- if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
- if ($where['cid'] !== '')
- $model = $model->where('cid', 'in', $where['cid']);
- else
- if ($where['merchant'])
- $model = $model->where('mer_id', '>', 0);
- else
- $model = $model->where('mer_id', 0);
- $model = $model->where('status', 1)->where('hide', 0)->order('id desc');
- return self::page($model, function ($item) {
- if (!$item['mer_id']) $item['admin_name'] = '总后台管理员---》' . SystemAdmin::where('id', $item['admin_id'])->value('real_name');
- else $item['admin_name'] = Merchant::where('id', $item['mer_id'])->value('mer_name') . '---》' . MerchantAdmin::where('id', $item['admin_id'])->value('real_name');
- $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
- $item['catename'] = ArticleCategory::where('id', $item['cid'])->value('title');
- $item['store_name'] = $item->profile->store_name ?? '';
- }, $where);
- }
- /**
- * 删除图文
- * @param $id
- * @return bool
- */
- public static function del($id)
- {
- return self::edit(['status' => 0], $id, 'id');
- }
- /**
- * 获取指定字段的值
- * @return array
- */
- public static function getNews()
- {
- return self::where('status', 1)->where('hide', 0)->order('id desc')->column('title', 'id');
- }
- /**
- * 给表中的字符串类型追加值
- * 删除所有有当前分类的id之后重新添加
- * @param $cid
- * @param $id
- * @return bool
- */
- public static function saveBatchCid($cid, $id)
- {
- $res_all = self::where('cid', 'LIKE', "%$cid%")->select();//获取所有有当前分类的图文
- foreach ($res_all as $k => $v) {
- $cid_arr = explode(',', $v['cid']);
- if (in_array($cid, $cid_arr)) {
- $key = array_search($cid, $cid_arr);
- array_splice($cid_arr, $key, 1);
- }
- if (empty($cid_arr)) {
- $data['cid'] = 0;
- self::edit($data, $v['id']);
- } else {
- $data['cid'] = implode(',', $cid_arr);
- self::edit($data, $v['id']);
- }
- }
- $res = self::where('id', 'IN', $id)->select();
- foreach ($res as $k => $v) {
- if (!in_array($cid, explode(',', $v['cid']))) {
- if (!$v['cid']) {
- $data['cid'] = $cid;
- } else {
- $data['cid'] = $v['cid'] . ',' . $cid;
- }
- self::edit($data, $v['id']);
- }
- }
- return true;
- }
- public static function setContent($id, $content)
- {
- $count = ArticleContent::where('nid', $id)->count();
- $data['nid'] = $id;
- $data['content'] = $content;
- if ($count) {
- $contentSql = ArticleContent::where('nid', $id)->value('content');
- if ($contentSql == $content){
- $res = true;
- } else{
- @file_put_contents('quanju.txt', $content . "-789\r\n", 8);
- $content = self::fixArticleContent($content);
- @file_put_contents('quanju.txt', $content . "-超级变换形态\r\n", 8);
- $res = ArticleContent::where('nid', $id)->update(['content' => $content]);
- }
- if ($res !== false) $res = true;
- } else {
- $res = ArticleContent::insert($data);
- }
- return $res;
- }
- public static function fixArticleContent($html) {
- // 1. 去除多余的转义反斜杠
- $fixed = stripslashes($html);
- // 2. 修复图片标签的格式问题
- $fixed = preg_replace_callback('/<img\s+([^>]*)>/i', function($matches) {
- $attrs = $matches[1];
- // 提取src属性
- preg_match('/src\s*=\s*["\']([^"\']+)["\']/i', $attrs, $srcMatch);
- $src = $srcMatch[1] ?? '';
- // 提取宽度和高度(如果有)
- preg_match('/width\s*:\s*([\d.]+px)/i', $attrs, $widthMatch);
- preg_match('/height\s*:\s*([\d.]+px)/i', $attrs, $heightMatch);
- $width = $widthMatch[1] ?? '';
- $height = $heightMatch[1] ?? '';
- // 提取现有样式(如果有)
- preg_match('/style\s*=\s*["\']([^"\']+)["\']/i', $attrs, $styleMatch);
- $existingStyle = $styleMatch[1] ?? '';
- // 构建新属性
- $newAttrs = '';
- // 保留所有有效属性(排除错误的=属性)
- preg_match_all('/(\w+)\s*=\s*["\'][^"\']*["\']/i', $attrs, $validAttrs);
- foreach ($validAttrs[0] as $attr) {
- // 跳过错误的=属性
- if (!strpos($attr, '="')) {
- continue;
- }
- // 跳过将被替换的width/height属性
- $attrName = strtolower(trim(explode('=', $attr)[0]));
- if ($attrName === 'width' || $attrName === 'height') {
- continue;
- }
- $newAttrs .= ' ' . $attr;
- }
- // 构建样式属性
- $style = trim($existingStyle);
- if (!empty($width) || !empty($height)) {
- $newStyle = '';
- if (!empty($width)) $newStyle .= "width: $width;";
- if (!empty($height)) $newStyle .= "height: $height;";
- if (!empty($style)) {
- // 合并新样式和现有样式
- $style = rtrim($style, ';') . ';' . $newStyle;
- } else {
- $style = $newStyle;
- }
- }
- // 添加样式属性(如果有)
- if (!empty($style)) {
- $newAttrs .= ' style="' . htmlspecialchars($style) . '"';
- }
- return '<img' . $newAttrs . '>';
- }, $fixed);
- // 3. 修复可能被破坏的段落标签
- $fixed = preg_replace('/<p>\s*<\/p>/', '', $fixed); // 移除空段落
- $fixed = preg_replace('/(<p[^>]*>)\s*(<\/p>)/', '$1$2', $fixed); // 移除段落内的空白
- return $fixed;
- }
- public static function merchantPage($where = [])
- {
- $model = new self;
- if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
- if ($where['cid'] !== '') $model = $model->where('cid', 'LIKE', "%$where[cid]%");
- $model = $model
- ->where('status', 1)
- ->where('hide', 0)
- ->where('admin_id', $where['admin_id'])
- ->where('mer_id', $where['mer_id']);
- return self::page($model, function ($item) {
- $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
- }, $where);
- }
- /**
- * 获取指定文章列表 图文管理使用
- * @param string $id
- * @param string $field
- * @return false|\PDOStatement|string|\think\Collection
- */
- public static function getArticleList($id = '', $field = 'title,author,image_input,synopsis,id')
- {
- $list = self::where('id', 'IN', $id)->field($field)->select();
- foreach ($list as $k => $v) {
- $list[$k]['content'] = ArticleContent::where('nid', $v['id'])->value('content');
- }
- return $list;
- }
- }
|