NewsInformation.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/19
  6. * Time: 上午9:22
  7. */
  8. namespace backend\server;
  9. use backend\base\Help;
  10. use Yii;
  11. use common\models\News;
  12. class NewsInformation
  13. {
  14. // public $state = [1=>'y', 2=>'n'];
  15. public function getImgResources($path,$url)
  16. {
  17. try{
  18. $aext = explode('.', $url);
  19. $ext = end($aext);
  20. $name = md5(uniqid(microtime())) . '.' . $ext;
  21. $source = @file_get_contents($url);
  22. if($source){
  23. if( file_put_contents($path.$name,$source,FILE_USE_INCLUDE_PATH))
  24. {
  25. return $name;
  26. }
  27. }
  28. }catch (\Exception $e){
  29. // p($e->getMessage());
  30. }
  31. return ;
  32. $state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容
  33. if($state){
  34. $dir=dirname(Yii::$app->BasePath).'/resource/'.Yii::$app->params['httpImg']['newsthumb'];
  35. $filename = $dir.date("dMYHis").'.jpg';//文件名称生成
  36. ob_start();//打开输出
  37. readfile($url);//输出图片文件
  38. $img = ob_get_contents();//得到浏览器输出
  39. ob_end_clean();//清除输出并关闭
  40. $size = strlen($img);//得到图片大小
  41. $fp2 = @fopen($filename, "a");
  42. //file_put_contents ($filename,$img);
  43. fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名
  44. fclose($fp2);
  45. return date("dMYHis").'.jpg';
  46. }
  47. else{
  48. return 0;
  49. }
  50. }
  51. public function CreateNews()
  52. {
  53. //处理图片
  54. $url = Yii::$app->params['img_url']['news'];
  55. if(!empty($_FILES)){
  56. $row = \backend\server\UploadFile::InstancesImgName('img',$url); //[0] => 3dc12b64b0efc4bdafa543e65c8a9ec2.jpg
  57. }else{
  58. $row[0]=$this->getImgResources(Yii::$app->request->post('img')[0]);
  59. }
  60. if(is_array($row))
  61. {
  62. $model = new News();
  63. $model->scenario = 'add';
  64. $model->thumb = json_encode($row);
  65. $newModel = $model->Authenticator(Yii::$app->request->post());
  66. if(is_object($newModel))
  67. {
  68. //处理水印
  69. if(Yii::$app->request->post('thumb_watermark') == 1 )
  70. {
  71. $ImgSet = new SetUploadImg();
  72. $ImgSet->SetImgWatermark($row,$url);
  73. }
  74. //压缩图片
  75. $compressParams = [];
  76. $compressParams['data']['imgname'] = $row;;
  77. $compressParams['data']['url'] = $url;
  78. $compressParams['data']['source'] = '资讯@添加';
  79. $this->onCompress($compressParams);
  80. if($newModel->insert() == true)
  81. {
  82. $UserNews = new \common\models\UserNews();
  83. $UserNews->uid = Yii::$app->session['user_info']['uid'];
  84. $UserNews->nid = $newModel->attributes['id'];
  85. $UserNews->create_at = $_SERVER['REQUEST_TIME'];
  86. $UserNews->insert();
  87. return true;
  88. }
  89. else
  90. {
  91. UploadFile::delImg($url,$row);
  92. }
  93. }
  94. return $newModel;
  95. }
  96. return $row;
  97. }
  98. public function NewsEdit()
  99. {
  100. $model = new News();
  101. $validateData = $model->Authenticator(Yii::$app->request->post());
  102. if(is_object($validateData))
  103. {
  104. $row = $model->FindById(Yii::$app->request->post('id'));
  105. if($row != null)
  106. {
  107. $delIdentifying = false;
  108. $row['thumb'] = json_decode($row['thumb'],true);
  109. $delimg = Yii::$app->request->post('delimg'); //需要删除的数组
  110. $ImgUrl = Yii::$app->params['img_url']['news'];
  111. $arrImg = [];
  112. if(!empty($delimg))
  113. {
  114. $arrImg = array_diff($row['thumb'],$delimg); //需要保留的数组 如果删除全部图片 返回空数组
  115. if(empty($arrImg))
  116. {
  117. $delIdentifying = true; //标识是否删除所有图片
  118. }
  119. foreach ($delimg as $val)
  120. {
  121. UploadFile::delImg($ImgUrl,$val);
  122. }
  123. }
  124. else
  125. {
  126. $arrImg = $row['thumb'];
  127. }
  128. //$imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl);
  129. if(!empty($_FILES)){
  130. $imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl); //[0] => 3dc12b64b0efc4bdafa543e65c8a9ec2.jpg
  131. }else{
  132. $imgArr[0]=$this->getImgResources(Yii::$app->request->post('img')[0]);
  133. if($imgArr[0]==0){
  134. $imgArr = \backend\server\UploadFile::InstancesImgName('img',$ImgUrl);
  135. }
  136. }
  137. if(is_array($imgArr))
  138. {
  139. $arrImg = array_merge($imgArr,$arrImg);
  140. if(Yii::$app->request->post('thumb_watermark') == 1 )
  141. {
  142. $ImgSet = new SetUploadImg();
  143. $ImgSet->SetImgWatermark($imgArr,$ImgUrl);
  144. }
  145. //压缩图片
  146. $compressParams = [];
  147. $compressParams['data']['imgname'] = $imgArr;;
  148. $compressParams['data']['url'] = $ImgUrl;
  149. $compressParams['data']['source'] = '资讯@修改';
  150. $this->onCompress($compressParams);
  151. }
  152. $setAttr = Help::SetAttr(Yii::$app->request->post(),$validateData,$row);
  153. if(!empty($arrImg) || $delIdentifying == true)
  154. {
  155. $arrImg=array_values($arrImg);
  156. $setAttr->thumb = json_encode($arrImg);
  157. }
  158. else
  159. {
  160. $setAttr->thumb = json_encode($setAttr->thumb); //没有修改图片的情况下
  161. }
  162. if($setAttr->update() == true) return true;
  163. }
  164. }
  165. return $validateData;
  166. }
  167. public function NewsList()
  168. {
  169. $model = new News();
  170. $model->id = Yii::$app->request->post('id');
  171. // $model->hid = Yii::$app->request->post('hid');
  172. $model->subject = Yii::$app->request->post('name');
  173. $model->category = Yii::$app->request->post('news');
  174. $model->city = Yii::$app->request->post('city');
  175. $model->author = Yii::$app->request->post('author');
  176. $rows['data'] = $model->getList(Yii::$app->request->post());
  177. $rows['count'] = $model->Total();
  178. if($rows != null)
  179. {
  180. foreach ($rows['data'] as &$val)
  181. {
  182. $val['create_at'] = date('Y-m-d',$val['create_at']);
  183. $val['state'] =Yii::$app->params['newsStatus'][$val['state']];
  184. }
  185. return $rows;
  186. }
  187. return false;
  188. }
  189. public function delNews($swith = 1)
  190. {
  191. $id = Yii::$app->request->post('id');
  192. switch ($swith){
  193. case 1:
  194. $result = \common\models\News::updateAll(['del'=>2],['id'=>$id]);
  195. // $row->del = 2;
  196. // if(is_array($id))
  197. // {
  198. //
  199. // }
  200. \common\models\HouseNews::updateAll(['del'=>2],['nid'=>$id]);
  201. break;
  202. case 2:
  203. $model = new News();
  204. $row = $model->FindByIdSort($id);
  205. if($row == null) return;
  206. if($row->state == 2)
  207. {
  208. $row->state = 1;
  209. }
  210. else if($row->state == 1)
  211. {
  212. $row->state = 2;
  213. }
  214. $result = $row->update();
  215. break;
  216. }
  217. if($result > 0 ) return true;
  218. }
  219. // public function SetNewsState()
  220. // {
  221. // $model = new News();
  222. // $row = $model->FindById(Yii::$app->request->post('id'));
  223. // $row->state = 2;
  224. // }
  225. //压缩图片
  226. private function onCompress($data)
  227. {
  228. $model = new \backend\event\TinifyEvent();
  229. $model->CompressImg($data);
  230. }
  231. }