TikTokUserCharge.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Jobs;
  3. use App\Consts\SysConsts;
  4. use App\Modules\Trade\Models\OceanengineReportRateConfig;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use App\Modules\Trade\Models\Order;
  12. use App\Modules\User\Models\User;
  13. use Redis;
  14. /**
  15. * 抖音用户推送数据
  16. */
  17. class TikTokUserCharge implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. private $uid;
  21. private $time;
  22. private $amount;
  23. /**
  24. * Create a new job instance.
  25. * @param int $uid
  26. * @param string $type 类型
  27. * @return void
  28. */
  29. public function __construct(int $uid, float $amount, string $time)
  30. {
  31. $this->uid = $uid;
  32. $this->amount = $amount;
  33. $this->time = $time;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. $client = new Client();
  43. if(empty($this->time)){
  44. \Log::info('origin_params:'.$this->time);
  45. $this->time = date('Y-m-d H:i:s');
  46. }
  47. $params = [
  48. 'uid' => $this->uid,
  49. 'amount' => $this->amount,
  50. 'time' => $this->time,
  51. 'source' => 'wdy'
  52. ];
  53. \Log::info('TikTokUserCharge:'.json_encode($params));
  54. // if( date('Y-m-d',strtotime( $this->time )) != date('Y-m-d')){
  55. // return ;
  56. // }
  57. $user = User::find($this->uid);
  58. $order = Order::where('uid', $this->uid)->where('status', 'PAID')->orderBy('id', 'desc')->first();
  59. $current_day_register = date('Y-m-d', strtotime($order->created_at)) == date('Y-m-d', strtotime($user->created_at));
  60. if ($order && $current_day_register && $order->price >= 30) {
  61. if(!$this->reportRate($user->distribution_channel_id)){
  62. return ;
  63. }
  64. $count = Order::where('uid', $this->uid)->where('status', 'PAID')->where('price', '>=', '30')->count();
  65. if ($count == 1) {
  66. } else {
  67. return;
  68. }
  69. } else {
  70. return;
  71. }
  72. $params['sign'] = _sign($params, SysConsts::TIKTOK_KEY);
  73. $url = 'https://newtrackapi.zhuishuyun.com/api/user/tiktokUserChargePush';
  74. $response = $client->request('post', $url, ['form_params' => $params])->getBody()->getContents();
  75. myLog('tiktok_push_charge')->info($response);
  76. $result = json_decode($response);
  77. if ($result) {
  78. if ($result->code != 0) {
  79. myLog('tiktok_push_charge')->info($response);
  80. }
  81. }
  82. }
  83. private function reportRate($channel_id){
  84. //OceanengineRechargeRecord::
  85. $rate = Redis::hget('channel:setting:'.$channel_id,'orange_site_report_rate');
  86. $rate = (int)$rate;
  87. if(!$rate || $rate == 100){
  88. return true;
  89. }
  90. $config = OceanengineReportRateConfig::where('channel_id',$channel_id)->first();
  91. if(!$config){
  92. OceanengineReportRateConfig::create(['channel_id'=>$channel_id,'report_rate'=>$rate,'report_num'=>1,'report_success_num'=>1]);
  93. return true;
  94. }else{
  95. if($config->report_rate == $rate){
  96. $now_report_num = $config->report_num+1;
  97. $now_rate = round($config->report_success_num/$now_report_num,4)*100;
  98. if($now_rate <= $rate){
  99. $config->report_num = $config->report_num+1;
  100. $config->report_success_num = $config->report_success_num+1;
  101. $config->save();
  102. return true;
  103. }
  104. $config->report_num = $config->report_num+1;
  105. $config->save();
  106. return false;
  107. }else{
  108. $config->report_rate = $rate;
  109. $config->report_num = 1;
  110. $config->report_success_num = 1;
  111. $config->save();
  112. return true;
  113. }
  114. }
  115. }
  116. }