TdcSearchReport.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * 用户阅读行为回传
  4. * Created by: PhpStorm
  5. * User: lytian
  6. * Date: 2020/3/5
  7. * Time: 14:15
  8. */
  9. namespace app\admin\command;
  10. use app\main\service\GdtService;
  11. use function GuzzleHttp\Psr7\str;
  12. use think\Config;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\input\Option;
  16. use think\console\Output;
  17. use think\Db;
  18. use think\Env;
  19. use think\Request;
  20. class TdcSearchReport extends Command
  21. {
  22. public function configure()
  23. {
  24. $this->setName('TdcSearchReport')
  25. ->setDescription('用户搜索行为回传');
  26. }
  27. public function execute(Input $input, Output $output)
  28. {
  29. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  30. $id = 0;
  31. $go = true;
  32. $dt = date("Ymd", strtotime("-2 days"));
  33. $dbConfig = Config::get("expanddb");
  34. $db = Db::connect($dbConfig);
  35. $tdcAccount = model("TdcAccount")->getInfoByAdminId();
  36. $adId = Env::get("tdc.user_action_set_id");
  37. if (empty($tdcAccount)) {
  38. exit("未配置TDC账号,脚本执行结束");
  39. }
  40. $postData = [];
  41. while ($go) {
  42. $sql = "SELECT * FROM gdt_book_search WHERE id > {$id} AND dt = '{$dt}' ORDER BY id ASC LIMIT 1000";
  43. $rows = $db->query($sql);
  44. if ($rows) {
  45. foreach ($rows as $row) {
  46. $id = $row['id'];
  47. if (count($postData) > 30) {
  48. //每30条请求一次接口
  49. GdtService::instance()->apiUserActionAdd($tdcAccount['account_id'], $tdcAccount['access_token'], $postData, ['ad_source_id' => $adId]);
  50. $postData = [];
  51. }
  52. $postData[] = $this->parseData($row);
  53. }
  54. } else {
  55. $go = false;
  56. }
  57. }
  58. if (count($postData) > 0) {
  59. GdtService::instance()->apiUserActionAdd($tdcAccount['account_id'], $tdcAccount['access_token'], $postData, ['ad_source_id' => $adId]);
  60. $postData = [];
  61. }
  62. exit("脚本执行结束");
  63. }
  64. private function parseData($data)
  65. {
  66. return
  67. [
  68. "action_time" => time(),
  69. "user_id" => [
  70. "wechat_openid" => $data['openid'],
  71. "wechat_app_id" => $data['appid'],
  72. ],
  73. "action_type" => 'SEARCH',
  74. 'action_param' => [
  75. 'content_type' => 'readings',
  76. 'object' => 'product',
  77. 'product_id' => strval($data['book_id']),
  78. 'query' => strval($data['book_name']),
  79. 'first_category_id' => intval($data['first_category_id']),
  80. 'first_category_name' => strval($data['first_category_name']),
  81. 'second_category_id' => intval($data['second_category_id']),
  82. 'second_category_name' => strval($data['second_category_name']),
  83. ],
  84. ];
  85. }
  86. }