瀏覽代碼

Add #120 邀请链接用户信息字符模糊化

兔姬桑 4 年之前
父節點
當前提交
7a54b61579

+ 24 - 7
app/Http/Controllers/AuthController.php

@@ -19,6 +19,7 @@ use Cache;
 use Captcha;
 use Cookie;
 use Hash;
+use Hashids\Hashids;
 use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
 use Log;
@@ -202,7 +203,7 @@ class AuthController extends Controller
         }
         $data = $request->validated();
         $register_token = $request->input('register_token');
-        $code = $request->input('code');
+        $invite_code = $request->input('invite_code');
         $verify_code = $request->input('verify_code');
         $aff = $request->input('aff');
 
@@ -229,8 +230,8 @@ class AuthController extends Controller
         // 如果需要邀请注册
         if (sysConfig('is_invite_register')) {
             // 校验邀请码合法性
-            if ($code) {
-                if (Invite::whereCode($code)->whereStatus(0)->doesntExist()) {
+            if ($invite_code) {
+                if (Invite::whereCode($invite_code)->whereStatus(0)->doesntExist()) {
                     return Redirect::back()->withInput($request->except('code'))->withErrors(trans('auth.invite.error.unavailable'));
                 }
             } elseif ((int) sysConfig('is_invite_register') === 2) { // 必须使用邀请码
@@ -267,14 +268,14 @@ class AuthController extends Controller
             }
         }
 
-        // 获取可用端口 TODO: 修改判断&提示
+        // 获取可用端口
         $port = Helpers::getPort();
         if ($port > sysConfig('max_port')) {
             return Redirect::back()->withInput()->withErrors(trans('auth.register.error.disable'));
         }
 
         // 获取aff
-        $affArr = $this->getAff($code, $aff);
+        $affArr = $this->getAff($invite_code, $aff);
         $inviter_id = $affArr['inviter_id'];
 
         $transfer_enable = MB * ((int) sysConfig('default_traffic') + ($inviter_id ? (int) sysConfig('referral_traffic') : 0));
@@ -400,15 +401,31 @@ class AuthController extends Controller
             // 检查一下cookie里有没有aff
             $cookieAff = \request()->cookie('register_aff');
             if ($cookieAff) {
-                $data['inviter_id'] = User::find($cookieAff) ? $cookieAff : null;
+                $cookieAff = $this->affConvert($cookieAff);
+                $data['inviter_id'] = $cookieAff && User::find($cookieAff) ? $cookieAff : null;
             } elseif ($aff) { // 如果cookie里没有aff,就再检查一下请求的url里有没有aff,因为有些人的浏览器会禁用了cookie,比如chrome开了隐私模式
-                $data['inviter_id'] = User::find($aff) ? $aff : null;
+                $aff = $this->affConvert($aff);
+                $data['inviter_id'] = $aff && User::find($aff) ? $aff : null;
             }
         }
 
         return $data;
     }
 
+    private function affConvert($aff)
+    {
+        if (is_numeric($aff)) {
+            return $aff;
+        } else {
+            $decode = (new Hashids(sysConfig('aff_salt'), 8))->decode($aff);
+            if (isset($decode)) {
+                return $decode[0];
+            }
+        }
+
+        return false;
+    }
+
     // 生成申请的请求地址
     private function addVerifyUrl($uid, $email)
     {

+ 16 - 8
app/Http/Controllers/User/AffiliateController.php

@@ -7,6 +7,7 @@ use App\Models\Order;
 use App\Models\ReferralApply;
 use App\Models\ReferralLog;
 use Auth;
+use Hashids\Hashids;
 use Illuminate\Http\JsonResponse;
 use Response;
 
@@ -19,16 +20,23 @@ class AffiliateController extends Controller
             return Response::view('auth.error', ['message' => trans('user.purchase_required').'<a class="btn btn-sm btn-danger" href="/">'.trans('common.back').'</a>'], 402);
         }
 
+        $affSalt = sysConfig('aff_salt');
+        if (isset($affSalt)) {
+            $aff_link = route('register', ['aff' => (new Hashids($affSalt, 8))->encode(Auth::id())]);
+        } else {
+            $aff_link = route('register', ['aff' => Auth::id()]);
+        }
+
         return view('user.referral', [
-            'referral_traffic' => flowAutoShow(sysConfig('referral_traffic') * MB),
-            'referral_percent' => sysConfig('referral_percent'),
-            'referral_money' => sysConfig('referral_money'),
-            'totalAmount' => ReferralLog::uid()->sum('commission') / 100,
-            'canAmount' => ReferralLog::uid()->whereStatus(0)->sum('commission') / 100,
-            'aff_link' => route('register', ['aff' => Auth::id()]),
-            'referralLogList' => ReferralLog::uid()->with('invitee:id,email')->latest()->paginate(10, ['*'], 'log_page'),
+            'referral_traffic'  => flowAutoShow(sysConfig('referral_traffic') * MB),
+            'referral_percent'  => sysConfig('referral_percent'),
+            'referral_money'    => sysConfig('referral_money'),
+            'totalAmount'       => ReferralLog::uid()->sum('commission') / 100,
+            'canAmount'         => ReferralLog::uid()->whereStatus(0)->sum('commission') / 100,
+            'aff_link'          => $aff_link,
+            'referralLogList'   => ReferralLog::uid()->with('invitee:id,email')->latest()->paginate(10, ['*'], 'log_page'),
             'referralApplyList' => ReferralApply::uid()->latest()->paginate(10, ['*'], 'apply_page'),
-            'referralUserList' => Auth::getUser()->invitees()->select(['email', 'created_at'])->latest()->paginate(10, ['*'], 'user_page'),
+            'referralUserList'  => Auth::getUser()->invitees()->select(['email', 'created_at'])->latest()->paginate(10, ['*'], 'user_page'),
         ]);
     }
 

+ 1 - 0
composer.json

@@ -18,6 +18,7 @@
     "fruitcake/laravel-cors": "^2.0",
     "geoip2/geoip2": "^v2.11",
     "guzzlehttp/guzzle": "^6.3.1|^7.0.1",
+    "hashids/hashids": "^4.1",
     "ip2location/ip2location-laravel": "^1.2",
     "ipip/db": "^1.0",
     "jenssegers/agent": "^2.6",

+ 71 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "2ee93ccff46fe286f1174eb0d199f128",
+    "content-hash": "0e7fb2dbb9264d385f8aa2f9c2725596",
     "packages": [
         {
             "name": "asm89/stack-cors",
@@ -1421,6 +1421,76 @@
             },
             "time": "2020-09-30T07:37:11+00:00"
         },
+        {
+            "name": "hashids/hashids",
+            "version": "4.1.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/vinkla/hashids.git",
+                "reference": "8cab111f78e0bd9c76953b082919fc9e251761be"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/vinkla/hashids/zipball/8cab111f78e0bd9c76953b082919fc9e251761be",
+                "reference": "8cab111f78e0bd9c76953b082919fc9e251761be",
+                "shasum": ""
+            },
+            "require": {
+                "ext-mbstring": "*",
+                "php": "^7.2 || ^8.0"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "^8.0 || ^9.4",
+                "squizlabs/php_codesniffer": "^3.5"
+            },
+            "suggest": {
+                "ext-bcmath": "Required to use BC Math arbitrary precision mathematics (*).",
+                "ext-gmp": "Required to use GNU multiple precision mathematics (*)."
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "4.1-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Hashids\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Ivan Akimov",
+                    "email": "[email protected]"
+                },
+                {
+                    "name": "Vincent Klaiber",
+                    "email": "[email protected]"
+                }
+            ],
+            "description": "Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers",
+            "homepage": "https://hashids.org/php",
+            "keywords": [
+                "bitly",
+                "decode",
+                "encode",
+                "hash",
+                "hashid",
+                "hashids",
+                "ids",
+                "obfuscate",
+                "youtube"
+            ],
+            "support": {
+                "issues": "https://github.com/vinkla/hashids/issues",
+                "source": "https://github.com/vinkla/hashids/tree/4.1.0"
+            },
+            "time": "2020-11-26T19:24:33+00:00"
+        },
         {
             "name": "intervention/image",
             "version": "2.5.1",

+ 22 - 0
database/migrations/2021_03_17_041036_add_aff_code_config.php

@@ -0,0 +1,22 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+
+class AddAffCodeConfig extends Migration
+{
+    protected $newConfigs = [
+        'aff_salt',
+    ];
+
+    public function up()
+    {
+        foreach ($this->newConfigs as $config) {
+            \App\Models\Config::insert(['name' => $config]);
+        }
+    }
+
+    public function down()
+    {
+        \App\Models\Config::whereIn('name', $this->newConfigs)->delete();
+    }
+}

+ 14 - 0
resources/views/admin/config/system.blade.php

@@ -291,6 +291,20 @@
                                             </div>
                                         </div>
                                     </div>
+                                    <div class="form-group col-lg-6">
+                                        <div class="row">
+                                            <label class="col-md-3" for="aff_salt">邀请链接 用户信息字符化</label>
+                                            <div class="col-md-6">
+                                                <div class="input-group">
+                                                    <input type="text" class="form-control" id="aff_salt" value="{{$aff_salt}}"/>
+                                                    <span class="input-group-append">
+                                                        <button class="btn btn-primary" type="button" onclick="update('aff_salt')">{{trans('common.update')}}</button>
+                                                    </span>
+                                                </div>
+                                                <span class="text-help"> 留空时,邀请链接将显示用户ID;填入任意英文/数字 即可对用户链接ID进行加密 </span>
+                                            </div>
+                                        </div>
+                                    </div>
                                     <div class="form-group col-lg-6">
                                         <div class="row">
                                             <label class="col-md-3 col-form-label" for="is_rand_port">随机端口</label>

+ 1 - 1
resources/views/admin/inviteList.blade.php

@@ -50,7 +50,7 @@
                                     <td> {{$invite->id}} </td>
                                     <td>
                                         <a href="javascript:void(0)" class="mt-clipboard" data-clipboard-action="copy"
-                                           data-clipboard-text="{{route('register',['code' => $invite->code])}}">{{$invite->code}}</a>
+                                           data-clipboard-text="{{route('register',['invite_code' => $invite->code])}}">{{$invite->code}}</a>
                                     </td>
                                     <td> {{$invite->dateline}} </td>
                                     <td>

+ 1 - 1
resources/views/auth/free.blade.php

@@ -15,7 +15,7 @@
                     <tbody>
                     @foreach($inviteList as $invite)
                         <tr>
-                            <td><a href="{{route('register', ['code' => $invite->code])}}" target="_blank">{{$invite->code}}</a></td>
+                            <td><a href="{{route('register', ['invite_code' => $invite->code])}}" target="_blank">{{$invite->code}}</a></td>
                             <td> {{$invite->dateline}} </td>
                         </tr>
                     @endforeach

+ 1 - 1
resources/views/user/invite.blade.php

@@ -46,7 +46,7 @@
                                     <td> {{$loop->iteration}} </td>
                                     <td>
                                         <a href="javascript:void(0)" class="mt-clipboard" data-clipboard-action="copy"
-                                           data-clipboard-text="{{route('register', ['aff' => Auth::id(), 'code' => $invite->code])}}">{{$invite->code}}</a>
+                                           data-clipboard-text="{{route('register', ['invite_code' => $invite->code])}}">{{$invite->code}}</a>
                                     </td>
                                     <td> {{$invite->dateline}} </td>
                                     <td>