Laravel确认控制器方法并继续执行任务


Laravel confirm controller method and continue the tasks

我有一个网站,里面有laravel和nodejs steam trade bot。在交易报价上,nodejs bot 开火 site.com/api/checkOffer。我想向用户显示网站中的确认框。如果用户点击确认接受交易报价。

这是我的检查报价功能

public function checkOffer()
{
    $data = $this->redis->lrange('check.list', 0, -1);
    foreach($data as $offerJson) {
        $offer = json_decode($offerJson);
        $accountID = $offer->accountid;
        $items = json_decode($offer->items, true);
        $itemsCount = count($items);
        $user = User::where('steamid64', $accountID)->first();
        if (is_null($user)) {
            $this->redis->lrem('usersQueue.list', 1, $accountID);
            $this->redis->lrem('check.list', 0, $offerJson);
            $this->redis->rpush('decline.list', $offer->offerid);
            continue;
        }else{
            if(empty($user->accessToken)){
                $this->redis->lrem('usersQueue.list', 1, $accountID);
                $this->redis->lrem('check.list', 0, $offerJson);
                $this->redis->rpush('decline.list', $offer->offerid);
                $this->_responseErrorToSite('Введите трейд ссылку!', $accountID, self::BET_DECLINE_CHANNEL);
                continue;
            }
        }
        $totalItems = $user->itemsCountByGame($this->game);
        if ($itemsCount > self::MAX_ITEMS || $totalItems > self::MAX_ITEMS || ($itemsCount+$totalItems) > self::MAX_ITEMS) {
            $this->_responseErrorToSite('Максимальное кол-во предметов для депозита - ' . self::MAX_ITEMS, $accountID, self::BET_DECLINE_CHANNEL);
            $this->redis->lrem('usersQueue.list', 1, $accountID);
            $this->redis->lrem('check.list', 0, $offerJson);
            $this->redis->rpush('decline.list', $offer->offerid);
            continue;
        }
        $total_price = $this->_parseItems($items, $missing, $price);
        if ($missing) {
            $this->_responseErrorToSite('Принимаются только предметы из CS:GO', $accountID, self::BET_DECLINE_CHANNEL);
            $this->redis->lrem('usersQueue.list', 1, $accountID);
            $this->redis->lrem('check.list', 0, $offerJson);
            $this->redis->rpush('decline.list', $offer->offerid);
            continue;
        }
        if ($price) {
            $this->_responseErrorToSite('Невозможно определить цену одного из предметов', $accountID, self::BET_DECLINE_CHANNEL);
            $this->redis->lrem('usersQueue.list', 1, $accountID);
            $this->redis->lrem('check.list', 0, $offerJson);
            $this->redis->rpush('decline.list', $offer->offerid);
            continue;
        }
        if ($total_price < self::MIN_PRICE) {
            $this->_responseErrorToSite('Минимальная сумма депозита ' . self::MIN_PRICE . 'р.', $accountID, self::BET_DECLINE_CHANNEL);
            $this->redis->lrem('usersQueue.list', 1, $accountID);
            $this->redis->lrem('check.list', 0, $offerJson);
            $this->redis->rpush('decline.list', $offer->offerid);
            continue;
        }
        $returnValue = [
            'offerid' => $offer->offerid,
            'userid' => $user->id,
            'steamid64' => $user->steamid64,
            'avatar' => $user->avatar,
            'gameid' => $this->game->id,
            'items' => $items,
            'price' => $total_price,
            'success' => true
        ];
        if ($this->game->status == Game::STATUS_PRE_FINISH || $this->game->status == Game::STATUS_FINISHED) {
            $this->_responseMessageToSite('Ваша ставка пойдёт на следующую игру.', $accountID);
            $returnValue['gameid'] = $returnValue['gameid'] + 1;
        }
        $this->redis->rpush('checked.list', json_encode($returnValue));
        $this->redis->lrem('check.list', 0, $offerJson);
    }
    return response()->json(['success' => true]);
}

您应该创建两条路由,例如:

Route::get('api/showForm', 'OfferController@showForm');
Route::post('api/checkOffer', 'OfferController@checkOffer');

然后,您应该创建OfferController控制器,将方法放入其中并创建方法showForm(),该方法返回带有表单的视图。

然后只需在您的视图中使用简单的表单或使用 Laravel Collective Form:: 来创建它:

{!! Form::open(array('route' => 'api/checkOffer', 'class' => 'form'))  !!}
    //
{!! Form::close() !!}