Xw.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\admin\controller;
  3. use app\data\model\DataXw;
  4. use app\data\model\DataXwAd;
  5. use app\data\model\DataXwCategory;
  6. use app\data\controller\api\auth\Center;
  7. use think\admin\Controller;
  8. /**
  9. * 新闻报道
  10. * Class Xw
  11. * @package app\admin\controller\Xw
  12. */
  13. class Xw extends Controller
  14. {
  15. /**
  16. * 新闻列表
  17. * @auth true
  18. * @menu true
  19. */
  20. public function index(){
  21. $this->title='新闻列表';
  22. $this->class = DataXwCategory::where('status',1)->select();
  23. $category_id = input('category_id');
  24. $where = [];
  25. if($category_id != ''){
  26. $where['category_id'] = $category_id;
  27. }
  28. DataXw::mQuery()
  29. ->with('category')
  30. ->where($where)
  31. ->withCount(['comments','likes'])
  32. ->like('title')->dateBetween('create_time')->layTable();
  33. }
  34. /**
  35. * 新闻添加
  36. * @auth true
  37. * @menu true
  38. */
  39. public function add(){
  40. $this->xw_vali();
  41. $this->assign('category',DataXwCategory::show()->select());
  42. DataXw::mForm('form');
  43. }
  44. /**
  45. * 新闻编辑
  46. * @auth true
  47. * @menu true
  48. */
  49. public function edit(){
  50. $this->xw_vali();
  51. $this->assign('category',DataXwCategory::show()->select());
  52. DataXw::mForm('form');
  53. }
  54. protected function xw_vali(){
  55. if($this->request->isPost()){
  56. $this->_vali([
  57. 'title.max:50'=>'标题过长',
  58. ]);
  59. }
  60. }
  61. /**
  62. * 新闻删除
  63. * @auth true
  64. * @menu true
  65. */
  66. public function remove(){
  67. $ids=$this->request->post('id');
  68. DataXw::whereIn('id',$ids)->select()->each(function ($d){$d->delete();});
  69. $this->success('删除成功');
  70. }
  71. /**
  72. * 新闻分类
  73. * @auth true
  74. * @menu true
  75. */
  76. public function category(){
  77. $this->title='分类';
  78. DataXwCategory::mQuery()
  79. ->like('name')
  80. ->layTable();
  81. }
  82. /**
  83. * 新闻分类添加
  84. * @auth true
  85. * @menu true
  86. */
  87. public function category_add(){
  88. if($this->request->isPost()) {
  89. $this->cate_vali();
  90. }
  91. DataXwCategory::mForm('form_category');
  92. }
  93. protected function cate_vali(){
  94. $this->_vali([
  95. 'name.require'=>'分类名必须',
  96. 'name.max:50'=>'分类名过长',
  97. 'sort.require'=>'排序必须',
  98. 'sort.egt:0'=>'排序必须大于0',
  99. 'sort.integer'=>'排序必须是数字',
  100. ]);
  101. }
  102. /**
  103. * 新闻分类编辑
  104. * @auth true
  105. * @menu true
  106. */
  107. public function category_edit(){
  108. if($this->request->isPost()) {
  109. $this->cate_vali();
  110. }
  111. DataXwCategory::mForm('form_category');
  112. }
  113. /**
  114. * 新闻分类删除
  115. * @auth true
  116. * @menu true
  117. */
  118. public function category_del(){
  119. $id=$this->request->post('id');
  120. $all=DataXwCategory::whereIn('id',$id)->select();
  121. $all->each(function ($c){
  122. $c->delete();
  123. });
  124. $this->success('删除成功');
  125. }
  126. /**
  127. * 新闻分类上下架
  128. * @auth true
  129. * @menu true
  130. */
  131. public function cate_state(){
  132. DataXwCategory::mSave([
  133. 'status'=>$this->request->post('status'),
  134. ]);
  135. }
  136. /**
  137. * 新闻上下架
  138. * @auth true
  139. * @menu true
  140. */
  141. public function state(){
  142. DataXw::mSave([
  143. 'status'=>$this->request->post('status'),
  144. ]);
  145. }
  146. /**
  147. * 新闻广告列表
  148. * @auth true
  149. * @menu true
  150. */
  151. public function ad_index(){
  152. $this->title='广告列表';
  153. DataXwAd::mQuery()->layTable();
  154. }
  155. /**
  156. * 新闻广告添加
  157. * @auth true
  158. * @menu true
  159. */
  160. public function ad_add(){
  161. $this->ad_vali();
  162. $this->assign('category',DataXwCategory::show()->select());
  163. DataXwAd::mForm('ad_form');
  164. }
  165. /**
  166. * 新闻广告编辑
  167. * @auth true
  168. * @menu true
  169. */
  170. public function ad_edit(){
  171. $this->ad_vali();
  172. $this->assign('category',DataXwCategory::show()->select());
  173. DataXwAd::mForm('ad_form');
  174. }
  175. protected function ad_vali(){
  176. if($this->request->isPost()){
  177. $this->_vali([
  178. 'title.max:50'=>'标题过长',
  179. ]);
  180. }
  181. }
  182. /**
  183. * 新闻广告删除
  184. * @auth true
  185. * @menu true
  186. */
  187. public function ad_del(){
  188. $ids=$this->request->post('id');
  189. DataXwAd::whereIn('id',$ids)->select()->each(function ($d){$d->delete();});
  190. $this->success('删除成功');
  191. }
  192. /**
  193. * 新闻广告上下架
  194. * @auth true
  195. * @menu true
  196. */
  197. public function ad_state(){
  198. DataXwAd::mSave([
  199. 'status'=>$this->request->post('status'),
  200. ]);
  201. }
  202. public function _form_filter($vo){
  203. if ($this->request->isGet()) {
  204. }elseif ($this->request->isPost()){
  205. $vo['content'] = $this->textcl($vo['content']);
  206. // $vo['user_id'] = null;
  207. // var_dump($vo);die;
  208. if($this->request->action() == 'edit' || $this->request->action() == 'add'){
  209. isset($vo['id']) ? DataXw::mk()->where('id',$vo['id'])->update($vo) : DataXw::mk()->insert($vo);
  210. $this->success('数据更新成功',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
  211. }else if(($this->request->action() == 'ad_add' || $this->request->action() == 'ad_edit')){
  212. isset($vo['id']) ? DataXwAd::mk()->where('id',$vo['id'])->update($vo) : DataXwAd::mk()->insert($vo);
  213. $this->success('数据更新成功',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
  214. }
  215. }
  216. }
  217. /**
  218. * 表单结果处理
  219. * @param boolean $state
  220. */
  221. protected function _form_result(bool $state,array $data)
  222. {
  223. if ($state) {
  224. $this->success('保存成功1!',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
  225. }
  226. }
  227. //视频加封面(阿里云文件)
  228. public function textcl($string = ''){
  229. // $string = '<div>
  230. // <video controls="controls" width="100%"><source src="https://shipcccn.oss-cn-shanghai.aliyuncs.com/dd/天文学家系列.mp4" type="audio/mp4" /></video>
  231. // </div>
  232. //
  233. // <p>11111</p>
  234. //
  235. // <div>
  236. // <video controls="controls" width="100%"><source src="https://shipcccn.oss-cn-shanghai.aliyuncs.com/dd/96326ec38bb7761ef74b7623e94e886c.mp4" type="audio/mp4" /></video>
  237. // </div>
  238. //
  239. // <p>111216546485465468</p>'; // 原始字符串
  240. $position = strpos($string, '<video '); // 获取'video'在字符串中的位置
  241. if ($position !== false) {
  242. preg_match_all('/<video[^>]*>(.*?)<\/video>/s', $string, $matches);
  243. if (isset($matches[1])) {
  244. // echo "视频标签内容为:" . htmlspecialchars($matches[0][1]);die;
  245. } else {
  246. return $string;
  247. // echo "未找到视频标签";
  248. }
  249. foreach ($matches[0] as $k => $v){
  250. //获取src部分 拼凑封面地址
  251. $pattern_src = '/\bsrc\b\s*=\s*[\'\"]?([^\'\"]*)[\'\"]?/i';
  252. preg_match_all($pattern_src,$matches[0][$k],$matchSrc);
  253. $url = $matchSrc[1][0];
  254. $url = $url.'?x-oss-process=video/snapshot,t_10000,m_fast';
  255. $replacement_d = '<video controls="controls" poster="'.$url.'" width="100%">';
  256. //第一次替换
  257. $originalString = $matches[0][$k]; //这是原始字符串
  258. $replacement = $replacement_d; //被替换后的内容
  259. $searchValue = '<video controls="controls" width="100%">'; //要替换的部分
  260. // 将 $searchValue 替换为 $replacement
  261. $newString = str_replace($searchValue, $replacement, $originalString);
  262. //第二次替换
  263. $originalString = $string; //这是原始字符串
  264. $replacement = $newString; //被替换后的内容
  265. $searchValue = $matches[0][$k]; //要替换的部分
  266. $newString = str_replace($searchValue, $replacement, $originalString);
  267. $string = $newString;
  268. }
  269. //echo $newString;die; // 输出结果为"这是被替换后的内容";
  270. return $newString;
  271. } else {
  272. return $string;
  273. // echo "未找到'video'关键词";
  274. }
  275. }
  276. /**
  277. * 新闻报道设置
  278. * @auth true
  279. * @menu true
  280. */
  281. public function config(){
  282. if($this->request->isGet()){
  283. $this->title='新闻报道设置';
  284. $this->assign('vo',sysconf('config_xw.'));
  285. $this->fetch();
  286. }else{
  287. $data=$this->_vali([
  288. 'split_num.require'=>'间隔数必须',
  289. 'user_avatar.require'=>'头像数必须',
  290. 'user_avatar.url'=>'头像链接有误',
  291. 'username.require'=>'名称必须',
  292. 'split_num.gt:0'=>'间隔数必须大于0',
  293. ]);
  294. sysconf('config_xw',$data);
  295. $this->success('保存成功');
  296. }
  297. }
  298. }