RepeatReport.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 RepeatReport extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'Wechat:RepeatReport';
  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. $client = new Client();
  47. $repair_list = Redis::LRANGE('Wechat:AdReport',0,-1);
  48. if($repair_list){
  49. $this->repair = true;
  50. foreach ($repair_list as $repair_item){
  51. $item = json_decode($repair_item);
  52. $time = date('Y-m-d H:i:s');
  53. if($item->action_type == 'ALL'){
  54. $this->reportOne($client,$item->id,$item->order_id,$item->appid,$item->openid,$item->price,$time,$item->report_type,'COMPLETE_ORDER',$item->type);
  55. $this->reportOne($client,$item->id,$item->order_id,$item->appid,$item->openid,$item->price,$time,$item->report_type,'PURCHASE',$item->type);
  56. }else{
  57. $this->reportOne($client,$item->id,$item->order_id,$item->appid,$item->openid,$item->price,$time,$item->report_type,$item->action_type,$item->type);
  58. }
  59. }
  60. Redis::del('Wechat:AdReport');
  61. }
  62. }
  63. private function reportOne(Client $client,$record_id,$order_id,$appid,$openid,$price,$created_at,$p_report_type,$action_type,$type){
  64. $report_type = $this->transferReportType($p_report_type,$created_at,$order_id);
  65. $user_action_set_id = $this->getDataSource($appid,$type);
  66. $book_info = $this->getBookName($order_id);
  67. $bid = $book_info['en_bid'];
  68. $product_name = $book_info['book_name'].'-'.env('PROJECT_NAME');
  69. $token = $this->getAccessToken($appid);
  70. if(!$user_action_set_id || !$token){
  71. $this->saveRecord($user_action_set_id,$created_at,$action_type,
  72. $appid,$openid,$price,$report_type,$bid,
  73. $order_id,$product_name,$type,'FAIL','',$this->error_msg);
  74. return ;
  75. }
  76. $action_time = strtotime($created_at);
  77. $value = $price*100;
  78. $result = [
  79. 'result_status'=>'FAIL',
  80. 'result_msg'=>'type error',
  81. 'result'=>''
  82. ];
  83. if($type == 'WECHAT'){
  84. $result = $this->requestMp($client, $user_action_set_id, $action_time, $action_type, $appid, $openid, $value, $product_name, $bid,$token,$report_type);
  85. }
  86. if($type == 'WEB'){
  87. $result = $this->requestGdt($client, $user_action_set_id, $action_time, $action_type, $appid, $openid, $value, $product_name, $bid,$token,$report_type);
  88. }
  89. $this->saveRecord($user_action_set_id,$created_at,$action_type,
  90. $appid,$openid,$price,$report_type,$bid,
  91. $order_id,$product_name,$type,$result['result_status'],$result['result'],$result['result_msg']);
  92. if($result['result_status'] == 'SUCCESS'){
  93. $status = 7;
  94. DB::connection('api_mysql')->table('distribution_channel_weixin_report_orders')->where('id',$record_id)
  95. ->update(['report_status'=>$status,'updated_at'=>date('Y-m-d H:i:s')]);
  96. }
  97. }
  98. private function requestGdt(Client $client, $user_action_set_id, $action_time, $action_type, $appid, $openid, $value, $product_name, $product_id,$token,$report_type)
  99. {
  100. $data['result_status'] = 'FAIL';
  101. $data['result_msg'] = '';
  102. $data['result'] = '';
  103. $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token=' . $token;
  104. $object = $report_type;
  105. if($action_type == 'PURCHASE'){
  106. $object = 1;
  107. }
  108. $request_data = ['user_action_set_id' => $user_action_set_id,
  109. 'actions' => [['url' => env('GDT_REPORT_URL').'/detail?id='.$product_id, 'action_time' => $action_time, 'action_type' => $action_type,
  110. 'user_id' => ['wechat_app_id' => $appid, 'wechat_openid' => $openid],
  111. 'action_param' => ['product_name' => $product_name, 'product_id' => $product_id, 'value' => $value, 'object'=>$object]]]];
  112. try {
  113. $result = $client->request('post', $url, [
  114. 'headers' => ['Content-Type' => 'application/json'],
  115. 'body' => \GuzzleHttp\json_encode($request_data)
  116. ])->getBody()->getContents();
  117. $result_array = \GuzzleHttp\json_decode($result, 1);
  118. if (isset($result_array['errcode']) && $result_array['errcode'] == 0) {
  119. $data['result_status'] = 'SUCCESS';
  120. $data['result_msg'] = $result_array['errcode'];
  121. $data['result'] = $result;
  122. } else {
  123. $data['result_status'] = 'FAIL';
  124. $data['result_msg'] = isset($result_array['errcode']) ? $result_array['errcode'] : 'unknown';
  125. $data['result'] = $result;
  126. }
  127. } catch (Exception $e) {
  128. $data['result_status'] = 'FAIL';
  129. $data['result_msg'] = $e->getMessage();
  130. }
  131. return $data;
  132. }
  133. private function requestMp(Client $client, $user_action_set_id, $action_time, $action_type, $appid, $openid, $value, $product_name, $product_id,$token,$report_type){
  134. $data['result_status'] = 'FAIL';
  135. $data['result_msg'] = '';
  136. $data['result'] = '';
  137. $object = $report_type;
  138. if($action_type == 'PURCHASE'){
  139. $object = 1;
  140. }
  141. $request_data = [
  142. 'actions'=>[
  143. [
  144. 'user_action_set_id'=>$user_action_set_id,
  145. //'url'=>'https://sitexyvz5mexll52mzn4.zhuishuyun.com/detail?id='.$product_id,
  146. 'url'=>env('GDT_REPORT_URL').'/detail?id='.$product_id,
  147. 'action_time'=>$action_time,
  148. 'action_type'=>$action_type,
  149. 'user_id'=>[
  150. 'wechat_app_id'=>$appid,
  151. 'wechat_openid'=>$openid
  152. ],
  153. 'action_param'=>[
  154. 'object'=>$object,
  155. 'product_name'=>$product_name,
  156. 'product_id'=>$product_id,
  157. 'source'=>'Biz',
  158. 'wechat_app_id'=>'',
  159. 'claim_type'=>0,
  160. 'value'=>$value
  161. ]
  162. ]
  163. ]
  164. ];
  165. $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token='.$token;
  166. try{
  167. $result = $client->request('post',$url,[
  168. 'headers'=>['Content-Type'=>'application/json'],
  169. 'body'=>\GuzzleHttp\json_encode($request_data)
  170. ])->getBody()->getContents();
  171. $result_array = \GuzzleHttp\json_decode($result,1);
  172. if(isset($result_array['errcode']) && $result_array['errcode'] == 0){
  173. $data['result_status'] = 'SUCCESS';
  174. $data['result_msg'] = $result_array['errcode'];
  175. $data['result'] = $result;
  176. }else{
  177. $data['result_status'] = 'FAIL';
  178. $data['result_msg'] = isset($result_array['errcode'])?$result_array['errcode']:'unknown';
  179. $data['result'] = $result;
  180. }
  181. }catch (Exception $e){
  182. Log::error($e);
  183. }
  184. return $data;
  185. }
  186. private function saveRecord($user_action_set_id,$created_at,$action_type,
  187. $appid,$openid,$price,$report_type,$bid,
  188. $product_id,$product_name,$type,$result_status,$result,$result_msg)
  189. {
  190. $action_time = strtotime($created_at);
  191. $data = [
  192. 'user_action_set_id'=>$user_action_set_id,
  193. //'url'=>'https://sitexyvz5mexll52mzn4.zhuishuyun.com/detail?id='.$bid, 'action_time'=>$action_time,
  194. 'url'=>env('GDT_REPORT_URL').'/detail?id='.$bid,
  195. 'action_type'=>$action_type, 'openid'=>$openid,
  196. 'appid'=>$appid, 'product_id'=>$product_id,
  197. 'value'=>$price*100,'result_status'=>$result_status,
  198. 'created_at'=>date('Y-m-d H:i:s'), 'updated_at'=>date('Y-m-d H:i:s'),
  199. 'bid'=>$bid,'product_name'=>$product_name,'result'=>$result,
  200. 'result_msg'=>$result_msg,'object'=>$report_type
  201. ];
  202. $table = 'wechat_advertise_gdt_report_record';
  203. if($type == 'WECHAT'){
  204. $data['source'] = 'Biz';
  205. $data['claim_type'] = 0;
  206. $table = 'wechat_advertise_report_record';
  207. }
  208. DB::table($table)->insert($data);
  209. }
  210. private function getDataSource($appid,$type='WECHAT'){
  211. if(!isset($this->user_action_set_id[$appid])){
  212. $result = $this->getDataSourceFromDB($appid,$type);
  213. if($result){
  214. $this->user_action_set_id[$appid] = $result;
  215. return $result;
  216. }
  217. $result = $this->createDataSource($appid,$type);
  218. if($result){
  219. $this->user_action_set_id[$appid] = $result;
  220. return $result;
  221. }
  222. return '';
  223. }
  224. return $this->user_action_set_id[$appid];
  225. }
  226. private function getDataSourceFromDB($appid, $type)
  227. {
  228. $result = DB::table('wechat_advertise_data_source')->where('appid', $appid)->where('type', $type)->select('user_action_set_id')->first();
  229. if ($result) {
  230. return $result->user_action_set_id;
  231. }
  232. return 0;
  233. }
  234. private function createDataSource(string $appid,string $type){
  235. $token = $this->getAccessToken($appid);
  236. if(!$token) return 0;
  237. $url = 'https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0&access_token='.$token;
  238. $client = new Client();
  239. $name = '支付下单';
  240. $description = '支付下单,充值统计';
  241. $body = json_encode([
  242. 'type'=>$type,
  243. 'wechat_app_id'=>$appid,
  244. 'name'=>$name,
  245. 'description'=>$description
  246. ]);
  247. try{
  248. $result = $client->request('post',$url,[
  249. 'headers'=>['Content-Type'=>'application/json'],
  250. 'body'=>$body
  251. ])->getBody()->getContents();
  252. $result = \GuzzleHttp\json_decode($result,1);
  253. if($result['errcode'] == 0 && isset($result['data']) && isset($result['data']['user_action_set_id'])){
  254. $user_action_set_id = $result['data']['user_action_set_id'];
  255. $this->saveDataSource($appid,$name,$type,$description,$user_action_set_id);
  256. return $user_action_set_id;
  257. }
  258. if($result['errcode'] == 900351000){
  259. $msg_array = explode(':',$result['errmsg']);
  260. if(isset($msg_array[1])){
  261. $user_action_set_id = trim($msg_array[1]);
  262. $this->saveDataSource($appid,$name,$type,$description,$user_action_set_id);
  263. return $user_action_set_id;
  264. }
  265. }
  266. }catch (Exception $e){
  267. Log::error($e);
  268. }
  269. $this->error_msg = $appid.',createDataSource fail';
  270. return 0;
  271. }
  272. private function transferReportType($report_type,$order_time,$order_id){
  273. $map = [
  274. 'one_day_first_pay'=>'read_1',
  275. 'one_day_all_pay'=>'read_2',
  276. 'current_day_register_first_pay'=>'read_3',
  277. 'current_day_register'=>'read_4',
  278. 'three_day_first_pay'=>'read_5',
  279. 'three_day_all_pay'=>'read_6'
  280. ];
  281. if(isset($map[$report_type])){
  282. return $map[$report_type];
  283. }
  284. $order_info = DB::connection('api_mysql')->table('orders')->where('id',$order_id)->select('uid')->first();
  285. if(!$order_info){
  286. return '';
  287. }
  288. $uid = $order_info->uid;
  289. $user_info = DB::connection('api_mysql')->table('users')->where('id',$uid)->select('created_at')->first();
  290. if(!$user_info) return '';
  291. $register_time = $user_info->created_at;
  292. $register_timestamp = strtotime($register_time);
  293. $order_timestamp = strtotime($order_time);
  294. if(date('Y-m-d',$register_timestamp) == date('Y-m-d',$order_timestamp)){
  295. return 'read_4';
  296. }
  297. if(strtotime($order_time) - strtotime($register_time) < 86400){
  298. return 'read_2';
  299. }
  300. if(strtotime($order_time) - strtotime($register_time) < 3*86400){
  301. return 'read_6';
  302. }
  303. return '';
  304. }
  305. private function saveDataSource(string $appid,string $name,string $type,string $description,int $user_action_set_id){
  306. DB::table('wechat_advertise_data_source')->insert([
  307. 'appid'=>$appid,
  308. 'name'=>$name,
  309. 'type'=>$type,
  310. 'description'=>$description,
  311. 'user_action_set_id'=>$user_action_set_id,
  312. 'created_at'=>date('Y-m-d H:i:s'),
  313. 'updated_at'=>date('Y-m-d H:i:s')
  314. ]);
  315. }
  316. private function getBookName($order_id){
  317. $result = ['bid'=>'','book_name'=>'','en_bid'=>''];
  318. try{
  319. $order_info = DB::connection('api_mysql')->table('orders')->select('from_bid')->where('id',$order_id)->first();
  320. if($order_info && $order_info->from_bid){
  321. $result['bid'] = $order_info->from_bid;
  322. $result['en_bid'] = \Hashids::encode($order_info->from_bid);
  323. $book_info = DB::connection('api_mysql')->table('book_configs')
  324. ->select('book_name','is_on_shelf')
  325. ->where('bid',$order_info->from_bid)->first();
  326. if($book_info){
  327. if($book_info->is_on_shelf == 2){
  328. $result['book_name'] = $book_info->book_name;
  329. }
  330. }
  331. return $result;
  332. }
  333. }catch (\Exception $e){
  334. }
  335. return $result;
  336. }
  337. private function getAccessToken($appid){
  338. if(isset($this->assess_token[$appid])){
  339. return $this->assess_token[$appid];
  340. }
  341. $access_token = Redis::get('Wechat:access_token:appid:'.$appid);
  342. if($access_token){
  343. $this->assess_token[$appid] = $access_token;
  344. return $this->assess_token[$appid];
  345. }
  346. $this->error_msg = $appid.',access_token fail';
  347. return '';
  348. }
  349. }