You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.6 KiB

3 years ago
<?php
namespace App\Http\Controllers\Worker;
use App\Models\Paramedic;
use App\Worker;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class AuthController extends Controller
{
public $guardName = "worker";
public $authModel;
public function __construct()
{
$this->authModel = new Worker();
}
/**
* Create a new AuthController instance.
*
* @return void
*/
public function guard()
{
return auth()->guard($this->guardName);
}
public function guardName()
{
return $this->guardName;
}
/**
* @OA\Post(
* path="/worker/login-by-username",
* tags={"护工端用户相关"},
* summary="V2-通过用户名密码登录",
* description="",
* @OA\Parameter(name="username", in="query", @OA\Schema(type="string"), required=true, description="用户名(身份证号)"),
* @OA\Parameter(name="password", in="query", @OA\Schema(type="string"), required=true, description="密码"),
* @OA\Response(
* response="200",
* description="护工通过用户名(身份证号)密码登录"
* )
* )
*/
public function loginByUsername()
{
$credentials = [
"id_card_number" => request()->username,
"password" => request()->password
];
if (!$token = $this->guard()->attempt($credentials)) {
return response()->json([
'errorcode' => '401',
'errormsg' => '登录失败'
], 401);
}
return $this->respondWithToken($token);
}
/**
* @OA\Post(
* path="/worker/me",
* tags={"护工端用户相关"},
* summary="V2-获取登录者信息",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* description="",
* @OA\Response(
* response="200",
* description="获取登录者信息"
* )
* )
*/
public function me()
{
$id = $this->guard()->id();
$paramedic = (new Paramedic())->find($id);
return response()->json($paramedic->toArray());
}
/**
* @OA\Post(
* path="/worker/logout",
* tags={"护工端用户相关"},
* summary="V2 退出登录",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* description="",
* @OA\Response(
* response="200",
* description="退出登录"
* )
* )
*/
public function logout()
{
DB::beginTransaction();
try {
$this->guard()->logout();
DB::commit();
return response()->json([
'errormsg' => "退出登录成功!"
]);
} catch (\Exception $exception) {
DB::rollBack();
return response()->json([
'errorcode' => '402',
'errormsg' => $exception->getMessage()
]);
}
}
/**
* Get the token array structure.
*
* @param string $token
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithToken($token)
{
$user = $this->guard()->user();
$user = $user->toArray();
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => $this->guard()->factory()->getTTL() * 60,
'user_info' => $user
]);
}
}