Tfsync.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/11
  6. * Time: 下午3:08
  7. */
  8. namespace backend\server\news;
  9. use common\models\News;
  10. use linslin\yii2\curl;
  11. use Yii;
  12. class Tfsync
  13. {
  14. public function getData($input)
  15. {
  16. if(!empty($input))
  17. {
  18. $Transaction = Yii::$app->db->beginTransaction();
  19. $arr = [];
  20. foreach ($input as $key=>$val)
  21. {
  22. $r = \common\models\News::find()->andWhere(['subject'=>$val['subject']])->exists();
  23. if($r)
  24. {
  25. throw new \Exception('资讯:'.$val['subject'].' 已存在');
  26. }
  27. $arr[$key]['subject'] = $val['subject'];
  28. $arr[$key]['thumb'] = $this->PullImg($val['thumb']);
  29. $arr[$key]['abstract'] = $val['abstract'];
  30. $arr[$key]['author'] = $val['author'];
  31. $arr[$key]['source'] = "易购房";
  32. $arr[$key]['sort'] = $val['sort'];
  33. $arr[$key]['clicks'] = $val['clicks'];
  34. $arr[$key]['state'] = $val['state'];
  35. if(isset($val['city_name']))
  36. {
  37. $arr[$key]['city'] = $this->CityColumn($val['city_name']);
  38. }
  39. $arr[$key]['category'] = $this->NewsColumn($val['news_name']);
  40. $arr[$key]['create_at'] = $_SERVER['REQUEST_TIME'];
  41. $arr[$key]['update_at'] = $_SERVER['REQUEST_TIME'];
  42. $arr[$key]['content'] = $val['content'];
  43. $arr[$key]['del'] = $val['del'];
  44. $arr[$key]['open_time'] = $val['open_time'];
  45. $arr[$key]['thumb_size'] = $val['thumb_size'];
  46. $arr[$key]['short_subject'] = $val['short_subject'];
  47. $arr[$key]['true_click'] = $val['true_click'];
  48. }
  49. if(!empty($arr))
  50. {
  51. $column = [];
  52. foreach ($arr[0] as $key=>$val)
  53. {
  54. array_push($column,$key);
  55. }
  56. $row = Yii::$app->db->createCommand()->batchInsert('pfg_news',$column,$arr)->execute();
  57. if($row >0)
  58. {
  59. $Transaction->commit();
  60. return true;
  61. }
  62. }
  63. $Transaction->rollBack();
  64. }
  65. throw new \Exception('添加失败');
  66. }
  67. /*
  68. * 转换资讯栏目
  69. */
  70. private function NewsColumn($name)
  71. {
  72. $model = \common\models\CategoryNews::find()->andWhere(['news_name'=>$name])->one();
  73. if($model)
  74. {
  75. return $model['id'];
  76. }
  77. throw new \Exception('未检测到资讯栏目:'.$name);
  78. }
  79. /*
  80. * 转换区域ID
  81. */
  82. public function CityColumn($city)
  83. {
  84. if(!empty($city))
  85. {
  86. $row = \common\models\CategoryCity::find()->andWhere(['city_name'=>$city])->one();
  87. if($row)
  88. {
  89. return $row['id'];
  90. }
  91. throw new \Exception('未检测到区域:'.$city);
  92. }
  93. }
  94. /*
  95. * 拉取图片
  96. */
  97. private function PullImg($img)
  98. {
  99. if(is_array($img) && !empty($img))
  100. {
  101. foreach ($img as $val)
  102. {
  103. $curl = new curl\Curl();
  104. $resultImg = $curl->get('http://img.taofangwang.com/pc/news/'.$val);
  105. $imgUrl = Yii::$app->params['img_url']['news'];
  106. if(is_file($imgUrl.$val))
  107. {
  108. unlink($imgUrl.$val);
  109. }
  110. if(!file_put_contents($imgUrl.$val,$resultImg))
  111. {
  112. throw new \Exception('图片添加失败,请联系管理员');
  113. }
  114. }
  115. return json_encode(array_values($img));
  116. }
  117. }
  118. }