123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- namespace app\admin\controller;
- use app\data\model\DataXw;
- use app\data\model\DataXwAd;
- use app\data\model\DataXwCategory;
- use app\data\controller\api\auth\Center;
- use think\admin\Controller;
- /**
- * 新闻报道
- * Class Xw
- * @package app\admin\controller\Xw
- */
- class Xw extends Controller
- {
- /**
- * 新闻列表
- * @auth true
- * @menu true
- */
- public function index(){
- $this->title='新闻列表';
- $this->class = DataXwCategory::where('status',1)->select();
- $category_id = input('category_id');
- $where = [];
- if($category_id != ''){
- $where['category_id'] = $category_id;
- }
- DataXw::mQuery()
- ->with('category')
- ->where($where)
- ->withCount(['comments','likes'])
- ->like('title')->dateBetween('create_time')->layTable();
- }
- /**
- * 新闻添加
- * @auth true
- * @menu true
- */
- public function add(){
- $this->xw_vali();
- $this->assign('category',DataXwCategory::show()->select());
- DataXw::mForm('form');
- }
- /**
- * 新闻编辑
- * @auth true
- * @menu true
- */
- public function edit(){
- $this->xw_vali();
- $this->assign('category',DataXwCategory::show()->select());
- DataXw::mForm('form');
- }
- protected function xw_vali(){
- if($this->request->isPost()){
- $this->_vali([
- 'title.max:50'=>'标题过长',
- ]);
- }
- }
- /**
- * 新闻删除
- * @auth true
- * @menu true
- */
- public function remove(){
- $ids=$this->request->post('id');
- DataXw::whereIn('id',$ids)->select()->each(function ($d){$d->delete();});
- $this->success('删除成功');
- }
- /**
- * 新闻分类
- * @auth true
- * @menu true
- */
- public function category(){
- $this->title='分类';
- DataXwCategory::mQuery()
- ->like('name')
- ->layTable();
- }
- /**
- * 新闻分类添加
- * @auth true
- * @menu true
- */
- public function category_add(){
- if($this->request->isPost()) {
- $this->cate_vali();
- }
- DataXwCategory::mForm('form_category');
- }
- protected function cate_vali(){
- $this->_vali([
- 'name.require'=>'分类名必须',
- 'name.max:50'=>'分类名过长',
- 'sort.require'=>'排序必须',
- 'sort.egt:0'=>'排序必须大于0',
- 'sort.integer'=>'排序必须是数字',
- ]);
- }
- /**
- * 新闻分类编辑
- * @auth true
- * @menu true
- */
- public function category_edit(){
- if($this->request->isPost()) {
- $this->cate_vali();
- }
- DataXwCategory::mForm('form_category');
- }
- /**
- * 新闻分类删除
- * @auth true
- * @menu true
- */
- public function category_del(){
- $id=$this->request->post('id');
- $all=DataXwCategory::whereIn('id',$id)->select();
- $all->each(function ($c){
- $c->delete();
- });
- $this->success('删除成功');
- }
- /**
- * 新闻分类上下架
- * @auth true
- * @menu true
- */
- public function cate_state(){
- DataXwCategory::mSave([
- 'status'=>$this->request->post('status'),
- ]);
- }
- /**
- * 新闻上下架
- * @auth true
- * @menu true
- */
- public function state(){
- DataXw::mSave([
- 'status'=>$this->request->post('status'),
- ]);
- }
- /**
- * 新闻广告列表
- * @auth true
- * @menu true
- */
- public function ad_index(){
- $this->title='广告列表';
- DataXwAd::mQuery()->layTable();
- }
- /**
- * 新闻广告添加
- * @auth true
- * @menu true
- */
- public function ad_add(){
- $this->ad_vali();
- $this->assign('category',DataXwCategory::show()->select());
- DataXwAd::mForm('ad_form');
- }
- /**
- * 新闻广告编辑
- * @auth true
- * @menu true
- */
- public function ad_edit(){
- $this->ad_vali();
- $this->assign('category',DataXwCategory::show()->select());
- DataXwAd::mForm('ad_form');
- }
- protected function ad_vali(){
- if($this->request->isPost()){
- $this->_vali([
- 'title.max:50'=>'标题过长',
- ]);
- }
- }
- /**
- * 新闻广告删除
- * @auth true
- * @menu true
- */
- public function ad_del(){
- $ids=$this->request->post('id');
- DataXwAd::whereIn('id',$ids)->select()->each(function ($d){$d->delete();});
- $this->success('删除成功');
- }
- /**
- * 新闻广告上下架
- * @auth true
- * @menu true
- */
- public function ad_state(){
- DataXwAd::mSave([
- 'status'=>$this->request->post('status'),
- ]);
- }
- public function _form_filter($vo){
- if ($this->request->isGet()) {
- }elseif ($this->request->isPost()){
- $vo['content'] = $this->textcl($vo['content']);
- // $vo['user_id'] = null;
- // var_dump($vo);die;
- if($this->request->action() == 'edit' || $this->request->action() == 'add'){
- isset($vo['id']) ? DataXw::mk()->where('id',$vo['id'])->update($vo) : DataXw::mk()->insert($vo);
- $this->success('数据更新成功',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
- }else if(($this->request->action() == 'ad_add' || $this->request->action() == 'ad_edit')){
- isset($vo['id']) ? DataXwAd::mk()->where('id',$vo['id'])->update($vo) : DataXwAd::mk()->insert($vo);
- $this->success('数据更新成功',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
- }
- }
- }
- /**
- * 表单结果处理
- * @param boolean $state
- */
- protected function _form_result(bool $state,array $data)
- {
- if ($state) {
- $this->success('保存成功1!',in_array($this->request->action(),['add','edit','ad_add','ad_edit'])? 'javascript:history.back()':'');
- }
- }
- //视频加封面(阿里云文件)
- public function textcl($string = ''){
- // $string = '<div>
- // <video controls="controls" width="100%"><source src="https://shipcccn.oss-cn-shanghai.aliyuncs.com/dd/天文学家系列.mp4" type="audio/mp4" /></video>
- // </div>
- //
- // <p>11111</p>
- //
- // <div>
- // <video controls="controls" width="100%"><source src="https://shipcccn.oss-cn-shanghai.aliyuncs.com/dd/96326ec38bb7761ef74b7623e94e886c.mp4" type="audio/mp4" /></video>
- // </div>
- //
- // <p>111216546485465468</p>'; // 原始字符串
- $position = strpos($string, '<video '); // 获取'video'在字符串中的位置
- if ($position !== false) {
- preg_match_all('/<video[^>]*>(.*?)<\/video>/s', $string, $matches);
- if (isset($matches[1])) {
- // echo "视频标签内容为:" . htmlspecialchars($matches[0][1]);die;
- } else {
- return $string;
- // echo "未找到视频标签";
- }
- foreach ($matches[0] as $k => $v){
- //获取src部分 拼凑封面地址
- $pattern_src = '/\bsrc\b\s*=\s*[\'\"]?([^\'\"]*)[\'\"]?/i';
- preg_match_all($pattern_src,$matches[0][$k],$matchSrc);
- $url = $matchSrc[1][0];
- $url = $url.'?x-oss-process=video/snapshot,t_10000,m_fast';
- $replacement_d = '<video controls="controls" poster="'.$url.'" width="100%">';
- //第一次替换
- $originalString = $matches[0][$k]; //这是原始字符串
- $replacement = $replacement_d; //被替换后的内容
- $searchValue = '<video controls="controls" width="100%">'; //要替换的部分
- // 将 $searchValue 替换为 $replacement
- $newString = str_replace($searchValue, $replacement, $originalString);
- //第二次替换
- $originalString = $string; //这是原始字符串
- $replacement = $newString; //被替换后的内容
- $searchValue = $matches[0][$k]; //要替换的部分
- $newString = str_replace($searchValue, $replacement, $originalString);
- $string = $newString;
- }
- //echo $newString;die; // 输出结果为"这是被替换后的内容";
- return $newString;
- } else {
- return $string;
- // echo "未找到'video'关键词";
- }
- }
- /**
- * 新闻报道设置
- * @auth true
- * @menu true
- */
- public function config(){
- if($this->request->isGet()){
- $this->title='新闻报道设置';
- $this->assign('vo',sysconf('config_xw.'));
- $this->fetch();
- }else{
- $data=$this->_vali([
- 'split_num.require'=>'间隔数必须',
- 'user_avatar.require'=>'头像数必须',
- 'user_avatar.url'=>'头像链接有误',
- 'username.require'=>'名称必须',
- 'split_num.gt:0'=>'间隔数必须大于0',
- ]);
- sysconf('config_xw',$data);
- $this->success('保存成功');
- }
- }
- }
|