AdReportTest.php 17 KB

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