appid = env("MANAGER_APPID"); $this->appsecret = env("MANAGER_APPSECRET"); $this->authModel = new Manager(); } /** * Create a new AuthController instance. * * @return void */ public function guard() { return auth()->guard($this->guardName); } public function guardName() { return $this->guardName; } /** * Get a JWT via given credentials. * * @return \Illuminate\Http\JsonResponse */ /** * @OA\Post( * path="/manager/login", * summary="通过code或用户名和密码登录", * description="使用code换取openid进行登录,如果用户不存在则换username、password登录", * @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=true, description="CODE"), * @OA\Parameter(name="username", in="query", @OA\Schema(type="string"), required=false, description="用户名"), * @OA\Parameter(name="password", in="query", @OA\Schema(type="string"), required=false, description="密码"), * @OA\Response( * response="200", * description="管理老师登录接口" * ) * ) */ public function login() { $code = request()->code; $url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"; $url = sprintf($url, $this->appid, $this->appsecret, $code); $res = curl($url); if (!isset($res["openid"])) { return response()->json($res, 401); } $openid = $res["openid"]; //换用户名密码登录 $credentials = request(['username', 'password']); if (!$token = $this->guard()->attempt($credentials)) { return response()->json([ 'errorcode' => '401', 'errormsg' => '登录失败' ], 401); } $user = $this->guard()->user(); $user->update([ "openid" => $openid ]); return $this->respondWithToken($token); } /** * @OA\Post( * path="/manager/login-by-code", * summary="通过微信端获取的code进行登录", * description="", * @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=true, description="code"), * @OA\Response( * response="200", * description="管理老师微信code登录接口" * ) * ) */ public function loginByCode() { $code = request()->code; $url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"; $url = sprintf($url, $this->appid, $this->appsecret, $code); $res = curl($url); if (!isset($res["openid"])) { return response()->json($res, 401); } $openid = $res["openid"]; $user = $this->authModel->where("openid", $openid)->first(); if (!$user) { return response()->json([ 'errorcode' => '401', 'errormsg' => '登录失败' ], 401); } $token = $this->guard()->login($user); return $this->respondWithToken($token); } /** * @OA\Post( * path="/manager/login-by-username", * 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 = request(['username', 'password']); if (!$token = $this->guard()->attempt($credentials)) { return response()->json([ 'errorcode' => '401', 'errormsg' => '登录失败' ], 401); } return $this->respondWithToken($token); } /** * @OA\Post( * path="/manager/me", * 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(); $manager = (new Manager())->with(["projects" => function ($query) { $query->select("project.id", "project.name", "project.address", "project.latitude", "project.longitude"); }])->select("id", "name", "username", "openid", "type", "sex", "mobile", "avatar")->find($id); return response()->json($manager->toArray()); } /** * @OA\Post( * path="/manager/logout", * 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() ]); } } /** * Refresh a token. * * @return \Illuminate\Http\JsonResponse */ public function refresh() { return $this->respondWithToken($this->guard()->refresh()); } /** * Get the token array structure. * * @param string $token * * @return \Illuminate\Http\JsonResponse */ protected function respondWithToken($token) { $user = $this->guard()->user(); $user = (new Manager())->with("projects")->find($user->id); $user->password = null; return response()->json([ 'access_token' => $token, 'token_type' => 'bearer', 'expires_in' => $this->guard()->factory()->getTTL() * 60, 'user_info' => $user ]); } }