用户相关api、jwt接入

This commit is contained in:
2025-05-05 14:20:17 +08:00
parent 147b2d3fd5
commit bd26d52fff
11 changed files with 660 additions and 27 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Service;
use App\Models\UserModel;
use App\Service\common\JWTService;
use App\Service\common\UtilsService;
use Exception;
use Illuminate\Support\Str;
class LoginService
@@ -29,11 +30,19 @@ class LoginService
return self::$_instance[$name];
}
/**
* 登录
* @param $account
* @param $password
* @return array
* @throws Exception
*/
public function login($account, $password)
{
$userModel = UserModel::where('account', $account)->first();
if (empty($userModel)) {
return $this->register($account, $password);
UtilsService::getInstance()->errorThrow('用户或密码错误!');
// return $this->register($account, $password);
}
if (!password_verify($password, $userModel->password)) {
@@ -62,6 +71,7 @@ class LoginService
'account' => $userModel->account,
'nick_name' => $userModel->nick_name,
'avatar' => $userModel->avatar,
'email' => $userModel->email,
'ip' => $userModel->ip,
'ip_table' => $userModel->ip_table,
];
@@ -70,7 +80,14 @@ class LoginService
return $result;
}
public function register($account, $password)
/**
* 注册
* @param $account
* @param $password
* @return array
* @throws Exception
*/
public function register($account, $password): array
{
$userModel = UserModel::where('account', $account)->first();
if ($userModel) {
@@ -96,6 +113,7 @@ class LoginService
'account' => $createModel->account,
'nick_name' => $createModel->nick_name,
'avatar' => $createModel->avatar,
'email' => $userModel->email?? '',
'ip' => $createModel->ip,
'ip_table' => $createModel->ip_table,
];

View File

@@ -3,8 +3,151 @@
namespace App\Service;
use App\BaseApp\BaseService;
use App\Enum\UserStatusEnum;
use App\Models\UserModel;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class UserService extends BaseService
{
public function __construct()
{
parent::__construct();
$this->selectField = [ 'id', 'account', 'nick_name', 'avatar', 'email', 'balance', 'qr_code', 'role_id', 'ip', 'ip_table', 'status', 'last_reset_password_at', 'created_at', 'updated_at', 'deleted_at', ];
$this->model = UserModel::class;
}
/**
* 获取用户列表
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list(): array
{
$result = $this->getPageList();
foreach ($result['items'] as &$v) {
$v['ip_table'] = json_decode($v['ip_table'], true);
$v['status_text'] = UserStatusEnum::from($v['status'])->description();
}
return $result;
}
public function detail($id)
{
$result = $this->getDetail($id);
$result['ip_table'] = json_decode($result['ip_table'], true);
$result['status_text'] = UserStatusEnum::from($result['status'])->description();
return $result;
}
public function option()
{
$this->optionField = ['id', 'nick_name'];
return $this->getOption();
}
/**
* 创建用户
* @param $params
* @return mixed
* @throws Exception
*/
public function create($params): mixed
{
$params['ip_table'] = json_encode([]);
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
$existsUser = $this->model::where('account', $params['account'])->whereOr('email', $params['email'])->exists();
if ($existsUser) {
$this->utils->errorThrow('账号或邮箱已存在');
}
return $this->insert($params);
}
/**
* 编辑用户
* @param $id
* @param $params
* @return mixed
* @throws Exception
*/
public function update($id, $params): mixed
{
return $this->save($id, $params);
}
/**
* 删除用户
* @param $ids
* @return mixed|true
* @throws Exception
*/
public function delete($ids): mixed
{
return $this->del($ids);
}
/**
* 修改密码
* @param $id
* @param $password
* @param $newPassword
* @return mixed
* @throws Exception
*/
public function changePassword($password, $newPassword): mixed
{
$info = $this->model::find($this->userId);
if (empty($info)) {
return $this->utils->notFound('数据不存在');
}
if (!password_verify($password, $info['password'])) {
return $this->utils->errorThrow('密码错误');
}
if (password_verify($newPassword, $info['password'])) {
return $this->utils->errorThrow('新密码不能与旧密码相同');
}
$result = $this->model::where('id', $this->userId)->update([
'password' => password_hash($newPassword, PASSWORD_DEFAULT),
'last_reset_password_at' => get_time()
]);
if (!$result) {
return $this->utils->errorThrow('重置密码失败');
}
return $result;
}
/**
* 重置密码
* @param $id
* @param $password
* @param $newPassword
* @return mixed
* @throws Exception
*/
public function resetPassword($id, $password, $newPassword): mixed
{
if ($password !== $newPassword) {
return $this->utils->errorThrow('两次密码不一致');
}
$info = $this->model::find($id);
if (empty($info)) {
return $this->utils->notFound('数据不存在');
}
if (password_verify($newPassword, $info['password'])) {
return $this->utils->errorThrow('新密码不能与旧密码相同');
}
$result = $this->model::where('id', $id)->update([
'password' => password_hash($newPassword, PASSWORD_DEFAULT),
'last_reset_password_at' => get_time()
]);
if (!$result) {
return $this->utils->errorThrow('重置密码失败');
}
return $result;
}
}

View File

@@ -48,7 +48,7 @@ class JWTService
public function generateToken(array $data): string
{
$issuedAt = time();
$expirationTime = $issuedAt + config('xk.redis.jwt_ttl');
$expirationTime = $issuedAt + config('cc.redis.jwt_ttl', 600000);
$payload = [
'iat' => $issuedAt,
@@ -84,7 +84,7 @@ class JWTService
{
try {
$jwt = $this->parseToken();
$user = RedisService::getInstance()->init(config('xk.redis.jwt'))->get($jwt->data->id);
$user = RedisService::getInstance()->init(config('cc.redis.jwt'))->get($jwt->data->id);
if (empty($user)) return UtilsService::getInstance()->notAuth('登录状态过期');
return json_decode($user, true);
} catch (Exception $e) {