BestKnowledgeAd.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller;
  3. use app\data\model\DataXwCategory;
  4. use app\data\model\DataBestKnowledgeAd;
  5. use think\admin\Controller;
  6. /**
  7. * 新闻报道
  8. * Class BestKnowledgeAd
  9. * @package app\admin\controller\BestKnowledgeAd
  10. */
  11. class BestKnowledgeAd extends Controller
  12. {
  13. /**
  14. * 新闻广告列表
  15. * @auth true
  16. * @menu true
  17. */
  18. public function ad_index(){
  19. $this->title='广告列表';
  20. DataBestKnowledgeAd::mQuery()->layTable();
  21. }
  22. /**
  23. * 新闻广告添加
  24. * @auth true
  25. * @menu true
  26. */
  27. public function ad_add(){
  28. $this->ad_vali();
  29. $this->assign('category',DataXwCategory::show()->select());
  30. DataBestKnowledgeAd::mForm('ad_form');
  31. }
  32. /**
  33. * 新闻广告编辑
  34. * @auth true
  35. * @menu true
  36. */
  37. public function ad_edit(){
  38. $this->ad_vali();
  39. $this->assign('category',DataXwCategory::show()->select());
  40. DataBestKnowledgeAd::mForm('ad_form');
  41. }
  42. protected function ad_vali(){
  43. if($this->request->isPost()){
  44. $this->_vali([
  45. 'title.max:50'=>'标题过长',
  46. ]);
  47. }
  48. }
  49. /**
  50. * 新闻广告删除
  51. * @auth true
  52. * @menu true
  53. */
  54. public function ad_del(){
  55. $ids=$this->request->post('id');
  56. DataBestKnowledgeAd::whereIn('id',$ids)->select()->each(function ($d){$d->delete();});
  57. $this->success('删除成功');
  58. }
  59. /**
  60. * 新闻广告上下架
  61. * @auth true
  62. * @menu true
  63. */
  64. public function ad_state(){
  65. DataBestKnowledgeAd::mSave([
  66. 'status'=>$this->request->post('status'),
  67. ]);
  68. }
  69. public function _form_filter($vo){
  70. if ($this->request->isGet()) {
  71. }elseif ($this->request->isPost()){
  72. $vo['content'] = $this->textcl($vo['content']);
  73. // $vo['user_id'] = null;
  74. // var_dump($vo);die;
  75. if($this->request->action() == 'edit' || $this->request->action() == 'add'){
  76. isset($vo['id']) ? DataBestKnowledge::mk()->where('id',$vo['id'])->update($vo) : DataBestKnowledge::mk()->insert($vo);
  77. $this->success('数据更新成功',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
  78. }else if(($this->request->action() == 'ad_add' || $this->request->action() == 'ad_edit')){
  79. isset($vo['id']) ? DataBestKnowledgeAd::mk()->where('id',$vo['id'])->update($vo) : DataBestKnowledgeAd::mk()->insert($vo);
  80. $this->success('数据更新成功',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
  81. }
  82. }
  83. }
  84. /**
  85. * 表单结果处理
  86. * @param boolean $state
  87. */
  88. protected function _form_result(bool $state)
  89. {
  90. if ($state) {
  91. $this->success('保存成功!',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
  92. }
  93. }
  94. //视频加封面(阿里云文件)
  95. public function textcl($string = ''){
  96. // $string = '<div>
  97. // <video controls="controls" width="100%"><source src="https://shipcccn.oss-cn-shanghai.aliyuncs.com/dd/天文学家系列.mp4" type="audio/mp4" /></video>
  98. // </div>
  99. //
  100. // <p>11111</p>
  101. //
  102. // <div>
  103. // <video controls="controls" width="100%"><source src="https://shipcccn.oss-cn-shanghai.aliyuncs.com/dd/96326ec38bb7761ef74b7623e94e886c.mp4" type="audio/mp4" /></video>
  104. // </div>
  105. //
  106. // <p>111216546485465468</p>'; // 原始字符串
  107. $position = strpos($string, '<video '); // 获取'video'在字符串中的位置
  108. if ($position !== false) {
  109. preg_match_all('/<video[^>]*>(.*?)<\/video>/s', $string, $matches);
  110. if (isset($matches[1])) {
  111. // echo "视频标签内容为:" . htmlspecialchars($matches[0][1]);die;
  112. } else {
  113. return $string;
  114. // echo "未找到视频标签";
  115. }
  116. foreach ($matches[0] as $k => $v){
  117. //获取src部分 拼凑封面地址
  118. $pattern_src = '/\bsrc\b\s*=\s*[\'\"]?([^\'\"]*)[\'\"]?/i';
  119. preg_match_all($pattern_src,$matches[0][$k],$matchSrc);
  120. $url = $matchSrc[1][0];
  121. $url = $url.'?x-oss-process=video/snapshot,t_10000,m_fast';
  122. $replacement_d = '<video controls="controls" poster="'.$url.'" width="100%">';
  123. //第一次替换
  124. $originalString = $matches[0][$k]; //这是原始字符串
  125. $replacement = $replacement_d; //被替换后的内容
  126. $searchValue = '<video controls="controls" width="100%">'; //要替换的部分
  127. // 将 $searchValue 替换为 $replacement
  128. $newString = str_replace($searchValue, $replacement, $originalString);
  129. //第二次替换
  130. $originalString = $string; //这是原始字符串
  131. $replacement = $newString; //被替换后的内容
  132. $searchValue = $matches[0][$k]; //要替换的部分
  133. $newString = str_replace($searchValue, $replacement, $originalString);
  134. $string = $newString;
  135. }
  136. //echo $newString;die; // 输出结果为"这是被替换后的内容";
  137. return $newString;
  138. } else {
  139. return $string;
  140. // echo "未找到'video'关键词";
  141. }
  142. }
  143. /**
  144. * 新闻报道设置
  145. * @auth true
  146. * @menu true
  147. */
  148. public function config(){
  149. if($this->request->isGet()){
  150. $this->title='间隔设置';
  151. $this->assign('vo',sysconf('config_xw.'));
  152. $this->fetch();
  153. }else{
  154. $data=$this->_vali([
  155. 'split_num2.require'=>'间隔数必须',
  156. 'user_avatar2.require'=>'头像数必须',
  157. 'user_avatar2.url'=>'头像链接有误',
  158. 'username2.require'=>'名称必须',
  159. 'split_num2.gt:0'=>'间隔数必须大于0',
  160. ]);
  161. sysconf('config_xw',$data);
  162. $this->success('保存成功');
  163. }
  164. }
  165. }