2025-05-12 00:19:35 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
2025-05-12 13:12:57 +08:00
|
|
|
|
use App\BaseApp\BaseNotAuthService;
|
2025-05-12 09:04:46 +08:00
|
|
|
|
use App\Models\AdminModel;
|
2025-05-12 00:19:35 +08:00
|
|
|
|
use App\Service\common\JWTService;
|
|
|
|
|
|
use App\Service\common\UtilsService;
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
2025-05-12 13:12:57 +08:00
|
|
|
|
class LoginService extends BaseNotAuthService
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 登录
|
2025-05-12 09:04:46 +08:00
|
|
|
|
* @param $phone
|
2025-05-12 00:19:35 +08:00
|
|
|
|
* @param $password
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2025-05-12 09:04:46 +08:00
|
|
|
|
public function login($phone, $password): array
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
2025-05-12 13:12:57 +08:00
|
|
|
|
$userModel = AdminModel::with(['role:id,name,value'])->where('phone', $phone)->first();
|
2025-05-12 00:19:35 +08:00
|
|
|
|
if (empty($userModel)) {
|
|
|
|
|
|
UtilsService::getInstance()->errorThrow('用户或密码错误!');
|
2025-05-12 09:04:46 +08:00
|
|
|
|
// return $this->register($phone, $password);
|
2025-05-12 00:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!password_verify($password, $userModel->password)) {
|
2025-05-12 13:12:57 +08:00
|
|
|
|
UtilsService::getInstance()->errorThrow('用户或密码错误2!');
|
2025-05-12 00:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($userModel->ip !== get_ip()) {
|
|
|
|
|
|
$ipTable = json_decode($userModel->ip_table, true);
|
2025-05-12 13:12:57 +08:00
|
|
|
|
if (!in_array(get_ip(), $ipTable?? [])) {
|
2025-05-12 00:19:35 +08:00
|
|
|
|
$ipTable[] = get_ip();
|
|
|
|
|
|
}
|
2025-05-12 09:04:46 +08:00
|
|
|
|
$update = AdminModel::where('id', $userModel->id)->update([
|
2025-05-12 00:19:35 +08:00
|
|
|
|
'ip' => get_ip(),
|
|
|
|
|
|
'ip_table' => json_encode($ipTable),
|
|
|
|
|
|
'updated_at' => get_time()
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
if (!$update) {
|
|
|
|
|
|
UtilsService::getInstance()->errorThrow('更新失败!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-12 09:04:46 +08:00
|
|
|
|
$userModel = AdminModel::where('id', $userModel->id)->first();
|
2025-05-12 00:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
$result = [
|
|
|
|
|
|
'id' => $userModel->id,
|
2025-05-12 09:04:46 +08:00
|
|
|
|
'phone' => $userModel->phone,
|
2025-05-12 00:19:35 +08:00
|
|
|
|
'nick_name' => $userModel->nick_name,
|
|
|
|
|
|
'avatar' => $userModel->avatar,
|
|
|
|
|
|
'email' => $userModel->email,
|
2025-05-12 16:13:46 +08:00
|
|
|
|
'role_id' => $userModel->role_id,
|
2025-05-12 13:12:57 +08:00
|
|
|
|
'role_name' => $userModel->role->name,
|
|
|
|
|
|
'role_value' => $userModel->role->value,
|
2025-05-12 00:19:35 +08:00
|
|
|
|
'ip' => $userModel->ip,
|
|
|
|
|
|
'ip_table' => $userModel->ip_table,
|
|
|
|
|
|
];
|
|
|
|
|
|
$token = JWTService::getInstance()->generateToken($result);
|
|
|
|
|
|
$result['token'] = $token;
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 注册
|
2025-05-12 09:04:46 +08:00
|
|
|
|
* @param $phone
|
2025-05-12 00:19:35 +08:00
|
|
|
|
* @param $password
|
|
|
|
|
|
* @param $email
|
|
|
|
|
|
* @param $code
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2025-05-12 09:04:46 +08:00
|
|
|
|
public function register($phone, $password, $email, $code): array
|
2025-05-12 00:19:35 +08:00
|
|
|
|
{
|
2025-05-12 09:04:46 +08:00
|
|
|
|
$userModel = AdminModel::where('phone', $phone)->first();
|
2025-05-12 00:19:35 +08:00
|
|
|
|
if ($userModel) {
|
|
|
|
|
|
UtilsService::getInstance()->errorThrow('账号已被占用!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-12 09:04:46 +08:00
|
|
|
|
$createModel = AdminModel::create([
|
|
|
|
|
|
'phone' => $phone,
|
2025-05-12 00:19:35 +08:00
|
|
|
|
'email' => $email,
|
|
|
|
|
|
'password' => password_hash($password, PASSWORD_DEFAULT),
|
|
|
|
|
|
'nick_name' => '新用户'. Str::random(),
|
|
|
|
|
|
'avatar' => 'https://pic.rmb.bdstatic.com/bjh/80852bfe7c321988191838517ba64e309354.jpeg@h_1280',
|
|
|
|
|
|
'role_id' => 2,
|
|
|
|
|
|
'ip' => get_ip(),
|
|
|
|
|
|
'ip_table' => json_encode([get_ip()]),
|
|
|
|
|
|
'created_at' => get_time()
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
if (!$createModel) {
|
|
|
|
|
|
UtilsService::getInstance()->errorThrow('注册失败!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-12 13:12:57 +08:00
|
|
|
|
$userInfo = AdminModel::with(['role:id,name,value'])->where('id', $createModel->id)->first();
|
2025-05-12 00:19:35 +08:00
|
|
|
|
|
|
|
|
|
|
$result = [
|
|
|
|
|
|
'id' => $userInfo->id,
|
2025-05-12 09:04:46 +08:00
|
|
|
|
'phone' => $userInfo->phone,
|
2025-05-12 00:19:35 +08:00
|
|
|
|
'nick_name' => $userInfo->nick_name,
|
|
|
|
|
|
'avatar' => $userInfo->avatar,
|
|
|
|
|
|
'email' => $userInfo->email?? '',
|
2025-05-12 13:12:57 +08:00
|
|
|
|
'role_name' => $userInfo->role->name,
|
|
|
|
|
|
'role_value' => $userInfo->role->value,
|
2025-05-12 00:19:35 +08:00
|
|
|
|
'ip' => $userInfo->ip,
|
|
|
|
|
|
'ip_table' => $userInfo->ip_table,
|
|
|
|
|
|
];
|
|
|
|
|
|
$token = JWTService::getInstance()->generateToken($result);
|
|
|
|
|
|
$result['token'] = $token;
|
|
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|