Article.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\admin\model\article;
  8. use app\admin\model\store\StoreProduct;
  9. use app\admin\model\system\SystemAdmin;
  10. use app\models\article\ArticleContent;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. /**
  14. * 图文管理 Model
  15. * Class WechatNews
  16. * @package app\admin\model\wechat
  17. */
  18. class Article extends BaseModel
  19. {
  20. use ModelTrait;
  21. protected $pk = 'id';
  22. protected $name = 'article';
  23. public function profile()
  24. {
  25. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field('store_name');
  26. }
  27. /**
  28. * 获取配置分类
  29. * @param array $where
  30. * @return array
  31. */
  32. public static function getAll($where = [])
  33. {
  34. $model = new self;
  35. // if($where['status'] !== '') $model = $model->where('status',$where['status']);
  36. // if($where['access'] !== '') $model = $model->where('access',$where['access']);
  37. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  38. if ($where['cid'] !== '')
  39. $model = $model->where('cid', 'in', $where['cid']);
  40. else
  41. if ($where['merchant'])
  42. $model = $model->where('mer_id', '>', 0);
  43. else
  44. $model = $model->where('mer_id', 0);
  45. $model = $model->where('status', 1)->where('hide', 0)->order('id desc');
  46. return self::page($model, function ($item) {
  47. if (!$item['mer_id']) $item['admin_name'] = '总后台管理员---》' . SystemAdmin::where('id', $item['admin_id'])->value('real_name');
  48. else $item['admin_name'] = Merchant::where('id', $item['mer_id'])->value('mer_name') . '---》' . MerchantAdmin::where('id', $item['admin_id'])->value('real_name');
  49. $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
  50. $item['catename'] = ArticleCategory::where('id', $item['cid'])->value('title');
  51. $item['store_name'] = $item->profile->store_name ?? '';
  52. }, $where);
  53. }
  54. /**
  55. * 删除图文
  56. * @param $id
  57. * @return bool
  58. */
  59. public static function del($id)
  60. {
  61. return self::edit(['status' => 0], $id, 'id');
  62. }
  63. /**
  64. * 获取指定字段的值
  65. * @return array
  66. */
  67. public static function getNews()
  68. {
  69. return self::where('status', 1)->where('hide', 0)->order('id desc')->column('title', 'id');
  70. }
  71. /**
  72. * 给表中的字符串类型追加值
  73. * 删除所有有当前分类的id之后重新添加
  74. * @param $cid
  75. * @param $id
  76. * @return bool
  77. */
  78. public static function saveBatchCid($cid, $id)
  79. {
  80. $res_all = self::where('cid', 'LIKE', "%$cid%")->select();//获取所有有当前分类的图文
  81. foreach ($res_all as $k => $v) {
  82. $cid_arr = explode(',', $v['cid']);
  83. if (in_array($cid, $cid_arr)) {
  84. $key = array_search($cid, $cid_arr);
  85. array_splice($cid_arr, $key, 1);
  86. }
  87. if (empty($cid_arr)) {
  88. $data['cid'] = 0;
  89. self::edit($data, $v['id']);
  90. } else {
  91. $data['cid'] = implode(',', $cid_arr);
  92. self::edit($data, $v['id']);
  93. }
  94. }
  95. $res = self::where('id', 'IN', $id)->select();
  96. foreach ($res as $k => $v) {
  97. if (!in_array($cid, explode(',', $v['cid']))) {
  98. if (!$v['cid']) {
  99. $data['cid'] = $cid;
  100. } else {
  101. $data['cid'] = $v['cid'] . ',' . $cid;
  102. }
  103. self::edit($data, $v['id']);
  104. }
  105. }
  106. return true;
  107. }
  108. public static function setContent($id, $content)
  109. {
  110. $count = ArticleContent::where('nid', $id)->count();
  111. $data['nid'] = $id;
  112. $data['content'] = $content;
  113. if ($count) {
  114. $contentSql = ArticleContent::where('nid', $id)->value('content');
  115. if ($contentSql == $content){
  116. $res = true;
  117. } else{
  118. @file_put_contents('quanju.txt', $content . "-789\r\n", 8);
  119. $content = self::fixArticleContent($content);
  120. @file_put_contents('quanju.txt', $content . "-超级变换形态\r\n", 8);
  121. $res = ArticleContent::where('nid', $id)->update(['content' => $content]);
  122. }
  123. if ($res !== false) $res = true;
  124. } else {
  125. $res = ArticleContent::insert($data);
  126. }
  127. return $res;
  128. }
  129. public static function fixArticleContent($html) {
  130. // 1. 去除多余的转义反斜杠
  131. $fixed = stripslashes($html);
  132. // 2. 修复图片标签的格式问题
  133. $fixed = preg_replace_callback('/<img\s+([^>]*)>/i', function($matches) {
  134. $attrs = $matches[1];
  135. // 提取src属性
  136. preg_match('/src\s*=\s*["\']([^"\']+)["\']/i', $attrs, $srcMatch);
  137. $src = $srcMatch[1] ?? '';
  138. // 提取宽度和高度(如果有)
  139. preg_match('/width\s*:\s*([\d.]+px)/i', $attrs, $widthMatch);
  140. preg_match('/height\s*:\s*([\d.]+px)/i', $attrs, $heightMatch);
  141. $width = $widthMatch[1] ?? '';
  142. $height = $heightMatch[1] ?? '';
  143. // 提取现有样式(如果有)
  144. preg_match('/style\s*=\s*["\']([^"\']+)["\']/i', $attrs, $styleMatch);
  145. $existingStyle = $styleMatch[1] ?? '';
  146. // 构建新属性
  147. $newAttrs = '';
  148. // 保留所有有效属性(排除错误的=属性)
  149. preg_match_all('/(\w+)\s*=\s*["\'][^"\']*["\']/i', $attrs, $validAttrs);
  150. foreach ($validAttrs[0] as $attr) {
  151. // 跳过错误的=属性
  152. if (!strpos($attr, '="')) {
  153. continue;
  154. }
  155. // 跳过将被替换的width/height属性
  156. $attrName = strtolower(trim(explode('=', $attr)[0]));
  157. if ($attrName === 'width' || $attrName === 'height') {
  158. continue;
  159. }
  160. $newAttrs .= ' ' . $attr;
  161. }
  162. // 构建样式属性
  163. $style = trim($existingStyle);
  164. if (!empty($width) || !empty($height)) {
  165. $newStyle = '';
  166. if (!empty($width)) $newStyle .= "width: $width;";
  167. if (!empty($height)) $newStyle .= "height: $height;";
  168. if (!empty($style)) {
  169. // 合并新样式和现有样式
  170. $style = rtrim($style, ';') . ';' . $newStyle;
  171. } else {
  172. $style = $newStyle;
  173. }
  174. }
  175. // 添加样式属性(如果有)
  176. if (!empty($style)) {
  177. $newAttrs .= ' style="' . htmlspecialchars($style) . '"';
  178. }
  179. return '<img' . $newAttrs . '>';
  180. }, $fixed);
  181. // 3. 修复可能被破坏的段落标签
  182. $fixed = preg_replace('/<p>\s*<\/p>/', '', $fixed); // 移除空段落
  183. $fixed = preg_replace('/(<p[^>]*>)\s*(<\/p>)/', '$1$2', $fixed); // 移除段落内的空白
  184. return $fixed;
  185. }
  186. public static function merchantPage($where = [])
  187. {
  188. $model = new self;
  189. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  190. if ($where['cid'] !== '') $model = $model->where('cid', 'LIKE', "%$where[cid]%");
  191. $model = $model
  192. ->where('status', 1)
  193. ->where('hide', 0)
  194. ->where('admin_id', $where['admin_id'])
  195. ->where('mer_id', $where['mer_id']);
  196. return self::page($model, function ($item) {
  197. $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
  198. }, $where);
  199. }
  200. /**
  201. * 获取指定文章列表 图文管理使用
  202. * @param string $id
  203. * @param string $field
  204. * @return false|\PDOStatement|string|\think\Collection
  205. */
  206. public static function getArticleList($id = '', $field = 'title,author,image_input,synopsis,id')
  207. {
  208. $list = self::where('id', 'IN', $id)->field($field)->select();
  209. foreach ($list as $k => $v) {
  210. $list[$k]['content'] = ArticleContent::where('nid', $v['id'])->value('content');
  211. }
  212. return $list;
  213. }
  214. }