71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\note\controller\v1;
|
|
|
|
use app\api\common\Response;
|
|
use app\note\controller\BaseController;
|
|
use app\note\service\AuthService;
|
|
use think\App;
|
|
use think\exception\ValidateException;
|
|
|
|
/**
|
|
* 笔记小程序认证控制器
|
|
*/
|
|
class Auth extends BaseController
|
|
{
|
|
/**
|
|
* @var PlanningService
|
|
*/
|
|
protected $authService;
|
|
|
|
public function __construct(App $app)
|
|
{
|
|
parent::__construct($app);
|
|
$this->authService = new AuthService();
|
|
}
|
|
|
|
/**
|
|
* 微信小程序登录
|
|
* POST /note/v1/auth/wechat-login
|
|
*/
|
|
public function wechatLogin()
|
|
{
|
|
try {
|
|
$data = $this->request->post();
|
|
|
|
validate([
|
|
'code' => 'require',
|
|
], [
|
|
'code.require' => '微信登录 code 不能为空',
|
|
])->check($data);
|
|
|
|
$result = $this->authService->wechatLogin(
|
|
(string) $data['code'],
|
|
isset($data['nickname']) ? (string) $data['nickname'] : null,
|
|
isset($data['avatar_url']) ? (string) $data['avatar_url'] : null
|
|
);
|
|
|
|
return Response::success($result, '登录成功');
|
|
} catch (ValidateException $e) {
|
|
return Response::error($e->getMessage(), 400);
|
|
} catch (\Throwable $e) {
|
|
return Response::error($e->getMessage(), $e->getCode() ?: 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取当前小程序用户信息
|
|
* GET /note/v1/auth/me
|
|
*/
|
|
public function me()
|
|
{
|
|
try {
|
|
$result = $this->authService->getUserInfo($this->getCurrentNoteUserId());
|
|
return Response::success($result);
|
|
} catch (\Throwable $e) {
|
|
return Response::error($e->getMessage(), $e->getCode() ?: 500);
|
|
}
|
|
}
|
|
}
|