DateController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 文件说明:幻灯片
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: wuwu <15093565100@163.com>
  8. // +----------------------------------------------------------------------
  9. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | Date: 2017-5-25
  12. // +----------------------------------------------------------------------
  13. namespace api\crontab\controller;
  14. use api\activity\model\ActivityModel;
  15. use api\common\Http;
  16. use api\portal\model\PortalPostModel;
  17. use api\user\model\UserModel;
  18. use think\Db;
  19. class DateController
  20. {
  21. /**
  22. * 主方法
  23. */
  24. public function index()
  25. {
  26. $this->_colNews();
  27. $this->_colActive();
  28. }
  29. /**
  30. * 新闻采集
  31. */
  32. public function _colNews()
  33. {
  34. $page = 0;
  35. $detail_url = "https://www.qzrcgw.gov.cn/api/app/cms/zc/tbCmsZc/queryById?id=";
  36. $this->_colNewsChild($page, $detail_url, '晋江');
  37. $this->_colNewsChild($page, $detail_url, '泉州市');
  38. }
  39. /**
  40. * 新闻列表和详情采集
  41. */
  42. private function _colNewsChild($page, $detail_url, $title)
  43. {
  44. while (true) {
  45. $page++;
  46. $title = urlencode($title);
  47. $list_url = "https://www.qzrcgw.gov.cn/api/app/cms/zc/tbCmsZc/list?pageNo={$page}&pageSize=20&title={$title}";
  48. $contents = json_decode(Http::http_get($list_url), true);
  49. if ($contents['code'] == 200) {
  50. $list = $contents['page']['list'];
  51. $ids = array_column($list, 'id');
  52. $third_ids = PortalPostModel::where('third_id', 'in', $ids)->column('third_id');
  53. $is_end = true;
  54. foreach ($list as $v) {
  55. //查重
  56. if (in_array($v['id'], $third_ids)) {
  57. continue;
  58. } else {
  59. $is_end = false;
  60. }
  61. //获取详情
  62. $detail_url_true = $detail_url . $v['id'];
  63. $detail = json_decode(file_get_contents($detail_url_true), true);
  64. if ($detail['code'] != 200) {
  65. break;
  66. }
  67. //数据处理
  68. $detail_data = $detail['data'];
  69. $create = [
  70. 'third_id' => $detail_data['id'],
  71. 'user_id' => 1,
  72. 'create_time' => strtotime($detail_data['createDate']),
  73. 'update_time' => strtotime($detail_data['updateDate']),
  74. 'published_time' => strtotime($detail_data['createDate']),
  75. 'post_title' => $detail_data['title'],
  76. 'post_source' => $detail_data['source'],
  77. 'post_content' => $detail_data['cont'],
  78. 'more' => '{"thumbnail":""}',
  79. ];
  80. Db::name('portal_post')->insert($create);
  81. }
  82. if ($is_end) {
  83. break;
  84. }
  85. if (count($list) < 20) {
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. //站点活动采集
  92. public function _colActive()
  93. {
  94. $page = 0;
  95. $detail_url = "https://www.qzrcgw.gov.cn//api/app/cms/rczj/tbCmsRczjHd/queryById?id=";
  96. $title = urlencode('晋江');
  97. while (true) {
  98. $page++;
  99. $list_url = "https://www.qzrcgw.gov.cn/api/app/cms/rczj/tbCmsRczjHd/list?pageNo={$page}&pageSize=20&title={$title}";
  100. $contents = json_decode(Http::http_get($list_url), true);
  101. if ($contents['code'] == 200) {
  102. $list = $contents['page']['list'];
  103. $ids = array_column($list, 'id');
  104. $third_ids = ActivityModel::where('third_id', 'in', $ids)->column('third_id');
  105. $is_end = true;
  106. //获取用户id
  107. $user_ids = [];
  108. foreach ($list as $v) {
  109. $user_ids[] = $v['rczj']['id'];
  110. }
  111. $user_list = UserModel::where('third_id', 'in', array_unique($user_ids))->column('id', 'third_id');
  112. foreach ($list as $v) {
  113. //查重
  114. if (in_array($v['id'], $third_ids)) {
  115. continue;
  116. } else {
  117. $is_end = false;
  118. }
  119. if (empty($user_list[$v['rczj']['id']])) {
  120. continue;
  121. }
  122. //获取详情
  123. $detail_url_true = $detail_url . $v['id'];
  124. $detail = json_decode(file_get_contents($detail_url_true), true);
  125. if ($detail['code'] != 200) {
  126. break;
  127. }
  128. //数据处理
  129. $detail_data = $detail['data'];
  130. $main_image = $v['urls'];
  131. if (strpos($main_image,'|') !== false) {
  132. $main_image = substr($main_image,0,strpos($main_image,'|'));
  133. }
  134. $create = [
  135. 'third_id' => $detail_data['id'],
  136. 'title' => $detail_data['title'],
  137. 'main_image' => 'https://www.qzrcgw.gov.cn' . $main_image,
  138. 'start_time' => strtotime($v['kssj']),
  139. 'end_time' => strtotime($v['jssj']),
  140. 'address' => $detail_data['address'],
  141. 'user_id' => $user_list[$v['rczj']['id']],
  142. 'content' => $detail_data['cont'],
  143. 'create_time' => strtotime($v['createDate']),
  144. 'status' => 2,
  145. 'options' => '[]',
  146. 'signin_code' => $this->_randomStr(16),
  147. ];
  148. Db::name('activity')->insert($create);
  149. }
  150. if ($is_end) {
  151. break;
  152. }
  153. if (count($list) < 20) {
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. private function _randomStr($length)
  160. {
  161. // 密码字符集,可任意添加你需要的字符
  162. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  163. $str = '';
  164. for ($i = 0; $i < $length; $i++) {
  165. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  166. }
  167. return $str;
  168. }
  169. }