Browse Source

抖音次留回传

onlinetest 4 years ago
parent
commit
c0d4c12ac9

+ 8 - 2
app/Http/Controllers/QuickApp/Book/ChapterController.php

@@ -10,6 +10,7 @@ use App\Modules\User\Services\ReadRecordService;
 use App\Http\Controllers\QuickApp\Book\Transformers\ChapterTransformer;
 use App\Modules\Book\Services\BookConfigService;
 use App\Http\Controllers\QuickApp\Book\Transformers\ChapterListTransformer;
+use App\Jobs\UserRententionJob;
 use App\Modules\Book\Services\BookService;
 use App\Modules\Subscribe\Services\BookOrderService;
 use App\Modules\Subscribe\Services\ChapterOrderService;
@@ -260,6 +261,10 @@ class ChapterController extends BaseController
         if (!$chapter) {
             return response()->error('QAPP_SYS_ERROR');
         }
+
+        $job = new UserRententionJob($this->uid, now());
+        dispatch($job)->onConnection('rabbitmq')->onQueue('user_rentention_queue');
+
         if ($chapter->is_vip == 0) {
             ReadRecordService::addReadRecord([
                 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
@@ -437,7 +442,7 @@ class ChapterController extends BaseController
     protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
     {
         $fee = $this->getPrice($book_info, $chapter_size);
-        if ((int)$this->user_info['balance'] >= $fee) {
+        if ((int) $this->user_info['balance'] >= $fee) {
             if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
                 return true;
             }
@@ -603,7 +608,8 @@ class ChapterController extends BaseController
                     'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
                     'send_order_id' => $this->send_order_id,
                 ]);
-            } catch (\Exception  $e) { }
+            } catch (\Exception  $e) {
+            }
         }
     }
 

+ 74 - 0
app/Jobs/UserRententionJob.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Consts\SysConsts;
+use App\Modules\User\Models\QappUserRententionLog;
+use App\Modules\User\Models\User;
+use GuzzleHttp\Client;
+use Illuminate\Bus\Queueable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+
+/**
+ * 用户留存
+ */
+class UserRententionJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $uid;
+
+    protected $time;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct(int $uid, string $time)
+    {
+        $this->uid = $uid;
+        $this->time = $time;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $user = User::find($this->uid);
+        $date = date('Y-m-d', strtotime($this->time));
+        $is_next_day = date('Y-m-d', strtotime($user->created_at)) == date('Y-m-d', strtotime('-1 days', strtotime($this->time)));
+        if ($is_next_day) {
+            $exists = QappUserRententionLog::where('uid', $this->uid)->exists();
+            if ($exists) {
+                return;
+            }
+            $client = new Client();
+            $params = [
+                'uid' => $this->uid,
+                'source' => 'zsy'
+            ];
+            $params['sign'] = _sign($params, SysConsts::TIKTOK_KEY);
+            $url = 'https://newtrackapi.zhuishuyun.com/api/qappuser/rentention';
+            $response =  $client->post($url, ['form_params' => $params])->getBody()->getContents();
+            myLog('new_user_rentention')->info($response);
+            $result =  json_decode($response);
+            if ($result) {
+                if ($result->code == 0) {
+                    QappUserRententionLog::create([
+                        'date' => $date,
+                        'uid' => $this->uid,
+                        'register_time' => $user->created_at,
+                        'read_time' => $this->time,
+                    ]);
+                }
+            }
+        }
+    }
+}

+ 3 - 2
app/Libs/Helpers.php

@@ -30,7 +30,7 @@ function _sign($params, $key)
     $signPars = "";
     ksort($params);
     foreach ($params as $k => $v) {
-        if ("" != $v && "sign" != $k && is_string($v)) {
+        if ("" != $v && "sign" != $k) {
             $signPars .= $k . "=" . $v . "&";
         }
     }
@@ -152,7 +152,8 @@ function get_channel_domain($distribution_channel_id)
 function checkParam(array $param, array $must)
 {
     foreach ($must as $item) {
-        if (array_key_exists($item, $param) && $param[$item] != '') { } else {
+        if (array_key_exists($item, $param) && $param[$item] != '') {
+        } else {
             return $item;
         }
     }

+ 16 - 0
app/Modules/User/Models/QappUserRententionLog.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Modules\User\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class QappUserRententionLog extends Model
+{
+    protected $table = 'qapp_user_rentention_logs';
+    protected $fillable = [
+        'date',
+        'uid',
+        'register_time',
+        'read_time',
+    ];
+}