123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- namespace App\Console\Commands\Wechat;
- use GuzzleHttp\Client;
- use Illuminate\Console\Command;
- use App\Http\Controllers\WechatController;
- use Exception;
- use Log;
- use DB;
- use Redis;
- class AdReportTest extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'Wechat:WeixinAdReportTest';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- private $user_action_set_id = [];
- private $assess_token = [];
- private $error_msg;
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- //
- /*$data = [
- 'created_at'=>date('Y-m-d H:i:s'),
- 'openid'=>'onkhc1BlHQzKuV_5o0n9wxo9Cu8A',
- 'id'=>'20488494',
- 'appid'=>'wx81f3952367416341',
- 'price'=>0.01,
- ];*/
- //Redis::RPUSH('Wechat:WeixinAdReport',\GuzzleHttp\json_encode($data));
- //echo \GuzzleHttp\json_encode($data);
- $this->start();
- }
- private function start(){
- /*$data = $this->getDataFromDB();
- if($data){
- $this->getDataAndReport($data);
- }*/
- $data = [
- [
- 'created_at'=>date('Y-m-d H:i:s'),
- 'openid'=>'oTO7qvlsPvv6B2dtQM8h3Cb6CXjI',
- 'id'=>'26787982',
- 'appid'=>'wx422e5d5e11db16d9',
- 'price'=>0.01,
- ]
- ];
- $this->getDataAndReport($data);
- }
- private function getDataFromDB(){
- $data = [];
- $start = date('Y-m-d');
- if(date('H:i:s') < '02:00:00'){
- $start = date('Y-m-d H:i:s',time()-3*3600);
- }
- $result = DB::connection('api_mysql')->table('distribution_channel_weixin_ad_report_orders')
- ->where('report_status',0)
- ->where('created_at','>=',$start)
- ->select('order_id','appid','openid','price','report_status','created_at')
- ->get();
- if($result){
- foreach ($result as $item){
- $order_status = DB::connection('api_mysql')->table('orders')->where('id',$item->order_id)->select('status')->first();
- if($order_status->status == 'UNPAID')continue;
- $report_info = DB::table('wechat_advertise_report_record')->where('product_id',$item->order_id)->where('result_status','SUCCESS')->count();
- if($report_info) {
- try{
- DB::connection('api_mysql')->table('distribution_channel_weixin_ad_report_orders')->where('order_id',$item->order_id)->update([
- 'report_status'=>1,
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- }catch (\Exception $e){
- \Log::info($e);
- }
- continue;
- }
- $data[] = ['created_at' => $item->created_at, 'openid' => $item->openid,
- 'id' => $item->order_id, 'appid' => $item->appid, 'price' => $item->price];
- }
- }
- return $data;
- }
- private function getDataAndReport($order_result){
- $result = $order_result;
- /*$result = Redis::LRANGE('Wechat:WeixinAdReport',0,-1);
- Redis::del('Wechat:WeixinAdReport');
- Log::info($result);
- if(!$result){
- Log::info('Wechat:WeixinAdReport,empty list');
- }*/
- $client = new Client();
- foreach ($result as $item){
- //$item_array = \GuzzleHttp\json_decode($item,1);
- $item_array = $item;
- $user_action_set_id = $this->getDataSurce($item_array['appid']);
- $data = [
- 'user_action_set_id'=>$user_action_set_id,
- 'url'=>'',
- 'action_time'=>strtotime($item_array['created_at']),
- 'action_type'=>'COMPLETE_ORDER',
- 'openid'=>$item_array['openid'],
- 'appid'=>$item_array['appid'],
- 'product_id'=>$item_array['id'],
- 'value'=>$item_array['price']*100,
- 'source'=>'Biz',
- 'claim_type'=>0
- ];
- $data['created_at'] = date('Y-m-d H:i:s');
- $data['updated_at'] = date('Y-m-d H:i:s');
- $data['result_status'] = 'FAIL';
- $book_info = $this->getBookName($item_array['id']);
- $product_id = '';
- $product_name = '';
- $data['bid'] = $book_info['en_bid'];
- if($book_info['en_bid'] && $book_info['book_name']){
- $product_id = $book_info['en_bid'];
- $data['bid'] = $book_info['en_bid'];
- $product_name = $book_info['book_name'].'-'.env('PROJECT_NAME');
- $data['product_name'] = $product_name;
- }
- $data['result'] = '';
- if(!$user_action_set_id) {
- $data['result_msg'] = $this->error_msg;
- DB::table('wechat_advertise_report_record')->insert($data);
- continue;
- };
- $request_data = [
- 'actions'=>[
- [
- 'user_action_set_id'=>$data['user_action_set_id'],
- 'url'=>$data['url'],
- 'action_time'=>$data['action_time'],
- 'action_type'=>$data['action_type'],
- 'user_id'=>[
- 'wechat_app_id'=>$data['appid'],
- 'wechat_openid'=>$data['openid']
- ],
- 'action_param'=>[
- 'object'=>'',
- 'product_name'=>$product_name,
- 'product_id'=>$product_id,
- 'source'=>$data['source'],
- 'wechat_app_id'=>'',
- 'claim_type'=>$data['claim_type'],
- 'value'=>$item_array['price']*100
- ]
- ]
- ]
- ];
- $token = $this->getAccessToken($item_array['appid']);
- if(!$token) continue;
- $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token='.$token;
- try{
- $result = $client->request('post',$url,[
- 'headers'=>['Content-Type'=>'application/json'],
- 'body'=>\GuzzleHttp\json_encode($request_data)
- ])->getBody()->getContents();
- Log::info('user_actions/add result is: '.$result);
- $result_array = \GuzzleHttp\json_decode($result,1);
- if(isset($result_array['errcode']) && $result_array['errcode'] == 0){
- $data['result_status'] = 'SUCCESS';
- $data['result_msg'] = $result_array['errcode'];
- $data['result'] = $result;
- }else{
- $data['result_status'] = 'FAIL';
- $data['result_msg'] = isset($result_array['errcode'])?$result_array['errcode']:'unknown';
- $data['result'] = $result;
- }
- DB::table('wechat_advertise_report_record')->insert($data);
- try{
- if($data['result_status'] == 'SUCCESS'){
- DB::connection('api_mysql')->table('distribution_channel_weixin_ad_report_orders')->where('order_id',$item_array['id'])->update([
- 'report_status'=>1,
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- }
- }catch (\Exception $e){
- \Log::info($e);
- }
- }catch (Exception $e){
- Log::error($e);
- }
- try{
- $this->secondReport($client,$item,$user_action_set_id,$book_info['en_bid'],$book_info['book_name'],$token);
- }catch (\Exception $e){
- \Log::info($e);
- }
- }
- }
- private function secondReport(Client $client,$data,$user_action_set_id,$bid='',$book_name='',$token=''){
- $item_array = $data;
- $data = [
- 'user_action_set_id'=>$user_action_set_id,
- 'url'=>'',
- 'action_time'=>strtotime($item_array['created_at']),
- 'action_type'=>'PURCHASE',
- 'openid'=>$item_array['openid'],
- 'appid'=>$item_array['appid'],
- 'product_id'=>$item_array['id'],
- 'value'=>$item_array['price']*100,
- 'source'=>'Biz',
- 'claim_type'=>0
- ];
- $data['created_at'] = date('Y-m-d H:i:s');
- $data['updated_at'] = date('Y-m-d H:i:s');
- $data['result_status'] = 'FAIL';
- $product_id = '';
- $product_name = '';
- $data['bid'] = '';
- if($bid && $book_name){
- $product_id = $bid;
- $data['bid'] = $bid;
- $product_name = $book_name.'-'.env('PROJECT_NAME');
- $data['product_name'] = $product_name;
- }
- $data['result'] = '';
- if(!$user_action_set_id) {
- $data['result_msg'] = $this->error_msg;
- DB::table('wechat_advertise_report_record')->insert($data);
- };
- $request_data = [
- 'actions'=>[
- [
- 'user_action_set_id'=>$data['user_action_set_id'],
- 'url'=>$data['url'],
- 'action_time'=>$data['action_time'],
- 'action_type'=>$data['action_type'],
- 'user_id'=>[
- 'wechat_app_id'=>$data['appid'],
- 'wechat_openid'=>$data['openid']
- ],
- 'action_param'=>[
- 'object'=>'',
- 'product_name'=>$product_name,
- 'product_id'=>$product_id,
- 'source'=>$data['source'],
- 'wechat_app_id'=>'',
- 'claim_type'=>$data['claim_type'],
- 'value'=>$item_array['price']*100
- ]
- ]
- ]
- ];
- if(!$token) return ;
- $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token='.$token;
- try{
- $result = $client->request('post',$url,[
- 'headers'=>['Content-Type'=>'application/json'],
- 'body'=>\GuzzleHttp\json_encode($request_data)
- ])->getBody()->getContents();
- Log::info('user_actions/add result is: '.$result);
- $result_array = \GuzzleHttp\json_decode($result,1);
- if(isset($result_array['errcode']) && $result_array['errcode'] == 0){
- $data['result_status'] = 'SUCCESS';
- $data['result_msg'] = $result_array['errcode'];
- $data['result'] = $result;
- }else{
- $data['result_status'] = 'FAIL';
- $data['result_msg'] = isset($result_array['errcode'])?$result_array['errcode']:'unknown';
- $data['result'] = $result;
- }
- DB::table('wechat_advertise_report_record')->insert($data);
- }catch (Exception $e){
- Log::error($e);
- }
- }
- private function getDataSurce($appid,$type='WECHAT'){
- if(!isset($this->user_action_set_id[$appid])){
- $result = $this->getDataSourceFromDB($appid,$type);
- if($result){
- $this->user_action_set_id[$appid] = $result;
- return $result;
- }
- $result = $this->createDataSource($appid,$type);
- if($result){
- $this->user_action_set_id[$appid] = $result;
- return $result;
- }
- Log::info('appid is :'.$appid.', createDataSource fail');
- return '';
- }
- return $this->user_action_set_id[$appid];
- }
- private function getAccessToken($appid){
- if(isset($this->assess_token[$appid])){
- return $this->assess_token[$appid];
- }
- /*$WechatController = new WechatController($appid);
- $accessToken = $WechatController->app->access_token; // EasyWeChat\Core\AccessToken 实例
- $token = $accessToken->getToken(); // token 字符串
- if($token){
- $this->assess_token[$appid] = $token;
- return $token;
- }
- Log::info('appid is :'.$appid.',request access_token fail');
- return '';*/
- $access_token = Redis::get('Wechat:access_token:appid:'.$appid);
- if($access_token){
- $this->assess_token[$appid] = $access_token;
- return $this->assess_token[$appid];
- }
- /*$appsecret = 'f73d3be068d86417bf1a3fafefe4596a';
- $url = sprintf(
- 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s',
- $appid,
- $appsecret
- );
- $client = new Client();
- $result = $client->request('get',$url)->getBody()->getContents();
- Log::info('request access token ,result is:'.$result);
- $result = \GuzzleHttp\json_decode($result,1);
- if($result && isset($result['access_token'])){
- Redis::setex('Wechat:access_token:appid:'.$appid,7000,$result['access_token']);
- $this->assess_token[$appid] = $result['access_token'];
- return $this->assess_token[$appid];
- }*/
- $this->error_msg = $appid.',access_token fail';
- return '';
- }
- private function createDataSource(string $appid,string $type){
- $token = $this->getAccessToken($appid);
- if(!$token) return 0;
- $url = 'https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0&access_token='.$token;
- $client = new Client();
- $name = '支付下单';
- $description = '支付下单,充值统计';
- $body = json_encode([
- 'type'=>$type,
- 'wechat_app_id'=>$appid,
- 'name'=>$name,
- 'description'=>$description
- ]);
- try{
- $result = $client->request('post',$url,[
- 'headers'=>['Content-Type'=>'application/json'],
- 'body'=>$body
- ])->getBody()->getContents();
- $result = \GuzzleHttp\json_decode($result,1);
- if($result['errcode'] == 0 && isset($result['data']) && isset($result['data']['user_action_set_id'])){
- $user_action_set_id = $result['data']['user_action_set_id'];
- $this->saveDataSource($appid,$name,$type,$description,$user_action_set_id);
- return $user_action_set_id;
- }
- if($result['errcode'] == 900351000){
- $msg_array = explode(':',$result['errmsg']);
- if(isset($msg_array[1])){
- $user_action_set_id = trim($msg_array[1]);
- $this->saveDataSource($appid,$name,$type,$description,$user_action_set_id);
- return $user_action_set_id;
- }
- }
- }catch (Exception $e){
- Log::error($e);
- }
- $this->error_msg = $appid.',createDataSource fail';
- return 0;
- }
- private function getDataSourceFromDB($appid,$type):int
- {
- $result = DB::table('wechat_advertise_data_source')->where('appid', $appid)->where('type', $type)->select('user_action_set_id')->first();
- if($result){
- return $result->user_action_set_id;
- }
- return 0;
- }
- private function saveDataSource(string $appid,string $name,string $type,string $description,int $user_action_set_id){
- DB::table('wechat_advertise_data_source')->insert([
- 'appid'=>$appid,
- 'name'=>$name,
- 'type'=>$type,
- 'description'=>$description,
- 'user_action_set_id'=>$user_action_set_id,
- 'created_at'=>date('Y-m-d H:i:s'),
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- }
- private function getBookName($order_id){
- $result = ['bid'=>'','book_name'=>'','en_bid'=>''];
- try{
- $order_info = DB::connection('api_mysql')->table('orders')->select('from_bid')->where('id',$order_id)->first();
- if($order_info && $order_info->from_bid){
- $result['bid'] = $order_info->from_bid;
- $result['en_bid'] = \Hashids::encode($order_info->from_bid);
- $book_info = DB::connection('api_mysql')->table('book_configs')->select('book_name')->where('bid',$order_info->from_bid)->first();
- if($book_info){
- $result['book_name'] = $book_info->book_name;
- }
- return $result;
- }
- }catch (\Exception $e){
- }
- return $result;
- }
- }
|