DistAppearanceTemplate.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use App\Admin\Repositories\DistAppearanceTemplateLog;
  4. use App\Models\DistAppearanceTemplate as Model;
  5. use App\Models\SiteAppearanceTemplate;
  6. use Dcat\Admin\Repositories\EloquentRepository;
  7. use DOMDocument;
  8. use Illuminate\Support\Carbon;
  9. use Symfony\Component\DomCrawler\Crawler;
  10. class DistAppearanceTemplate extends EloquentRepository
  11. {
  12. /**
  13. * Model.
  14. *
  15. * @var string
  16. */
  17. protected $eloquentClass = Model::class;
  18. /*
  19. * 同步模版到正式表上
  20. */
  21. public static function syncAppearanceTemplates($appearanceId,$distId)
  22. {
  23. $model = new Model();
  24. return $model->syncAppearanceTemplates($appearanceId,$distId);
  25. }
  26. /*
  27. * 得到独立页的模版数组
  28. */
  29. public static function getLandingPageTemplateOptions()
  30. {
  31. $distInfo = DistAdminDistributor::getInfo();
  32. $distId = $distInfo->id;
  33. $appearanceId = $distInfo->appearance_id;
  34. $model = new Model();
  35. $result = $model->where('appearance_id', $appearanceId)->where('dist_id', $distId)->select('file_name')->get();
  36. $options = ['pages_detail.liquid'=>config('dictionary.landing_page_default_template')];
  37. foreach ($result as $key => $value) {
  38. // 检查字符串是否以 "pages_sp_" 开头
  39. if (strpos($value->file_name, 'pages_sp_') === 0) {
  40. // 提取 "xxxx" 部分
  41. // $part = str_replace('pages_sp_', '', $value->file_name);
  42. // $part = str_replace('.liquid', '', $part);
  43. $part = $value->file_name;
  44. $options[$part] = $part;
  45. }
  46. }
  47. return $options;
  48. }
  49. public static function previewSave($data)
  50. {
  51. $distInfo = DistAdminDistributor::getInfo();
  52. $distId = $distInfo->id;
  53. $appearanceId = $distInfo->appearance_id;
  54. //查找$data中是否存在mtb_id
  55. $inputElements = self::findElementsWithMtbId($data);
  56. foreach ($inputElements as $prefix => $elements) {
  57. //找查找对应的模板
  58. $template = Model::where('dist_id', $distId)->where('appearance_id', $appearanceId)->where('template_code',$prefix)->first();
  59. if ($template) {
  60. //替换模板内容
  61. $oldContent = self::contentChange($template->content,$template->template_code);//旧模板内容
  62. $newContent = self::contentReplace($oldContent,$elements);
  63. //更新模板内容
  64. $template->content = $newContent;
  65. $template->updated_at = Carbon::now();
  66. $template->save();
  67. //记录日志
  68. DistAppearanceTemplateLog::insertLog($appearanceId,$distId,$template->file_name,$template->file_path,$template->template_code,$newContent,$oldContent);
  69. }
  70. }
  71. }
  72. /*
  73. * 替换模板内容
  74. * @param $content 模板内容
  75. * @param $elements Array([mtb_id] => 676a6d11f1a57_1,[outHtml] => <div mtb_id="abc_2"><p>Again3</p></div>)
  76. */
  77. public static function contentReplace($content,$elements) {
  78. foreach ($elements as $element) {
  79. $mtbId = $element['mtb_id'];
  80. $outHtml = $element['outHtml'];
  81. $content = preg_replace_callback(
  82. '/<([a-zA-Z0-9]+)[^>]*mtb_id="' . preg_quote($mtbId, '/') . '"[^>]*>(.*?)<\/\1>/s',
  83. function ($matches) use ($outHtml) {
  84. // 返回替换后的内容
  85. return $outHtml;
  86. },
  87. $content
  88. );
  89. //替换img等标签的情况
  90. $content = preg_replace_callback(
  91. '/<([a-zA-Z0-9]+)[^>]*mtb_id="' . preg_quote($mtbId, '/') . '"[^>]*>/s',
  92. function ($matches) use ($outHtml) {
  93. // 返回替换后的内容
  94. return $outHtml;
  95. },
  96. $content
  97. );
  98. }
  99. return $content;
  100. }
  101. /*
  102. * 查找带有 mtb_id 属性的 HTML 元素
  103. */
  104. public static function findElementsWithMtbId($html) {
  105. // 定义正则表达式来匹配所有带有 mtb_id 属性的 HTML 元素
  106. $pattern = '/<([a-zA-Z0-9-_]+)([^>]*\smtb_id="([^"]+)")([^>]*)>(.*?)<\/\1>/is';
  107. preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
  108. // 存储结果
  109. $result = [];
  110. foreach ($matches as $match) {
  111. // 提取 mtb_id 和匹配的 HTML 内容
  112. $outHtml = $match[0];
  113. //如果第一个字符>后边有<,则加上空格
  114. $pattern = '/>(?=<)/'; // 匹配 > 后紧跟 <
  115. $replacement = '> '; // 在 > 和 < 之间添加一个空格
  116. $outHtml = preg_replace($pattern, $replacement, $outHtml, 1);
  117. //去掉mtb_id属性
  118. $outHtml = preg_replace('/\s*mtb_id="[^"]*"/', '', $outHtml);
  119. $result[] = [
  120. 'mtb_id' => $match[3], // mtb_id 的值
  121. 'outHtml' => $outHtml, // 匹配到的完整 HTML 元素
  122. ];
  123. }
  124. //匹配img等标签
  125. $pattern = '/<([a-zA-Z0-9]+)[^>]*\smtb_id="([^"]+)"[^>]*>/is'; // 匹配所有含有 mtb_id 属性的标签
  126. preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
  127. foreach ($matches as $match) {
  128. //去掉mtb_id属性
  129. $outHtml = $match[0];
  130. $outHtml = preg_replace('/\s*mtb_id="[^"]*"/', '', $outHtml);
  131. $result[] = [
  132. 'mtb_id' => $match[2], // mtb_id 的值,这里改为 $match[2]
  133. 'outHtml' => $outHtml, // 匹配到的完整 HTML 元素
  134. ];
  135. }
  136. $result = array_reduce($result, function ($carry, $item) {
  137. // 获取 mtb_id 的前缀
  138. $prefix = explode('_', $item['mtb_id'])[0];
  139. // 如果前缀不存在,初始化一个空数组
  140. if (!isset($carry[$prefix])) {
  141. $carry[$prefix] = [];
  142. }
  143. // 将当前项添加到对应的前缀数组中
  144. $carry[$prefix][] = $item;
  145. return $carry;
  146. }, []);
  147. return $result;
  148. }
  149. /*
  150. * TODO: 模板内容增加 mtb_id 属性,用于区分不同模板
  151. */
  152. public static function contentChange($content,$templateCode)
  153. {
  154. $count = 1;
  155. $newContent = preg_replace_callback(
  156. '/(<[^>]+?mtb_edit=[\'"][^\'"]+[\'"][^>]*>)/',
  157. function ($matches) use (&$count, $templateCode) {
  158. // 在匹配到的 HTML 元素后添加 mtb_id 属性
  159. return preg_replace('/(mtb_edit=[\'"][^\'"]+[\'"])([^>]*)>/', '$1 mtb_id="' . $templateCode .'_'. $count++ . '"$2>', $matches[0]);
  160. },
  161. $content
  162. );
  163. return $newContent;
  164. }
  165. }