Commit 186d5283 by 杨树贤

api返回

parent 84d82062
......@@ -2,7 +2,9 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="fb90add0-1393-48c2-9f26-72365d42cd03" name="变更" comment="">
<change beforePath="$PROJECT_DIR$/.env" beforeDir="false" afterPath="$PROJECT_DIR$/.env" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Api/Controller.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Api/Controller.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/composer.json" beforeDir="false" afterPath="$PROJECT_DIR$/composer.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/public/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/public/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/framework/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/framework/.gitignore" afterDir="false" />
......@@ -147,18 +149,18 @@
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/resources/views/about" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/app/Http" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_package_manager_path" value="npm" />
<property name="vue.rearranger.settings.migration" value="true" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Controllers\Api" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\resources\views\about" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Traits" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Controllers\Api" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Api" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\public" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\resources\views\home" />
......
<?php
namespace App\Http\ApiHelper;
interface ApiCode
{
const API_CODE_SUCCESS = 0;//接口请求正常
const API_CODE_ERROR = 1;//接口请求异常 可预测失败
const API_CODE_NEED_LOGIN = 101; //需要登录
const API_CODE_LOGIN_ERROR = 102; // 登录信息错误
const API_CODE_MOBILE_NOTFOUND_ERROR = 103; // 手机号不存在
const API_CODE_PASSWD_NOTFOUND_ERROR = 104; // 密码错误
const API_CODE_USER_DISABLE_ERROR = 105; // 账号被封禁
}
<?php
/**
* Created by PhpStorm.
* User: duwenjun
* Date: 2021/8/11
* Time: 2:52 PM
*/
namespace App\Http\ApiHelper;
class Response
{
public static function setError($errMsg, $errCode = ApiCode::API_CODE_ERROR, $data = [])
{
return json_encode(['code' => $errCode, 'msg' => $errMsg, 'data' => $data],
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
public static function setSuccess($data, $code = ApiCode::API_CODE_SUCCESS, $msg = "")
{
return json_encode(['code' => $code, 'msg' => $msg, 'data' => $data],
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
}
\ No newline at end of file
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\RedirectsUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
class AuthApiController extends Controller
{
use RedirectsUsers, ThrottlesLogins;
public function login(Request $request)
{
$this->validateLogin($request);
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
}
......@@ -2,13 +2,75 @@
namespace App\Http\Controllers\Api;
use App\Http\ApiHelper\ApiCode;
use App\Http\Traits\Response;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests,Response;
use AuthorizesRequests, DispatchesJobs, ValidatesRequests, Response, AuthenticatesUsers;
public function setSuccessData($data = [], $count = 0, $code = ApiCode::API_CODE_SUCCESS, $msg = 'ok')
{
$res_data = [
"code" => $code,
"data" => $data,
];
if ($msg) {
$res_data['msg'] = $msg;
}
if ($count) {
$res_data['count'] = $count;
}
return response()->json($res_data);
}
public function setSuccess($msg = '操作成功', $code = ApiCode::API_CODE_SUCCESS, $data = [])
{
$res_data = [
"code" => $code,
"msg" => $msg,
'data' => (object)$data,
];
return response()->json($res_data);
}
public function setError($msg, $code = ApiCode::API_CODE_ERROR, $data = [])
{
$res_data = [
"code" => $code,
"msg" => $msg,
];
if ($data) {
$res_data['data'] = $data;
}
$this->logErr($msg, $code = ApiCode::API_CODE_ERROR, $data = null);
return response()->json($res_data);
}
private function logErr($msg, $code = ApiCode::API_CODE_ERROR, $data = null)
{
$request_uri = $_SERVER['REQUEST_URI'] ?? '';
$path_info = parse_url($request_uri);
$err_info = [
'domain' => $_SERVER['HTTP_HOST'] ?? '',
'interface' => isset($path_info) ? $path_info['path'] : '',
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'ip' => request()->getClientIp(),
'time' => time(),
'other' => '',
'request_params' => $_REQUEST,
'msg' => $msg,
"code" => $code,
"data" => $data
];
Log::error(json_encode($err_info, JSON_UNESCAPED_UNICODE));
}
}
......@@ -14,7 +14,8 @@
"guzzlehttp/guzzle": "^6.3.1|^7.0.1",
"laravel/framework": "^7.29",
"laravel/tinker": "^2.5",
"laravel/ui": "2.*"
"laravel/ui": "2.*",
"ext-json": "*"
},
"require-dev": {
"facade/ignition": "^2.0",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment