AdReport.php 18 KB

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