trangshopeeaffiliate/app/Http/Controllers/Web/PublicConvertController.php
2026-07-11 22:56:22 +07:00

70 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Services\LinkConverterService;
use Illuminate\Http\Request;
class PublicConvertController extends Controller
{
protected LinkConverterService $linkConverterService;
public function __construct(LinkConverterService $linkConverterService)
{
$this->linkConverterService = $linkConverterService;
}
public function index()
{
return view('public_convert');
}
public function process(Request $request)
{
$request->validate([
'url' => 'required|url',
'customer_name' => 'nullable|string|max:100',
'customer_phone' => 'nullable|string|max:20',
]);
$url = $request->input('url');
$customerName = $request->input('customer_name');
$customerPhone = $request->input('customer_phone');
try {
// Process link conversion with source type 'public_web' and customer details
$result = $this->linkConverterService->process(
$url,
null,
null,
'public_web',
$customerName,
$customerPhone
);
if ($result->isSuccess) {
// Find the logged history to get cào details (title, price, estimated_commission)
$history = \App\Models\ConversionHistory::where('original_url', $url)
->where('converter_type', 'public_web')
->latest()
->first();
return back()->with([
'success' => 'Chuyển đổi link Shopee Affiliate thành công!',
'original_url' => $url,
'short_url' => $result->affiliateUrl,
'product_title' => $history ? $history->product_title : null,
'product_price' => $history ? $history->product_price : 0.00,
'estimated_commission' => $history ? $history->estimated_commission : 0.00
]);
}
return back()->withInput()->with('error', $result->errorMessage ?: 'Không thể chuyển đổi link này. Vui lòng kiểm tra lại link Shopee.');
} catch (\Exception $e) {
return back()->withInput()->with('error', 'Đã xảy ra lỗi hệ thống: ' . $e->getMessage());
}
}
}