GdtAdReport.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/4/28
  6. * Time: 16:38
  7. */
  8. namespace App\Console\Commands\Wechat;
  9. use Illuminate\Console\Command;
  10. use GuzzleHttp\Client;
  11. use Exception;
  12. use Log;
  13. use DB;
  14. use Redis;
  15. class GdtAdReport extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'Wechat:GdtAdReport {--type=}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = 'Command description';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. private $user_action_set_id = [];
  39. private $assess_token = [];
  40. private $error_msg;
  41. /**
  42. * Execute the console command.
  43. *
  44. * @return mixed
  45. */
  46. public function handle()
  47. {
  48. $option = $this->option('type');
  49. if($option == 'order'){
  50. $this->reportOrders();
  51. }
  52. if($option == 'register'){
  53. $this->reportRegister();
  54. }
  55. }
  56. private function reportRegister(){
  57. $action_type = 'REGISTER';
  58. $users = $this->getRegisterData($action_type);
  59. if(!$users) return ;
  60. $client = new Client();
  61. foreach ($users as $item){
  62. $user_action_set_id = $this->getDataSurce($item['appid'],'WEB');
  63. $action_time = strtotime($item['created_at']);
  64. $data = [
  65. 'user_action_set_id'=>$user_action_set_id,
  66. 'url'=>'',
  67. 'action_time'=>$action_time,
  68. 'action_type'=>$action_type,
  69. 'openid'=>$item['openid'],
  70. 'appid'=>$item['appid'],
  71. 'product_id'=>$item['id'],
  72. 'value'=>0,
  73. 'created_at'=>date('Y-m-d H:i:s'),
  74. 'updated_at'=>date('Y-m-d H:i:s'),
  75. 'result_status'=>'FAIL',
  76. 'result_msg'=>'',
  77. 'result'=>'',
  78. ];
  79. $book_info = $this->getBookNameByBid($item['bid']);
  80. $product_id = '';
  81. $product_name = '';
  82. $data['bid'] = $book_info['en_bid'];
  83. if($book_info['en_bid'] && $book_info['book_name']){
  84. $product_id = $book_info['en_bid'];
  85. $data['bid'] = $book_info['en_bid'];
  86. $product_name = $book_info['book_name'].'-'.env('PROJECT_NAME');
  87. $data['product_name'] = $product_name;
  88. $data['url'] = env('GDT_REPORT_URL').'/detail?id='.$book_info['en_bid'];
  89. }
  90. $token = $this->getAccessToken($item['appid']);
  91. if(!$user_action_set_id || !$token) {
  92. $data['result_msg'] = $this->error_msg;
  93. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  94. continue;
  95. };
  96. $result = $this->request($client, $user_action_set_id, $action_time, $action_type, $item['appid'], $item['openid'], 0, $product_name, $product_id,$token,'');
  97. $data['result_status'] = $result['result_status'];
  98. $data['result_msg'] = $result['result_msg'];
  99. $data['result'] = $result['result'];
  100. try{
  101. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  102. if($data['result_status'] == 'SUCCESS'){
  103. DB::connection('api_mysql')->table('distribution_channel_weixin_spread_users')->where('uid',$item['id'])->update([
  104. 'report_status'=>1,
  105. 'updated_at'=>date('Y-m-d H:i:s')
  106. ]);
  107. }
  108. }catch (\Exception $e){}
  109. //second
  110. $data['action_type'] = 'FOLLOW';
  111. $result = $this->request($client, $user_action_set_id, $action_time, 'FOLLOW', $item['appid'], $item['openid'], 0, $product_name, $product_id,$token,'');
  112. $data['result_status'] = $result['result_status'];
  113. $data['result_msg'] = $result['result_msg'];
  114. $data['result'] = $result['result'];
  115. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  116. }
  117. }
  118. private function reportOrders(){
  119. $action_type = 'COMPLETE_ORDER';
  120. $orders = $this->getOrderData($action_type);
  121. if(!$orders) return ;
  122. $client = new Client();
  123. foreach ($orders as $item){
  124. $report_type = $this->transferReportType($item['report_type'],$item['created_at'],$item['id']);
  125. $user_action_set_id = $this->getDataSurce($item['appid'],'WEB');
  126. $action_time = strtotime($item['created_at']);
  127. $data = [
  128. 'user_action_set_id'=>$user_action_set_id,
  129. 'url'=>'',
  130. 'action_time'=>$action_time,
  131. 'action_type'=>$action_type,
  132. 'openid'=>$item['openid'],
  133. 'appid'=>$item['appid'],
  134. 'product_id'=>$item['id'],
  135. 'value'=>$item['price']*100,
  136. 'created_at'=>date('Y-m-d H:i:s'),
  137. 'updated_at'=>date('Y-m-d H:i:s'),
  138. 'result_status'=>'FAIL',
  139. 'result_msg'=>'',
  140. 'result'=>'',
  141. 'object'=>$report_type
  142. ];
  143. $book_info = $this->getBookNameFromOrer($item['id']);
  144. $product_id = '';
  145. $product_name = '';
  146. $data['bid'] = $book_info['en_bid'];
  147. if($book_info['en_bid'] && $book_info['book_name']){
  148. $product_id = $book_info['en_bid'];
  149. $data['bid'] = $book_info['en_bid'];
  150. $product_name = $book_info['book_name'].'-'.env('PROJECT_NAME');
  151. $data['product_name'] = $product_name;
  152. $data['url'] = env('GDT_REPORT_URL').'/detail?id='.$book_info['en_bid'];
  153. }
  154. $token = $this->getAccessToken($item['appid']);
  155. if(!$user_action_set_id || !$token) {
  156. $data['result_msg'] = $this->error_msg;
  157. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  158. continue;
  159. };
  160. $result = $this->request($client, $user_action_set_id, $action_time, $action_type, $item['appid'], $item['openid'], $item['price']*100, $product_name, $product_id,$token,$report_type);
  161. $data['result_status'] = $result['result_status'];
  162. $data['result_msg'] = $result['result_msg'];
  163. $data['result'] = $result['result'];
  164. try{
  165. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  166. if($data['result_status'] == 'SUCCESS'){
  167. DB::connection('api_mysql')->table('distribution_channel_weixin_spread_orders')->where('order_id',$item['id'])->update([
  168. 'report_status'=>1,
  169. 'updated_at'=>date('Y-m-d H:i:s')
  170. ]);
  171. }
  172. }catch (\Exception $e){}
  173. //second
  174. $data['action_type'] = 'PURCHASE';
  175. $result = $this->request($client, $user_action_set_id, $action_time, 'PURCHASE', $item['appid'], $item['openid'], $item['price']*100, $product_name, $product_id,$token,$report_type);
  176. $data['result_status'] = $result['result_status'];
  177. $data['result_msg'] = $result['result_msg'];
  178. $data['result'] = $result['result'];
  179. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  180. try{
  181. if($data['result_status'] == 'SUCCESS'){
  182. DB::connection('api_mysql')->table('distribution_channel_weixin_spread_orders')->where('order_id',$item['id'])->update([
  183. 'report_status'=>1,
  184. 'updated_at'=>date('Y-m-d H:i:s')
  185. ]);
  186. }
  187. }catch (\Exception $e){}
  188. }
  189. }
  190. private function getOrderData($action_type)
  191. {
  192. $data = [];
  193. $start = date('Y-m-d');
  194. if (date('H:i:s') < '02:00:00') {
  195. $start = date('Y-m-d H:i:s', time() - 3 * 3600);
  196. }
  197. $result = DB::connection('api_mysql')->table('distribution_channel_weixin_spread_orders')
  198. ->where('report_status', 0)
  199. ->where('created_at', '>=', $start)
  200. ->select('order_id', 'appid', 'openid', 'price', 'report_status', 'created_at','report_type')
  201. ->get();
  202. if ($result) {
  203. foreach ($result as $item) {
  204. $report_info = DB::table('wechat_advertise_gdt_report_record')
  205. ->where('product_id', $item->order_id)
  206. ->where('action_type', $action_type)
  207. ->where('result_status', 'SUCCESS')
  208. ->count();
  209. if ($report_info) {
  210. try {
  211. DB::connection('api_mysql')->table('distribution_channel_weixin_spread_orders')->where('order_id', $item->order_id)->update([
  212. 'report_status' => 1,
  213. 'updated_at' => date('Y-m-d H:i:s')
  214. ]);
  215. } catch (\Exception $e) {
  216. \Log::info($e);
  217. }
  218. continue;
  219. }
  220. $data[] = ['created_at' => $item->created_at, 'openid' => $item->openid,'report_type'=>$item->report_type,
  221. 'id' => $item->order_id, 'appid' => $item->appid, 'price' => $item->price];
  222. }
  223. }
  224. return $data;
  225. }
  226. private function getRegisterData($action_type)
  227. {
  228. $data = [];
  229. $start = date('Y-m-d');
  230. if (date('H:i:s') < '02:00:00') {
  231. $start = date('Y-m-d H:i:s', time() - 3 * 3600);
  232. }
  233. $result = DB::connection('api_mysql')->table('distribution_channel_weixin_spread_users')
  234. ->where('report_status', 0)
  235. ->where('created_at', '>=', $start)
  236. ->select('uid', 'appid', 'openid', 'report_status', 'created_at', 'bid')
  237. ->get();
  238. if ($result) {
  239. foreach ($result as $item) {
  240. $report_info = DB::table('wechat_advertise_gdt_report_record')
  241. ->where('product_id', $item->uid)
  242. ->where('action_type', $action_type)
  243. ->where('result_status', 'SUCCESS')
  244. ->count();
  245. if ($report_info) {
  246. continue;
  247. }
  248. $data[] = ['created_at' => $item->created_at, 'openid' => $item->openid,
  249. 'id' => $item->uid, 'appid' => $item->appid, 'bid' => $item->bid];
  250. }
  251. }
  252. return $data;
  253. }
  254. private function request(Client $client, $user_action_set_id, $action_time, $action_type, $appid, $openid, $value, $product_name, $product_id,$token,$report_type)
  255. {
  256. $data['result_status'] = 'FAIL';
  257. $data['result_msg'] = '';
  258. $data['result'] = '';
  259. $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token=' . $token;
  260. $request_data = [
  261. 'user_action_set_id' => $user_action_set_id,
  262. 'actions' => [
  263. ['url' => '', 'action_time' => $action_time, 'action_type' => $action_type,
  264. 'user_id' => [
  265. 'wechat_app_id' => $appid,
  266. 'wechat_openid' => $openid
  267. ],
  268. 'action_param' => [
  269. 'product_name' => $product_name,
  270. 'product_id' => $product_id,
  271. 'value' => $value,
  272. 'object'=>$report_type
  273. ]
  274. ]
  275. ]
  276. ];
  277. try {
  278. $result = $client->request('post', $url, [
  279. 'headers' => ['Content-Type' => 'application/json'],
  280. 'body' => \GuzzleHttp\json_encode($request_data)
  281. ])->getBody()->getContents();
  282. Log::info('gdt user_actions/add body is ' );
  283. Log::info(\GuzzleHttp\json_encode($request_data));
  284. Log::info('gdt user_actions/add result is: ' . $result);
  285. $result_array = \GuzzleHttp\json_decode($result, 1);
  286. if (isset($result_array['errcode']) && $result_array['errcode'] == 0) {
  287. $data['result_status'] = 'SUCCESS';
  288. $data['result_msg'] = $result_array['errcode'];
  289. $data['result'] = $result;
  290. } else {
  291. $data['result_status'] = 'FAIL';
  292. $data['result_msg'] = isset($result_array['errcode']) ? $result_array['errcode'] : 'unknown';
  293. $data['result'] = $result;
  294. }
  295. return $data;
  296. } catch (Exception $e) {
  297. $data['result_status'] = 'FAIL';
  298. $data['result_msg'] = $e->getMessage();
  299. Log::error($e);
  300. return $data;
  301. }
  302. }
  303. private function getDataSurce($appid, $type = 'WECHAT')
  304. {
  305. if (!isset($this->user_action_set_id[$appid])) {
  306. $result = $this->getDataSourceFromDB($appid, $type);
  307. if ($result) {
  308. $this->user_action_set_id[$appid] = $result;
  309. return $result;
  310. }
  311. $this->error_msg = 'getDataSurce fail';
  312. return '';
  313. }
  314. return $this->user_action_set_id[$appid];
  315. }
  316. private function getDataSourceFromDB($appid, $type)
  317. {
  318. $result = DB::table('wechat_advertise_data_source')->where('appid', $appid)->where('type', $type)->select('user_action_set_id')->first();
  319. if ($result) {
  320. return $result->user_action_set_id;
  321. }
  322. return 0;
  323. }
  324. private function getAccessToken($appid)
  325. {
  326. if (isset($this->assess_token[$appid])) {
  327. return $this->assess_token[$appid];
  328. }
  329. $access_token = Redis::get('Wechat:access_token:appid:' . $appid);
  330. if ($access_token) {
  331. $this->assess_token[$appid] = $access_token;
  332. return $this->assess_token[$appid];
  333. }
  334. $this->error_msg = $appid . ',access_token fail';
  335. return '';
  336. }
  337. private function getBookNameFromOrer($order_id){
  338. $result = ['bid'=>'','book_name'=>'','en_bid'=>''];
  339. try{
  340. $order_info = DB::connection('api_mysql')->table('orders')->select('from_bid')->where('id',$order_id)->first();
  341. if($order_info && $order_info->from_bid){
  342. $result['bid'] = $order_info->from_bid;
  343. $result['en_bid'] = \Hashids::encode($order_info->from_bid);
  344. $book_info = DB::connection('api_mysql')->table('book_configs')
  345. ->select('book_name','is_on_shelf')
  346. ->where('bid',$order_info->from_bid)->first();
  347. if($book_info){
  348. if($book_info->is_on_shelf == 2){
  349. $result['book_name'] = $book_info->book_name;
  350. }
  351. }
  352. return $result;
  353. }
  354. }catch (\Exception $e){
  355. }
  356. return $result;
  357. }
  358. private function getBookNameByBid($bid){
  359. $result = ['bid'=>'','book_name'=>'','en_bid'=>''];
  360. if(!$bid) return $result;
  361. $result['en_bid'] = \Hashids::encode($bid);
  362. $result['bid'] = $bid;
  363. $book_info = DB::connection('api_mysql')->table('book_configs')->select('book_name','is_on_shelf')->where('bid',$bid)->first();
  364. if($book_info){
  365. if($book_info->is_on_shelf == 2){
  366. $result['book_name'] = $book_info->book_name;
  367. }
  368. }
  369. return $result;
  370. }
  371. private function transferReportType($report_type,$order_time,$order_id){
  372. $map = [
  373. 'one_day_first_pay'=>'read_1',
  374. 'one_day_all_pay'=>'read_2',
  375. 'current_day_register_first_pay'=>'read_3',
  376. 'current_day_register'=>'read_4',
  377. 'three_day_first_pay'=>'read_5',
  378. 'three_day_all_pay'=>'read_6'
  379. ];
  380. if(isset($map[$report_type])){
  381. return $map[$report_type];
  382. }
  383. $order_info = DB::connection('api_mysql')->table('orders')->where('id',$order_id)->select('uid')->first();
  384. if(!$order_info){
  385. return '';
  386. }
  387. $uid = $order_info->uid;
  388. $user_info = DB::connection('api_mysql')->table('users')->where('id',$uid)->select('created_at')->first();
  389. if(!$user_info) return '';
  390. $register_time = $user_info->created_at;
  391. $register_timestamp = strtotime($register_time);
  392. $order_timestamp = strtotime($order_time);
  393. if(date('Y-m-d',$register_timestamp) == date('Y-m-d',$order_timestamp)){
  394. return 'read_4';
  395. }
  396. if(strtotime($order_time) - strtotime($register_time) < 86400){
  397. return 'read_2';
  398. }
  399. if(strtotime($order_time) - strtotime($register_time) < 3*86400){
  400. return 'read_6';
  401. }
  402. return '';
  403. }
  404. }