TdcReadBookReport.php 3.2 KB

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