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.

230 lines
6.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Manager;
use App\Manager;
use App\Models\Recharge;
use App\Notifications\RechargePaid;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;
use Tymon\JWTAuth\Facades\JWTAuth;
class AuthController extends Controller
{
public $guardName = "manager";
public $appid;
public $appsecret;
public $authModel;
public function __construct()
{
$this->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);
}
/**
* Get the authenticated User.
*
* @return \Illuminate\Http\JsonResponse
*/
public function me()
{
return response()->json($this->guard()->user());
}
/**
* @OA\Post(
* path="/manager/logout",
* summary="退出登录同时把openid清空",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* description="",
* @OA\Response(
* response="200",
* description="退出登录同时把openid清空"
* )
* )
*/
public function logout()
{
DB::beginTransaction();
try {
$user = $this->guard()->user();
$user->update([
"openid" => null
]);
$this->guard()->logout();
DB::commit();
return response()->json([
'errormsg' => "Successfully logged out"
]);
} 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
]);
}
}