You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel开发PTC系统:实现单次单广告展示及刷新切换功能

问题:Laravel PTC系统实现依次展示广告功能

我正在使用Laravel开发一个PTC系统,当前所有可用的PTC广告会一次性全部展示。我需要实现依次展示广告的功能:用户点击「开始」按钮后展示第一个PTC广告,完成任务并查看广告后,刷新页面或返回开始按钮时展示另一个新的PTC广告。

原控制器代码:

public function allAdvertise()
{
    $siteConfigure = Configure::first();

    $activeAds = Ptc::where('status',1)
            ->where('remaining','>',0)
            ->inRandomOrder()
            ->orderBy('id','DESC')
            ->withCount('todayPtcView')
            ->paginate(config('basic.paginate'));
    return view($this->theme . 'user.ptc.allAdvertise', compact('activeAds', 'siteConfigure'));
}

解决方案

核心逻辑是跟踪用户已浏览的广告,每次请求时排除这些广告,返回下一个符合条件的广告。以下提供两种可行方案:

方案一:Session临时存储(适合临时会话场景)

通过Session记录用户当前会话内已浏览的广告ID,每次请求返回一个未浏览的广告:

修改控制器方法

public function showNextAdvertise()
{
    $siteConfigure = Configure::first();
    // 从Session获取已浏览广告ID,默认空数组
    $viewedAds = session()->get('viewed_ptc_ads', []);

    // 查询下一个有效且未浏览的广告
    $nextAd = Ptc::where('status', 1)
        ->where('remaining', '>', 0)
        ->whereNotIn('id', $viewedAds)
        ->inRandomOrder()
        ->withCount('todayPtcView')
        ->first();

    // 无可用广告时清空Session,跳转到无广告页面
    if (!$nextAd) {
        session()->forget('viewed_ptc_ads');
        return view($this->theme . 'user.ptc.noAds', compact('siteConfigure'));
    }

    return view($this->theme . 'user.ptc.singleAdvertise', compact('nextAd', 'siteConfigure'));
}

广告完成后的处理

用户完成广告查看后,将当前广告ID加入Session:

public function completeAdvertise($adId)
{
    // 此处添加广告有效性验证、用户权限检查等逻辑
    
    // 更新已浏览广告列表
    $viewedAds = session()->get('viewed_ptc_ads', []);
    if (!in_array($adId, $viewedAds)) {
        $viewedAds[] = $adId;
        session()->put('viewed_ptc_ads', $viewedAds);
    }

    // 此处添加广告剩余次数更新、用户收益发放等逻辑
    
    return redirect()->route('user.ptc.next');
}

方案二:数据库持久化存储(适合跨会话保留记录)

创建用户广告浏览记录表(如ptc_user_views),字段包含user_id、ptc_id、created_at,用于持久化记录用户的广告浏览历史。

修改控制器方法

public function showNextAdvertise()
{
    $siteConfigure = Configure::first();
    $userId = auth()->id();

    // 获取用户今日已浏览的广告ID
    $viewedAdsIds = DB::table('ptc_user_views')
        ->where('user_id', $userId)
        ->whereDate('created_at', today())
        ->pluck('ptc_id')
        ->toArray();

    // 查询下一个有效且未浏览的广告
    $nextAd = Ptc::where('status', 1)
        ->where('remaining', '>', 0)
        ->whereNotIn('id', $viewedAdsIds)
        ->inRandomOrder()
        ->withCount('todayPtcView')
        ->first();

    if (!$nextAd) {
        return view($this->theme . 'user.ptc.noAds', compact('siteConfigure'));
    }

    return view($this->theme . 'user.ptc.singleAdvertise', compact('nextAd', 'siteConfigure'));
}

广告完成后的处理

用户完成广告后,插入浏览记录到数据库:

public function completeAdvertise($adId)
{
    // 此处添加广告有效性验证、用户权限检查等逻辑
    
    // 记录用户浏览历史
    DB::table('ptc_user_views')->insert([
        'user_id' => auth()->id(),
        'ptc_id' => $adId,
        'created_at' => now()
    ]);

    // 此处添加广告剩余次数更新、用户收益发放等逻辑
    
    return redirect()->route('user.ptc.next');
}

配套调整

  1. 视图文件:
    • 创建singleAdvertise.blade.php,仅展示单个广告和「完成任务」按钮
    • 创建noAds.blade.php,提示用户当前无可用广告
  2. 路由配置:
    Route::get('ptc/next', [PtcController::class, 'showNextAdvertise'])->name('user.ptc.next');
    Route::post('ptc/complete/{adId}', [PtcController::class, 'completeAdvertise'])->name('user.ptc.complete');
    

内容的提问来源于stack exchange,提问作者Obasi Galaxy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:23:26