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/update", * summary="V2-登录者个人信息修改", * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=false, description="姓名"), * @OA\Parameter(name="sex", in="query", @OA\Schema(type="string"), required=false, description="性别:[男,女]"), * @OA\Parameter(name="password", in="query", @OA\Schema(type="string"), required=false, description="密码"), * @OA\Parameter(name="openid", in="query", @OA\Schema(type="string"), required=false, description="微信openid"), * @OA\Parameter(name="unionid", in="query", @OA\Schema(type="string"), required=false, description="微信unionid"), * @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="手机号码"), * @OA\Parameter(name="birthday", in="query", @OA\Schema(type="string"), required=false, description="生日"), * @OA\Parameter(name="avatar", in="query", @OA\Schema(type="string"), required=false, description="头像访问路径(相对于根目录的绝对路径)"), * description="", * @OA\Response( * response="200", * description="登录者个人信息修改" * ) * ) */ public function update() { $manager = $this->guard()->user(); $fillable = (new Manager())->getFillable(); $update = []; foreach (request()->all() as $k => $v) { if (in_array($k, $fillable)) { $update[$k] = $v; } } $manager->update($update); 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() ]); } } /** * @OA\Post( * path="/manager/refresh", * summary="V2-刷新token", * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * description="", * @OA\Response( * response="200", * description="刷新token" * ) * ) */ /** * Refresh a token. * * @return \Illuminate\Http\JsonResponse */ public function refresh() { $token = JWTAuth::getToken(); dd($token); 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; $user = $user->toArray(); return response()->json([ 'access_token' => $token, 'token_type' => 'bearer', 'expires_in' => $this->guard()->factory()->getTTL() * 60, 'user_info' => $user ]); } /** * @OA\Get( * path="/manager/get-notifications", * summary="V2-获取通知列表", * description="获取通知列表", * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Parameter(name="page", in="query", @OA\Schema(type="integer"), required=false, description="当前页码,默认为1"), * @OA\Parameter(name="page_size", in="query", @OA\Schema(type="integer"), required=false, description="每页数量,默认为10"), * @OA\Parameter(name="order_by", in="query", @OA\Schema(type="integer"), required=false, description="排序方法[created_at|desc/created_at|asc],默认为created_at|desc"), * @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), required=false, description="已读状态[read/unread],默认为空即全部内容,read表示已读,unread表示未读"), * @OA\Response( * response="200", * description="获取通知列表" * ) * ) */ public function getNotifications() { $page_size = request()->page_size ? (int)request()->page_size : 10; if (in_array(request()->order_by, ["created_at|desc", "created_at|asc"])) { $order_by = explode("|", request()->order_by); } else { $order_by = ["created_at", "desc"]; } $notifications = new Notifications(); switch (request()->status) { case "read": $notifications = $notifications->whereNotNull("read_at"); break; case "unread": $notifications = $notifications->whereNull("read_at"); break; } $notifications = $notifications->where("notifiable_type", Manager::class) ->where("notifiable_id", $this->guard()->id()) ->select("id", "type", "notifiable_type", "notifiable_id", "data", "read_at", "created_at") ->orderBy($order_by[0], $order_by[1]) ->paginate($page_size); return response()->json($notifications->toArray()); } /** * @OA\Get( * path="/manager/get-notification/{id}", * summary="V2-获取通知内容", * description="获取通知内容", * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="获取通知内容" * ) * ) */ public function getNotification($id) { $notification = (new Notifications())->where("id", $id)->select("id", "type", "notifiable_type", "notifiable_id", "data", "read_at", "created_at")->first(); if (!$notification) { return response()->json([ 'errorcode' => '801', 'errormsg' => "获取通知内容失败" ]); } $notification->update([ "read_at" => date("Y-m-d H:i:s") ]); return response()->json($notification->toArray()); } }