API接口
1.新增鉴权接口/api/v1/authToken 2.修复注册,登录,获取首页初始化数据,获取用户信息接口报错
This commit is contained in:
@@ -19,7 +19,8 @@ use support\Response;
|
||||
class Account extends Frontend
|
||||
{
|
||||
protected array $noNeedLogin = ['retrievePassword'];
|
||||
protected array $noNeedPermission = ['verification', 'changeBind'];
|
||||
// 移动端 API 不走会员权限表(user_group/user_rule)校验,仅校验登录态
|
||||
protected array $noNeedPermission = ['*'];
|
||||
|
||||
public function userProfile(Request $request): Response
|
||||
{
|
||||
@@ -44,7 +45,6 @@ class Account extends Frontend
|
||||
'code' => 1,
|
||||
'message' => __('ok'),
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'username' => $user->username,
|
||||
'head_image' => $user->avatar ?? '',
|
||||
'coin' => $user->coin,
|
||||
@@ -172,7 +172,7 @@ class Account extends Frontend
|
||||
if (!Token::check($params['accountVerificationToken'], $params['type'] . '-pass', $user->id)) {
|
||||
return $this->error(__('You need to verify your account before modifying the binding information'));
|
||||
}
|
||||
} elseif (!isset($params['password']) || !verify_password($params['password'], $user->password, ['salt' => $user->salt])) {
|
||||
} elseif (!isset($params['password']) || !verify_password($params['password'], $user->password, ['salt' => $user->salt ?? ''])) {
|
||||
return $this->error(__('Password error'));
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ class Account extends Frontend
|
||||
$model = $this->auth->getUser();
|
||||
$params = $request->only(['oldPassword', 'newPassword']);
|
||||
|
||||
if (!verify_password($params['oldPassword'], $model->password, ['salt' => $model->salt])) {
|
||||
if (!verify_password($params['oldPassword'], $model->password, ['salt' => $model->salt ?? ''])) {
|
||||
return $this->error(__('Old password error'));
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,8 @@ use support\Response;
|
||||
|
||||
class Auth extends MobileBase
|
||||
{
|
||||
protected array $noNeedLogin = ['userRegister', 'userLogin', 'tokenRefresh'];
|
||||
protected array $noNeedLogin = ['register', 'login', 'refreshToken', 'userRegister', 'userLogin', 'tokenRefresh'];
|
||||
protected array $noNeedAuthToken = ['register', 'login', 'refreshToken', 'userRegister', 'userLogin', 'tokenRefresh'];
|
||||
|
||||
public function userRegister(Request $request): Response
|
||||
{
|
||||
@@ -23,27 +24,22 @@ class Auth extends MobileBase
|
||||
return $response;
|
||||
}
|
||||
|
||||
$account = trim((string) $request->post('account', ''));
|
||||
$accountType = trim((string) $request->post('account_type', ''));
|
||||
$username = trim((string) $request->post('username', ''));
|
||||
if ($username === '') {
|
||||
$username = trim((string) $request->post('account', ''));
|
||||
}
|
||||
$password = (string) $request->post('password', '');
|
||||
$inviteCode = trim((string) $request->post('invite_code', ''));
|
||||
|
||||
if ($account === '' || $accountType === '' || $password === '') {
|
||||
if ($username === '' || $password === '') {
|
||||
return $this->mobileError(1001, 'Missing parameters');
|
||||
}
|
||||
if ($accountType !== 'phone' && $accountType !== 'email') {
|
||||
return $this->mobileError(1003, 'Invalid parameter value');
|
||||
if (!preg_match('/^1[3-9]\d{9}$/', $username)) {
|
||||
return $this->mobileError(1003, 'Please enter the correct mobile number');
|
||||
}
|
||||
|
||||
$username = $account;
|
||||
$mobile = '';
|
||||
$phone = $username;
|
||||
$email = '';
|
||||
if ($accountType === 'phone') {
|
||||
$mobile = $account;
|
||||
}
|
||||
if ($accountType === 'email') {
|
||||
$email = $account;
|
||||
}
|
||||
|
||||
$extend = [];
|
||||
if ($inviteCode !== '') {
|
||||
@@ -56,7 +52,7 @@ class Auth extends MobileBase
|
||||
$extend['channel_id'] = $inviterAdmin['channel_id'] ?? null;
|
||||
}
|
||||
|
||||
$registered = $this->auth->register($username, $password, $mobile, $email, 1, $extend);
|
||||
$registered = $this->auth->register($username, $password, $phone, $email, 1, $extend);
|
||||
if (!$registered) {
|
||||
return $this->mobileError(2000, (string) $this->auth->getError());
|
||||
}
|
||||
@@ -66,17 +62,7 @@ class Auth extends MobileBase
|
||||
return $this->mobileError(2000, 'Registered successfully but login failed');
|
||||
}
|
||||
|
||||
$userInfo = $this->auth->getUserInfo();
|
||||
return $this->mobileSuccess([
|
||||
'user_id' => $userInfo['id'] ?? null,
|
||||
'access_token' => $userInfo['token'] ?? '',
|
||||
'expires_in' => config('buildadmin.user_token_keep_time', 259200),
|
||||
'profile' => [
|
||||
'username' => $userInfo['username'] ?? '',
|
||||
'coin' => $userInfo['coin'] ?? '0.0000',
|
||||
'channel_id' => $userInfo['channel_id'] ?? null,
|
||||
],
|
||||
]);
|
||||
return $this->mobileSuccess($this->buildLoginPayload());
|
||||
}
|
||||
|
||||
public function userLogin(Request $request): Response
|
||||
@@ -86,28 +72,20 @@ class Auth extends MobileBase
|
||||
return $response;
|
||||
}
|
||||
|
||||
$account = trim((string) $request->post('account', ''));
|
||||
$username = trim((string) $request->post('username', ''));
|
||||
if ($username === '') {
|
||||
$username = trim((string) $request->post('account', ''));
|
||||
}
|
||||
$password = (string) $request->post('password', '');
|
||||
if ($account === '' || $password === '') {
|
||||
if ($username === '' || $password === '') {
|
||||
return $this->mobileError(1001, 'Missing parameters');
|
||||
}
|
||||
|
||||
$ok = $this->auth->login($account, $password, true);
|
||||
$ok = $this->auth->login($username, $password, true);
|
||||
if (!$ok) {
|
||||
return $this->mobileError(1101, 'Incorrect account or password');
|
||||
}
|
||||
$userInfo = $this->auth->getUserInfo();
|
||||
return $this->mobileSuccess([
|
||||
'access_token' => $userInfo['token'] ?? '',
|
||||
'refresh_token' => $userInfo['refresh_token'] ?? '',
|
||||
'expires_in' => config('buildadmin.user_token_keep_time', 259200),
|
||||
'user' => [
|
||||
'id' => $userInfo['id'] ?? null,
|
||||
'username' => $userInfo['username'] ?? '',
|
||||
'coin' => $userInfo['coin'] ?? '0.0000',
|
||||
'risk_flags' => $userInfo['risk_flags'] ?? 0,
|
||||
],
|
||||
]);
|
||||
return $this->mobileSuccess($this->buildLoginPayload());
|
||||
}
|
||||
|
||||
public function tokenRefresh(Request $request): Response
|
||||
@@ -130,9 +108,25 @@ class Auth extends MobileBase
|
||||
$newToken = Random::uuid();
|
||||
Token::set($newToken, UserAuth::TOKEN_TYPE, $tokenData['user_id'], config('buildadmin.user_token_keep_time', 259200));
|
||||
return $this->mobileSuccess([
|
||||
'access_token' => $newToken,
|
||||
'user-token' => $newToken,
|
||||
'expires_in' => config('buildadmin.user_token_keep_time', 259200),
|
||||
]);
|
||||
}
|
||||
|
||||
private function buildLoginPayload(): array
|
||||
{
|
||||
$userInfo = $this->auth->getUserInfo();
|
||||
return [
|
||||
'user-token' => $userInfo['token'] ?? '',
|
||||
'refresh_token' => $userInfo['refresh_token'] ?? '',
|
||||
'expires_in' => config('buildadmin.user_token_keep_time', 259200),
|
||||
'user' => [
|
||||
'username' => $userInfo['username'] ?? '',
|
||||
'coin' => $userInfo['coin'] ?? '0.0000',
|
||||
'channel_id' => $userInfo['channel_id'] ?? null,
|
||||
'risk_flags' => $userInfo['risk_flags'] ?? 0,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ class Ems extends Frontend
|
||||
return $this->error(__('Please use the account registration email to send the verification code'));
|
||||
}
|
||||
$password = $request->post('password');
|
||||
if (!verify_password($password, $this->auth->password, ['salt' => $this->auth->salt])) {
|
||||
if (!verify_password($password, $this->auth->password, ['salt' => $this->auth->salt ?? ''])) {
|
||||
return $this->error(__('Password error'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user