80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\api\service\UserService;
|
|
use app\api\validate\UserValidate;
|
|
use app\api\exception\ApiException;
|
|
use app\common\exception\EnumCode;
|
|
use think\Hook;
|
|
use think\Request;
|
|
|
|
class User
|
|
{
|
|
protected $userValidate;
|
|
protected $userService;
|
|
|
|
public function __construct(UserValidate $userValidate, UserService $userService)
|
|
{
|
|
$this->userValidate = $userValidate;
|
|
$this->userService = $userService;
|
|
}
|
|
|
|
/**
|
|
* 注册 function
|
|
*
|
|
* @author dotdotdot <6383846@qq.com>
|
|
* @date 2022-08-06
|
|
* @param Request $request
|
|
* @return array
|
|
*/
|
|
public function register(Request $request)
|
|
{
|
|
$actionAllowed = ['method' => 'register_', 'key' => $request->param('phone'), 'period' => 5];
|
|
|
|
Hook::listen('action_allowed', $actionAllowed);
|
|
|
|
$params = [
|
|
'phone' => $request->param('phone'),
|
|
'password' => $request->param('password'),
|
|
'confirm_password' => $request->param('confirm_password')
|
|
];
|
|
|
|
if (!$this->userValidate->scene('register')->check($params)) {
|
|
throw new ApiException((string)$this->userValidate->getError(), EnumCode::ValidateError);
|
|
}
|
|
|
|
$this->userService->register($params);
|
|
|
|
return json(['code' => EnumCode::Success, 'msg' => 'ok']);
|
|
}
|
|
|
|
/**
|
|
* 登录 function
|
|
*
|
|
* @author dotdotdot <6383846@qq.com>
|
|
* @date 2022-08-08
|
|
* @param Request $request
|
|
* @return array
|
|
*/
|
|
public function login(Request $request)
|
|
{
|
|
$actionAllowed = ['method' => 'login_', 'key' => $request->param('phone'), 'period' => 5];
|
|
|
|
Hook::listen('action_allowed', $actionAllowed);
|
|
|
|
$params = [
|
|
'phone' => $request->param('phone'),
|
|
'password' => $request->param('password')
|
|
];
|
|
|
|
if (!$this->userValidate->scene('login')->check($params)) {
|
|
throw new ApiException((string)$this->userValidate->getError(), EnumCode::ValidateError);
|
|
}
|
|
|
|
$data = $this->userService->login($params);
|
|
|
|
return json(['code' => EnumCode::Success, 'msg' => 'ok', 'data' => $data]);
|
|
}
|
|
}
|