123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/3/19
- * Time: 上午9:22
- */
- namespace backend\server;
- use backend\base\Help;
- use Yii;
- use common\models\News;
- class NewsInformation
- {
- // public $state = [1=>'y', 2=>'n'];
- public function getImgResources($path,$url)
- {
- try{
- $aext = explode('.', $url);
- $ext = end($aext);
- $name = md5(uniqid(microtime())) . '.' . $ext;
- $source = @file_get_contents($url);
- if($source){
- if( file_put_contents($path.$name,$source,FILE_USE_INCLUDE_PATH))
- {
- return $name;
- }
- }
- }catch (\Exception $e){
- // p($e->getMessage());
- }
- return ;
- $state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容
- if($state){
- $dir=dirname(Yii::$app->BasePath).'/resource/'.Yii::$app->params['httpImg']['newsthumb'];
- $filename = $dir.date("dMYHis").'.jpg';//文件名称生成
- ob_start();//打开输出
- readfile($url);//输出图片文件
- $img = ob_get_contents();//得到浏览器输出
- ob_end_clean();//清除输出并关闭
- $size = strlen($img);//得到图片大小
- $fp2 = @fopen($filename, "a");
- //file_put_contents ($filename,$img);
- fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名
- fclose($fp2);
- return date("dMYHis").'.jpg';
- }
- else{
- return 0;
- }
- }
- public function CreateNews()
- {
- //处理图片
- $url = Yii::$app->params['img_url']['news'];
- if(!empty($_FILES)){
- $row = \backend\server\UploadFile::InstancesImgName('img',$url); //[0] => 3dc12b64b0efc4bdafa543e65c8a9ec2.jpg
- }else{
- $row[0]=$this->getImgResources(Yii::$app->request->post('img')[0]);
- }
- if(is_array($row))
- {
- $model = new News();
- $model->scenario = 'add';
- $model->thumb = json_encode($row);
- $newModel = $model->Authenticator(Yii::$app->request->post());
- if(is_object($newModel))
- {
- //处理水印
- if(Yii::$app->request->post('thumb_watermark') == 1 )
- {
- $ImgSet = new SetUploadImg();
- $ImgSet->SetImgWatermark($row,$url);
- }
- //压缩图片
- $compressParams = [];
- $compressParams['data']['imgname'] = $row;;
- $compressParams['data']['url'] = $url;
- $compressParams['data']['source'] = '资讯@添加';
- $this->onCompress($compressParams);
- if($newModel->insert() == true)
- {
- $UserNews = new \common\models\UserNews();
- $UserNews->uid = Yii::$app->session['user_info']['uid'];
- $UserNews->nid = $newModel->attributes['id'];
- $UserNews->create_at = $_SERVER['REQUEST_TIME'];
- $UserNews->insert();
- return true;
- }
- else
- {
- UploadFile::delImg($url,$row);
- }
- }
- return $newModel;
- }
- return $row;
- }
- public function NewsEdit()
- {
- $model = new News();
- $validateData = $model->Authenticator(Yii::$app->request->post());
- if(is_object($validateData))
- {
- $row = $model->FindById(Yii::$app->request->post('id'));
- if($row != null)
- {
- $delIdentifying = false;
- $row['thumb'] = json_decode($row['thumb'],true);
- $delimg = Yii::$app->request->post('delimg'); //需要删除的数组
- $ImgUrl = Yii::$app->params['img_url']['news'];
- $arrImg = [];
- if(!empty($delimg))
- {
- $arrImg = array_diff($row['thumb'],$delimg); //需要保留的数组 如果删除全部图片 返回空数组
- if(empty($arrImg))
- {
- $delIdentifying = true; //标识是否删除所有图片
- }
- foreach ($delimg as $val)
- {
- UploadFile::delImg($ImgUrl,$val);
- }
- }
- else
- {
- $arrImg = $row['thumb'];
- }
- //$imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl);
- if(!empty($_FILES)){
- $imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl); //[0] => 3dc12b64b0efc4bdafa543e65c8a9ec2.jpg
- }else{
- $imgArr[0]=$this->getImgResources(Yii::$app->request->post('img')[0]);
- if($imgArr[0]==0){
- $imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl);
- }
- }
- if(is_array($imgArr))
- {
- $arrImg = array_merge($imgArr,$arrImg);
- if(Yii::$app->request->post('thumb_watermark') == 1 )
- {
- $ImgSet = new SetUploadImg();
- $ImgSet->SetImgWatermark($imgArr,$ImgUrl);
- }
- //压缩图片
- $compressParams = [];
- $compressParams['data']['imgname'] = $imgArr;;
- $compressParams['data']['url'] = $ImgUrl;
- $compressParams['data']['source'] = '资讯@修改';
- $this->onCompress($compressParams);
- }
- $setAttr = Help::SetAttr(Yii::$app->request->post(),$validateData,$row);
- if(!empty($arrImg) || $delIdentifying == true)
- {
- $arrImg=array_values($arrImg);
- $setAttr->thumb = json_encode($arrImg);
- }
- else
- {
- $setAttr->thumb = json_encode($setAttr->thumb); //没有修改图片的情况下
- }
- if($setAttr->update() == true) return true;
- }
- }
- return $validateData;
- }
- public function NewsList()
- {
- $model = new News();
- $model->id = Yii::$app->request->post('id');
- // $model->hid = Yii::$app->request->post('hid');
- $model->subject = Yii::$app->request->post('name');
- $model->category = Yii::$app->request->post('news');
- $model->city = Yii::$app->request->post('city');
- $model->author = Yii::$app->request->post('author');
- $rows['data'] = $model->getList(Yii::$app->request->post());
- $rows['count'] = $model->Total();
- if($rows != null)
- {
- foreach ($rows['data'] as &$val)
- {
- $val['create_at'] = date('Y-m-d',$val['create_at']);
- $val['state'] =Yii::$app->params['newsStatus'][$val['state']];
- }
- return $rows;
- }
- return false;
- }
- public function delNews($swith = 1)
- {
- $id = Yii::$app->request->post('id');
- switch ($swith){
- case 1:
- $result = \common\models\News::updateAll(['del'=>2],['id'=>$id]);
- // $row->del = 2;
- // if(is_array($id))
- // {
- //
- // }
- \common\models\HouseNews::updateAll(['del'=>2],['nid'=>$id]);
- break;
- case 2:
- $model = new News();
- $row = $model->FindByIdSort($id);
- if($row == null) return;
- if($row->state == 2)
- {
- $row->state = 1;
- }
- else if($row->state == 1)
- {
- $row->state = 2;
- }
- $result = $row->update();
- break;
- }
- if($result > 0 ) return true;
- }
- // public function SetNewsState()
- // {
- // $model = new News();
- // $row = $model->FindById(Yii::$app->request->post('id'));
- // $row->state = 2;
- // }
- //压缩图片
- private function onCompress($data)
- {
- $model = new \backend\event\TinifyEvent();
- $model->CompressImg($data);
- }
- }
|