12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- /**
- * 用户阅读行为回传
- * Created by: PhpStorm
- * User: lytian
- * Date: 2020/3/5
- * Time: 14:15
- */
- namespace app\admin\command;
- use app\main\service\GdtService;
- use function GuzzleHttp\Psr7\str;
- use think\Config;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use think\Db;
- use think\Env;
- use think\Request;
- class TdcSearchReport extends Command
- {
- public function configure()
- {
- $this->setName('TdcSearchReport')
- ->setDescription('用户搜索行为回传');
- }
- public function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
- $id = 0;
- $go = true;
- $dt = date("Ymd", strtotime("-2 days"));
- $dbConfig = Config::get("expanddb");
- $db = Db::connect($dbConfig);
- $tdcAccount = model("TdcAccount")->getInfoByAdminId();
- $adId = Env::get("tdc.user_action_set_id");
- if (empty($tdcAccount)) {
- exit("未配置TDC账号,脚本执行结束");
- }
- $postData = [];
- while ($go) {
- $sql = "SELECT * FROM gdt_book_search WHERE id > {$id} AND dt = '{$dt}' ORDER BY id ASC LIMIT 1000";
- $rows = $db->query($sql);
- if ($rows) {
- foreach ($rows as $row) {
- $id = $row['id'];
- if (count($postData) > 30) {
- //每30条请求一次接口
- GdtService::instance()->apiUserActionAdd($tdcAccount['account_id'], $tdcAccount['access_token'], $postData, ['ad_source_id' => $adId]);
- $postData = [];
- }
- $postData[] = $this->parseData($row);
- }
- } else {
- $go = false;
- }
- }
- if (count($postData) > 0) {
- GdtService::instance()->apiUserActionAdd($tdcAccount['account_id'], $tdcAccount['access_token'], $postData, ['ad_source_id' => $adId]);
- $postData = [];
- }
- exit("脚本执行结束");
- }
- private function parseData($data)
- {
- return
- [
- "action_time" => time(),
- "user_id" => [
- "wechat_openid" => $data['openid'],
- "wechat_app_id" => $data['appid'],
- ],
- "action_type" => 'SEARCH',
- 'action_param' => [
- 'content_type' => 'readings',
- 'object' => 'product',
- 'product_id' => strval($data['book_id']),
- 'query' => strval($data['book_name']),
- 'first_category_id' => intval($data['first_category_id']),
- 'first_category_name' => strval($data['first_category_name']),
- 'second_category_id' => intval($data['second_category_id']),
- 'second_category_name' => strval($data['second_category_name']),
- ],
- ];
- }
- }
|