GdtAdReport.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('order_id',$item['id'])->update([
  159. 'report_status'=>5,
  160. 'updated_at'=>date('Y-m-d H:i:s')
  161. ]);
  162. continue;
  163. };
  164. $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);
  165. $data['result_status'] = $result['result_status'];
  166. $data['result_msg'] = $result['result_msg'];
  167. $data['result'] = $result['result'];
  168. try{
  169. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  170. if($data['result_status'] == 'SUCCESS'){
  171. DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('order_id',$item['id'])->update([
  172. 'report_status'=>1,
  173. 'updated_at'=>date('Y-m-d H:i:s')
  174. ]);
  175. }
  176. }catch (\Exception $e){}
  177. //second
  178. $data['action_type'] = 'PURCHASE';
  179. $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);
  180. $data['result_status'] = $result['result_status'];
  181. $data['result_msg'] = $result['result_msg'];
  182. $data['result'] = $result['result'];
  183. DB::table('wechat_advertise_gdt_report_record')->insert($data);
  184. try{
  185. if($data['result_status'] == 'SUCCESS'){
  186. DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('order_id',$item['id'])->update([
  187. 'report_status'=>1,
  188. 'updated_at'=>date('Y-m-d H:i:s')
  189. ]);
  190. }
  191. }catch (\Exception $e){}
  192. }
  193. }
  194. private function getOrderData($action_type)
  195. {
  196. $data = [];
  197. $start = date('Y-m-d');
  198. if (date('H:i:s') < '02:00:00') {
  199. $start = date('Y-m-d H:i:s', time() - 3 * 3600);
  200. }
  201. $result = DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')
  202. ->where('report_status', 0)
  203. ->where('created_at', '>=', $start)
  204. ->where('type','WEB')
  205. ->where('action_type','ALL')
  206. ->select('order_id', 'appid', 'openid', 'price', 'report_status', 'created_at','report_type')
  207. ->get();
  208. if ($result) {
  209. foreach ($result as $item) {
  210. $report_info = DB::table('wechat_advertise_gdt_report_record')
  211. ->where('product_id', $item->order_id)
  212. ->where('action_type', $action_type)
  213. ->where('result_status', 'SUCCESS')
  214. ->count();
  215. if ($report_info) {
  216. try {
  217. DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('order_id', $item->order_id)->update([
  218. 'report_status' => 1,
  219. 'updated_at' => date('Y-m-d H:i:s')
  220. ]);
  221. } catch (\Exception $e) {
  222. \Log::info($e);
  223. }
  224. continue;
  225. }
  226. $data[] = ['created_at' => $item->created_at, 'openid' => $item->openid,'report_type'=>$item->report_type,
  227. 'id' => $item->order_id, 'appid' => $item->appid, 'price' => $item->price];
  228. }
  229. }
  230. return $data;
  231. }
  232. private function getRegisterData($action_type)
  233. {
  234. $data = [];
  235. $start = date('Y-m-d');
  236. if (date('H:i:s') < '02:00:00') {
  237. $start = date('Y-m-d H:i:s', time() - 3 * 3600);
  238. }
  239. $result = DB::connection('api_mysql')->table('distribution_channel_weixin_spread_users')
  240. ->where('report_status', 0)
  241. ->where('created_at', '>=', $start)
  242. ->select('uid', 'appid', 'openid', 'report_status', 'created_at', 'bid')
  243. ->get();
  244. if ($result) {
  245. foreach ($result as $item) {
  246. $report_info = DB::table('wechat_advertise_gdt_report_record')
  247. ->where('product_id', $item->uid)
  248. ->where('action_type', $action_type)
  249. ->where('result_status', 'SUCCESS')
  250. ->count();
  251. if ($report_info) {
  252. continue;
  253. }
  254. $data[] = ['created_at' => $item->created_at, 'openid' => $item->openid,
  255. 'id' => $item->uid, 'appid' => $item->appid, 'bid' => $item->bid];
  256. }
  257. }
  258. return $data;
  259. }
  260. private function request(Client $client, $user_action_set_id, $action_time, $action_type, $appid, $openid, $value, $product_name, $product_id,$token,$report_type)
  261. {
  262. $data['result_status'] = 'FAIL';
  263. $data['result_msg'] = '';
  264. $data['result'] = '';
  265. $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token=' . $token;
  266. $request_data = [
  267. 'user_action_set_id' => $user_action_set_id,
  268. 'actions' => [
  269. ['url' => '', 'action_time' => $action_time, 'action_type' => $action_type,
  270. 'user_id' => [
  271. 'wechat_app_id' => $appid,
  272. 'wechat_openid' => $openid
  273. ],
  274. 'action_param' => [
  275. 'product_name' => $product_name,
  276. 'product_id' => $product_id,
  277. 'value' => $value,
  278. 'object'=>$report_type
  279. ]
  280. ]
  281. ]
  282. ];
  283. try {
  284. $result = $client->request('post', $url, [
  285. 'headers' => ['Content-Type' => 'application/json'],
  286. 'body' => \GuzzleHttp\json_encode($request_data)
  287. ])->getBody()->getContents();
  288. Log::info('gdt user_actions/add body is ' );
  289. Log::info(\GuzzleHttp\json_encode($request_data));
  290. Log::info('gdt user_actions/add result is: ' . $result);
  291. $result_array = \GuzzleHttp\json_decode($result, 1);
  292. if (isset($result_array['errcode']) && $result_array['errcode'] == 0) {
  293. $data['result_status'] = 'SUCCESS';
  294. $data['result_msg'] = $result_array['errcode'];
  295. $data['result'] = $result;
  296. } else {
  297. $data['result_status'] = 'FAIL';
  298. $data['result_msg'] = isset($result_array['errcode']) ? $result_array['errcode'] : 'unknown';
  299. $data['result'] = $result;
  300. }
  301. return $data;
  302. } catch (Exception $e) {
  303. $data['result_status'] = 'FAIL';
  304. $data['result_msg'] = $e->getMessage();
  305. Log::error($e);
  306. return $data;
  307. }
  308. }
  309. private function getDataSurce($appid, $type = 'WECHAT')
  310. {
  311. if (!isset($this->user_action_set_id[$appid])) {
  312. $result = $this->getDataSourceFromDB($appid, $type);
  313. if ($result) {
  314. $this->user_action_set_id[$appid] = $result;
  315. return $result;
  316. }
  317. $this->error_msg = 'getDataSurce fail';
  318. return '';
  319. }
  320. return $this->user_action_set_id[$appid];
  321. }
  322. private function getDataSourceFromDB($appid, $type)
  323. {
  324. $result = DB::table('wechat_advertise_data_source')->where('appid', $appid)->where('type', $type)->select('user_action_set_id')->first();
  325. if ($result) {
  326. return $result->user_action_set_id;
  327. }
  328. return 0;
  329. }
  330. private function getAccessToken($appid)
  331. {
  332. if (isset($this->assess_token[$appid])) {
  333. return $this->assess_token[$appid];
  334. }
  335. $access_token = Redis::get('Wechat:access_token:appid:' . $appid);
  336. if ($access_token) {
  337. $this->assess_token[$appid] = $access_token;
  338. return $this->assess_token[$appid];
  339. }
  340. $this->error_msg = $appid . ',access_token fail';
  341. return '';
  342. }
  343. private function getBookNameFromOrer($order_id){
  344. $result = ['bid'=>'','book_name'=>'','en_bid'=>''];
  345. try{
  346. $order_info = DB::connection('api_mysql')->table('orders')->select('from_bid')->where('id',$order_id)->first();
  347. if($order_info && $order_info->from_bid){
  348. $result['bid'] = $order_info->from_bid;
  349. $result['en_bid'] = \Hashids::encode($order_info->from_bid);
  350. $book_info = DB::connection('api_mysql')->table('book_configs')
  351. ->select('book_name','is_on_shelf')
  352. ->where('bid',$order_info->from_bid)->first();
  353. if($book_info){
  354. if($book_info->is_on_shelf == 2){
  355. $result['book_name'] = $book_info->book_name;
  356. }
  357. }
  358. return $result;
  359. }
  360. }catch (\Exception $e){
  361. }
  362. return $result;
  363. }
  364. private function getBookNameByBid($bid){
  365. $result = ['bid'=>'','book_name'=>'','en_bid'=>''];
  366. if(!$bid) return $result;
  367. $result['en_bid'] = \Hashids::encode($bid);
  368. $result['bid'] = $bid;
  369. $book_info = DB::connection('api_mysql')->table('book_configs')->select('book_name','is_on_shelf')->where('bid',$bid)->first();
  370. if($book_info){
  371. if($book_info->is_on_shelf == 2){
  372. $result['book_name'] = $book_info->book_name;
  373. }
  374. }
  375. return $result;
  376. }
  377. private function transferReportType($report_type,$order_time,$order_id){
  378. $map = [
  379. 'one_day_first_pay'=>'read_1',
  380. 'one_day_all_pay'=>'read_2',
  381. 'current_day_register_first_pay'=>'read_3',
  382. 'current_day_register'=>'read_4',
  383. 'three_day_first_pay'=>'read_5',
  384. 'three_day_all_pay'=>'read_6'
  385. ];
  386. if(isset($map[$report_type])){
  387. return $map[$report_type];
  388. }
  389. $order_info = DB::connection('api_mysql')->table('orders')->where('id',$order_id)->select('uid')->first();
  390. if(!$order_info){
  391. return '';
  392. }
  393. $uid = $order_info->uid;
  394. $user_info = DB::connection('api_mysql')->table('users')->where('id',$uid)->select('created_at')->first();
  395. if(!$user_info) return '';
  396. $register_time = $user_info->created_at;
  397. $register_timestamp = strtotime($register_time);
  398. $order_timestamp = strtotime($order_time);
  399. if(date('Y-m-d',$register_timestamp) == date('Y-m-d',$order_timestamp)){
  400. return 'read_4';
  401. }
  402. if(strtotime($order_time) - strtotime($register_time) < 86400){
  403. return 'read_2';
  404. }
  405. if(strtotime($order_time) - strtotime($register_time) < 3*86400){
  406. return 'read_6';
  407. }
  408. return '';
  409. }
  410. }