Laravel 5.4不一致,没有响应,日志中没有错误

时间:2018-10-16 18:53:06

标签: php angularjs laravel laravel-5 laravel-5.4

重要编辑::我似乎无法在Chrome上复制该问题,因为它在Firefox上是间歇性的(大约50/50)。而且在Edge上发生的更多(大约70/30)。

我已经检查了其他类似的问题,但是它们不能解决问题。

我在Laravel 5.4中使用了Angular JS,发送了一个GET请求,它有时成功,有时失败。当我说失败时,我的意思是我什么都没得到。我没有收到任何错误,我可以确认laravel.log文件正在记录其他错误,但与此无关。

在控制台中,我有两个示例,一个接一个地完成。

该请求称为get-user-roles,该函数位于UserController@getUserRoles

失败: enter image description here

成功enter image description here 您可以在第二个中看到我正在按预期方式恢复空白数据。

这是我在/ routes中的web.php

Route::group(
    [
        'middleware' => ['cors', 'switchdb'],

    ], function () {

    // auth

    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');

    Route::group(
        [
            'middleware' => ['jwt.auth'],

        ], function () {
        Route::get('get-user-roles', 'UserController@getUserRoles');
    });

    Route::group(
        [
            'middleware' => ['jwt.auth', 'is-not-customer'],

        ], function () {
        // more functions
    });
});

这是我的控制人UserController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Role;
use App\Http\Requests\PublishUserRequest;
use App\Http\Requests\ChangePasswordRequest;
use Auth;


class UserController extends Controller
{

    protected $user;

    public function __construct()
    {
        $this->user = new User();
    }

    public function getUserRoles()
    {
        try {
            $roles = $this->user->getPagesForRoles(Auth::user()->id);
            foreach ($roles as $key => $role) {
                if ($role['hasRole'] == 0) {
                    unset($roles[$key]);
                }
            }

            return response()->json([
                'data' => $roles
            ], 200
            );

        } catch (\Exception $e) {
            return response()->json([
                'message' => $e->getMessage(),
                'line' => $e->getLine()
            ], 400
            );
        }
    }
}

还有用于获取角色的我的模型代码(App\User) ...

public function getPagesForRoles($userID = false)
{
    if (!$userID) {
        $userID = Auth::user()->id;
    }

    $pagesForRoles = $this->role->getRoles();

    // check if user has role or not and add it to data
    if ($userID != 0) {

        foreach ($pagesForRoles as $key => &$role) {
            $hasRole = DB::table('user_roles')
                ->where('role_id', '=', $key)
                ->where('user_id', '=', $userID)
                ->get();

            if (isset($hasRole[0])) {
                if($key == 'admin') {
                    $hasAdminRole = 1;
                }
                $role['hasRole'] = 1;
            } else {
                $role['hasRole'] = 0;
            }
        }
    }

    if($userID == 1 || (isset($hasAdminRole) && $hasAdminRole == 1)) {
        $pagesForRoles = array(
            $pagesForRoles['admin']
        );
        $pagesForRoles[0]['hasRole'] = 1;
    }

    return $pagesForRoles;
}
在模型代码中调用的

getRoles()只是返回一个硬编码的数组。

编辑:值得一提的是,当我在控制器函数中执行die('test');时,每次都会得到响应。

0 个答案:

没有答案
相关问题