SyncNews.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/12
  6. * Time: 上午9:57
  7. * 同步资讯
  8. */
  9. namespace backend\server;
  10. use Imagine\Image\ManipulatorInterface;
  11. use yii\imagine\Image;
  12. use common\models\CategoryWatermark;
  13. use Yii;
  14. use common\models\News;
  15. use linslin\yii2\curl;
  16. class SyncNews
  17. {
  18. public $type = null;
  19. public function NewsList($input)
  20. {
  21. if (!empty($input['site']) && is_array($input['site'])) {
  22. $rows = News::find()->andWhere(['pfg_news.id' => $input['id']])
  23. ->select(['pfg_house.name as h_name', 'pfg_category_news.news_name', 'pfg_category_city.city_name', 'pfg_news.*'])
  24. ->leftJoin('pfg_house', 'pfg_news.hid = pfg_house.id')
  25. ->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id')
  26. ->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id')
  27. ->asArray()
  28. ->all();
  29. if ($rows) {
  30. foreach ($rows as &$val) {
  31. if (!empty($val['thumb'])) {
  32. $val['thumb'] = json_decode($val['thumb'], true);
  33. }
  34. }
  35. }
  36. $curl = new curl\Curl();
  37. foreach ($input['site'] as $v) {
  38. $response = $curl->setPostParams([
  39. 'data' => $rows,
  40. 'url' => Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['newsthumb'],
  41. 'source' => '11111'
  42. ])->post($v);
  43. }
  44. return $response;
  45. }
  46. }
  47. //资讯同步接收
  48. public function newsOpe($input)
  49. {
  50. $this->type = $input['type'];
  51. $oss_url = $input['url'];//资讯缩略图url
  52. $data = $input['data'];//资讯信息
  53. try {
  54. $Transaction = Yii::$app->db->beginTransaction();
  55. $arr = [];
  56. $arr['subject'] = $data['subject'];
  57. $arr['thumb'] = $this->PullImg(json_decode($data['thumb'], true), $oss_url);
  58. // $arr['thumb'] = $data['thumb'];
  59. $arr['abstract'] = $data['abstract'];
  60. $arr['author'] = $data['author'];
  61. if(trim($data['source']) == '同步站'){
  62. $arr['source'] = '易购房';
  63. }else{
  64. $arr['source'] = $data['source'];
  65. }
  66. $arr['sort'] = $data['sort'];
  67. $arr['clicks'] = $data['clicks'];
  68. $arr['state'] = $data['state'];
  69. if (isset($data['city_name']) && !empty($data['city_name'])) {
  70. $arr['city'] = $this->CityColumn($data['city_name'], $data['level']);
  71. }
  72. if ((isset($data['house_name']) || isset($data['unid'])) && !empty($data['house_name'])) {
  73. $arr['hid'] = $this->HouseColumn($data['house_name'], $data['unid']);
  74. }
  75. $arr['category'] = $this->NewsColumn($data['news_category']);
  76. $arr['content'] = $data['content'];
  77. $arr['del'] = $data['del'];
  78. $arr['create_at'] = $_SERVER['REQUEST_TIME'];
  79. $arr['update_at'] = $_SERVER['REQUEST_TIME'];
  80. $arr['open_time'] = $data['open_time'];
  81. $arr['thumb_size'] = $data['thumb_size'];
  82. $arr['short_subject'] = $data['short_subject'];
  83. $arr['true_click'] = $data['true_click'];
  84. $newsInfo = $this->exetNews($data);
  85. if($this->type == 1){
  86. $news = 'pfg_news';
  87. }elseif($this->type == 2){
  88. // $news = 'pfg_news';
  89. }
  90. if ($newsInfo) {
  91. //资讯存在
  92. $row = Yii::$app->db->createCommand()->update($news, $arr, 'id = ' . $newsInfo['id'])->execute();
  93. $thisid = $newsInfo['id'];
  94. $code = 'edit';
  95. } else {
  96. //资讯不存在
  97. $row = Yii::$app->db->createCommand()->insert($news, $arr)->execute();
  98. $thisid = Yii::$app->db->getLastInsertID();
  99. $code = 'add';
  100. }
  101. if ($row > 0) {
  102. $syncInfo = [];
  103. $syncInfo['this_content'] = json_encode($newsInfo);//原数据
  104. $syncInfo['content'] = json_encode($arr);//同步数据
  105. $syncInfo['audit_name'] = $input['auditor_name'];//审核人
  106. $syncInfo['unid'] = $data['unid'];
  107. $syncInfo['own_newsid'] = $thisid;
  108. $syncInfo['type'] = $this->type;
  109. $syncInfo['code'] = $code;
  110. $sync = Yii::$app->db->createCommand()->insert('pfg_syncnewsrecord', $syncInfo)->execute();
  111. if ($sync > 0) {
  112. $Transaction->commit();
  113. return true;
  114. } else {
  115. $Transaction->rollBack();
  116. return '添加记录表失败';
  117. }
  118. } else {
  119. $Transaction->rollBack();
  120. return '插入/修改信息失败(可能已存在完全相同的资讯信息)';
  121. }
  122. } catch (\Exception $e) {
  123. return $e->getMessage();
  124. }
  125. }
  126. /*
  127. *检测资讯是否存在
  128. * */
  129. public function exetnews($data)
  130. {
  131. if($this->type == 1){
  132. $state = \common\models\Syncnewsrecord::find()->andWhere(['unid' => $data['unid']])->one();
  133. if(!empty($state)){
  134. return \common\models\News::find()->andWhere(['id' => $state['own_newsid']])->asArray()->one();
  135. }
  136. return \common\models\News::find()->andWhere(['subject' => $data['subject']])->one();
  137. }elseif($this->type == 2){
  138. }
  139. }
  140. /*
  141. * 转换资讯栏目
  142. */
  143. private function NewsColumn($name)
  144. {
  145. if($this->type == 1){
  146. $model = \common\models\CategoryNews::find()->andWhere(['news_name' => $name])->andWhere(['del' => 1])->one();
  147. if ($model) {
  148. return $model['id'];
  149. } else {
  150. $rows = new \common\models\CategoryNews();
  151. $rows->news_name = $name;
  152. if ($rows->save()) {
  153. return $rows->attributes['id'];
  154. }
  155. }
  156. }elseif($this->type == 2){
  157. }
  158. throw new \Exception('未检测到资讯栏目:' . $name);
  159. }
  160. /*
  161. * 转换区域ID
  162. */
  163. public function CityColumn($city, $level = 1)
  164. {
  165. if (!empty($city)) {
  166. if($this->type == 1){
  167. $row = \common\models\CategoryCity::find()->andFilterWhere(['like','city_name',$city])->andWhere([ 'del' => 1, 'level' => $level])->one();
  168. if ($row) {
  169. return $row['id'];
  170. }
  171. }elseif($this->type ==2){
  172. }
  173. throw new \Exception('未检测到区域:' . $city);
  174. }
  175. }
  176. /*
  177. * 转换楼盘ID
  178. */
  179. public function HouseColumn($hname, $unid)
  180. {
  181. if($this->type == 1){
  182. if (!empty($hname)) {
  183. $row = \common\models\House::find()->andWhere(['name' => $hname, 'del' => 1])->one();
  184. if ($row) {
  185. return $row['id'];
  186. }
  187. }
  188. if (!empty($unid)) {
  189. $row = \common\models\Synchouserecord::find()->andWhere(['unid' => $unid])->one();
  190. if ($row) {
  191. return $row['own_hid'];
  192. }
  193. }
  194. }elseif($this->type == 2){
  195. }
  196. throw new \Exception('未检测到楼盘信息:' . $hname);
  197. }
  198. /*
  199. * 拉取图片
  200. */
  201. private function PullImg($img, $oos)
  202. {
  203. if (is_array($img) && !empty($img)) {
  204. foreach ($img as $val) {
  205. $curl = new curl\Curl();
  206. $resultImg = $curl->get($oos . $val);
  207. $imgUrl = Yii::$app->params['img_url']['news'];
  208. if (!is_file($imgUrl . $val)) {
  209. if (!file_put_contents($imgUrl . $val, $resultImg)) {
  210. throw new \Exception('图片添加失败,请联系管理员');
  211. }
  212. }
  213. }
  214. return json_encode(array_values($img));
  215. }
  216. }
  217. }