2023-02-22 18:00:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Web;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Affiliate\Affiliates;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
class AffiliateController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*/
|
2023-02-23 01:40:40 +00:00
|
|
|
public function index(Request $request): View|RedirectResponse
|
2023-02-22 18:00:53 +00:00
|
|
|
{
|
2023-02-23 01:40:40 +00:00
|
|
|
$user = $request->user('web');
|
2023-02-22 18:00:53 +00:00
|
|
|
$user->load('affiliate');
|
|
|
|
|
|
|
|
$affiliate = $user->affiliate;
|
|
|
|
|
|
|
|
// 检测用户是否激活了推介计划
|
|
|
|
if (! $affiliate) {
|
|
|
|
return redirect()->route('affiliates.create');
|
|
|
|
}
|
|
|
|
|
|
|
|
$affiliateUsers = auth()->user()->affiliateUsers()->paginate(10);
|
|
|
|
|
|
|
|
return view('affiliates.index', compact('affiliateUsers', 'affiliate'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-28 10:04:58 +00:00
|
|
|
* Store a newly created resource in storage.
|
2023-02-22 18:00:53 +00:00
|
|
|
*/
|
2023-02-28 10:04:58 +00:00
|
|
|
public function store(Request $request): RedirectResponse
|
2023-02-22 18:00:53 +00:00
|
|
|
{
|
2023-02-28 10:04:58 +00:00
|
|
|
if (auth()->user()->affiliate) {
|
2023-02-22 18:00:53 +00:00
|
|
|
return redirect()->route('affiliates.index')->with('error', '您已经激活了推介计划。');
|
|
|
|
}
|
|
|
|
|
2023-02-28 10:04:58 +00:00
|
|
|
$request->user('web')->affiliate()->create();
|
|
|
|
|
|
|
|
return redirect()->route('affiliates.index')->with('success', '欢迎您,并感谢您。');
|
2023-02-22 18:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-28 10:04:58 +00:00
|
|
|
* Show the form for creating a new resource.
|
2023-02-22 18:00:53 +00:00
|
|
|
*/
|
2023-02-28 10:04:58 +00:00
|
|
|
public function create(Request $request): View|RedirectResponse
|
2023-02-22 18:00:53 +00:00
|
|
|
{
|
2023-02-28 10:04:58 +00:00
|
|
|
$user = $request->user('web');
|
|
|
|
$user->load('affiliate', 'affiliateUser.affiliate.user');
|
|
|
|
|
|
|
|
if ($user->affiliate) {
|
2023-02-22 18:00:53 +00:00
|
|
|
return redirect()->route('affiliates.index')->with('error', '您已经激活了推介计划。');
|
|
|
|
}
|
|
|
|
|
2023-02-28 10:04:58 +00:00
|
|
|
return view('affiliates.create', compact('user'));
|
2023-02-22 18:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*/
|
2023-02-23 01:59:34 +00:00
|
|
|
public function show($affiliate): RedirectResponse
|
2023-02-22 18:00:53 +00:00
|
|
|
{
|
2023-02-23 02:04:05 +00:00
|
|
|
$redirect = redirect()->route('register');
|
2023-02-23 01:59:34 +00:00
|
|
|
|
|
|
|
$affiliate = Affiliates::where('uuid', $affiliate)->first();
|
|
|
|
|
|
|
|
if (auth('web')->guest() && $affiliate) {
|
2023-02-22 18:00:53 +00:00
|
|
|
session()->put('affiliate_id', $affiliate->id);
|
|
|
|
|
|
|
|
$cache_key = 'affiliate_ip:'.$affiliate->id.':'.request()->ip();
|
|
|
|
|
|
|
|
if (! Cache::has($cache_key)) {
|
|
|
|
$affiliate->increment('visits');
|
|
|
|
Cache::put($cache_key, true, now()->addHour());
|
|
|
|
}
|
2023-02-23 01:59:34 +00:00
|
|
|
} else {
|
|
|
|
$redirect->with('error', '此推介链接已失效。');
|
2023-02-22 18:00:53 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 01:59:34 +00:00
|
|
|
return $redirect;
|
2023-02-22 18:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*/
|
|
|
|
public function destroy(Affiliates $affiliate): RedirectResponse
|
|
|
|
{
|
|
|
|
$affiliate->delete();
|
|
|
|
|
|
|
|
return redirect()->route('affiliates.create')->with('success', '推介计划已经成功删除。');
|
|
|
|
}
|
|
|
|
}
|