123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- // +----------------------------------------------------------------------
- // | 文件说明:幻灯片
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Author: wuwu <15093565100@163.com>
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Date: 2017-5-25
- // +----------------------------------------------------------------------
- namespace api\crontab\controller;
- use api\activity\model\ActivityModel;
- use api\common\Http;
- use api\portal\model\PortalPostModel;
- use api\user\model\UserModel;
- use think\Db;
- class DateController
- {
- /**
- * 主方法
- */
- public function index()
- {
- $this->_colNews();
- $this->_colActive();
- }
- /**
- * 新闻采集
- */
- public function _colNews()
- {
- $page = 0;
- $detail_url = "https://www.qzrcgw.gov.cn/api/app/cms/zc/tbCmsZc/queryById?id=";
- $this->_colNewsChild($page, $detail_url, '晋江');
- $this->_colNewsChild($page, $detail_url, '泉州市');
- }
- /**
- * 新闻列表和详情采集
- */
- private function _colNewsChild($page, $detail_url, $title)
- {
- while (true) {
- $page++;
- $title = urlencode($title);
- $list_url = "https://www.qzrcgw.gov.cn/api/app/cms/zc/tbCmsZc/list?pageNo={$page}&pageSize=20&title={$title}";
- $contents = json_decode(Http::http_get($list_url), true);
- if ($contents['code'] == 200) {
- $list = $contents['page']['list'];
- $ids = array_column($list, 'id');
- $third_ids = PortalPostModel::where('third_id', 'in', $ids)->column('third_id');
- $is_end = true;
- foreach ($list as $v) {
- //查重
- if (in_array($v['id'], $third_ids)) {
- continue;
- } else {
- $is_end = false;
- }
- //获取详情
- $detail_url_true = $detail_url . $v['id'];
- $detail = json_decode(file_get_contents($detail_url_true), true);
- if ($detail['code'] != 200) {
- break;
- }
- //数据处理
- $detail_data = $detail['data'];
- $create = [
- 'third_id' => $detail_data['id'],
- 'user_id' => 1,
- 'create_time' => strtotime($detail_data['createDate']),
- 'update_time' => strtotime($detail_data['updateDate']),
- 'published_time' => strtotime($detail_data['createDate']),
- 'post_title' => $detail_data['title'],
- 'post_source' => $detail_data['source'],
- 'post_content' => $detail_data['cont'],
- 'more' => '{"thumbnail":""}',
- ];
- Db::name('portal_post')->insert($create);
- }
- if ($is_end) {
- break;
- }
- if (count($list) < 20) {
- break;
- }
- }
- }
- }
- //站点活动采集
- public function _colActive()
- {
- $page = 0;
- $detail_url = "https://www.qzrcgw.gov.cn//api/app/cms/rczj/tbCmsRczjHd/queryById?id=";
- $title = urlencode('晋江');
- while (true) {
- $page++;
- $list_url = "https://www.qzrcgw.gov.cn/api/app/cms/rczj/tbCmsRczjHd/list?pageNo={$page}&pageSize=20&title={$title}";
- $contents = json_decode(Http::http_get($list_url), true);
- if ($contents['code'] == 200) {
- $list = $contents['page']['list'];
- $ids = array_column($list, 'id');
- $third_ids = ActivityModel::where('third_id', 'in', $ids)->column('third_id');
- $is_end = true;
- //获取用户id
- $user_ids = [];
- foreach ($list as $v) {
- $user_ids[] = $v['rczj']['id'];
- }
- $user_list = UserModel::where('third_id', 'in', array_unique($user_ids))->column('id', 'third_id');
- foreach ($list as $v) {
- //查重
- if (in_array($v['id'], $third_ids)) {
- continue;
- } else {
- $is_end = false;
- }
- if (empty($user_list[$v['rczj']['id']])) {
- continue;
- }
- //获取详情
- $detail_url_true = $detail_url . $v['id'];
- $detail = json_decode(file_get_contents($detail_url_true), true);
- if ($detail['code'] != 200) {
- break;
- }
- //数据处理
- $detail_data = $detail['data'];
- $main_image = $v['urls'];
- if (strpos($main_image,'|') !== false) {
- $main_image = substr($main_image,0,strpos($main_image,'|'));
- }
- $create = [
- 'third_id' => $detail_data['id'],
- 'title' => $detail_data['title'],
- 'main_image' => 'https://www.qzrcgw.gov.cn' . $main_image,
- 'start_time' => strtotime($v['kssj']),
- 'end_time' => strtotime($v['jssj']),
- 'address' => $detail_data['address'],
- 'user_id' => $user_list[$v['rczj']['id']],
- 'content' => $detail_data['cont'],
- 'create_time' => strtotime($v['createDate']),
- 'status' => 2,
- 'options' => '[]',
- 'signin_code' => $this->_randomStr(16),
- ];
- Db::name('activity')->insert($create);
- }
- if ($is_end) {
- break;
- }
- if (count($list) < 20) {
- break;
- }
- }
- }
- }
- private function _randomStr($length)
- {
- // 密码字符集,可任意添加你需要的字符
- $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $str = '';
- for ($i = 0; $i < $length; $i++) {
- $str .= $chars[mt_rand(0, strlen($chars) - 1)];
- }
- return $str;
- }
- }
|