data_center/app/api/logic/LoginLogic.php

166 lines
5.7 KiB
PHP
Raw Normal View History

2023-09-18 09:11:13 +08:00
<?php
namespace app\api\logic;
use app\common\cache\UserTokenCache;
use app\common\logic\BaseLogic;
use app\api\service\UserTokenService;
2023-11-10 18:09:36 +08:00
use app\api\service\JwtTokenService;
2023-09-18 17:51:25 +08:00
use app\common\model\auth\Admin;
2023-09-18 09:11:13 +08:00
use app\common\service\ConfigService;
use app\common\model\user\User;
use think\facade\Config;
2023-09-18 17:51:25 +08:00
use think\facade\Db;
2023-09-18 17:04:42 +08:00
use think\facade\Log;
2023-09-18 09:11:13 +08:00
/**
* 登录逻辑
* Class LoginLogic
* @package app\api\logic
*/
class LoginLogic extends BaseLogic
{
// 账号密码注册
public static function register(array $params): bool
{
//创建密码和默认头像
$passwordSalt = Config::get('project.unique_identification');
2023-09-18 17:51:25 +08:00
$adminPassword = create_password($params['phone'], $passwordSalt);
$userPassword = create_password($params['password'], $passwordSalt);
2023-09-18 09:11:13 +08:00
$avatar = env('project.web_domain').'/'.ConfigService::get('default_image', 'user_avatar');
2023-09-19 10:39:28 +08:00
Db::startTrans();
2023-09-18 09:11:13 +08:00
try {
2023-09-18 17:51:25 +08:00
//添加到用户表
$user = User::create([
2023-11-13 11:21:30 +08:00
'admin_id' => 0,
2023-09-18 17:51:25 +08:00
'phone' => $params['phone'],
2023-09-19 10:39:28 +08:00
'nick_name' => '用户'.$params['phone'],
2023-09-18 17:51:25 +08:00
'password' => $userPassword,
'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'],
2023-09-19 10:39:28 +08:00
'age' => empty($params['age']) ? 0 : $params['age'],
'gender' => empty($params['gender']) ? 0 : $params['gender'],
'real_name' => empty($params['real_name']) ? '' : $params['real_name'],
'id_card' => empty($params['id_card']) ? '' : $params['id_card'],
'province' => empty($params['province']) ? 0 : $params['province'],
'city' => empty($params['city']) ? 0 : $params['city'],
'area' => empty($params['area']) ? 0 : $params['area'],
'street' => empty($params['street']) ? 0 : $params['street'],
'village' => empty($params['village']) ? 0 : $params['village'],
'brigade' => empty($params['brigade']) ? 0 : $params['brigade'],
'address' => empty($params['address']) ? '' : $params['address'],
2023-09-18 17:51:25 +08:00
]);
if(!empty($admin['id']) && !empty($user['id'])){
2023-09-19 10:39:28 +08:00
Db::commit();
2023-09-18 17:51:25 +08:00
return true;
}else{
2023-09-19 10:39:28 +08:00
Db::rollback();
2023-09-18 17:51:25 +08:00
return false;
}
2023-09-18 09:11:13 +08:00
} catch (\Exception $e) {
2023-09-19 10:39:28 +08:00
Db::rollback();
2023-09-18 17:04:42 +08:00
Log::error($e->getMessage());
2023-09-18 09:11:13 +08:00
self::setError($e->getMessage());
return false;
}
}
// 账号/手机号登录,手机号验证码
public static function login($params): bool|array
{
try {
2023-09-18 17:51:25 +08:00
$where = ['phone' => $params['account']];
2023-11-10 18:09:36 +08:00
$user = User::field(['id', 'phone'])->where($where)->findOrEmpty();
2023-09-18 09:11:13 +08:00
//更新登录信息
2023-09-18 17:51:25 +08:00
$user->last_login_time = time();
$user->last_login_ip = request()->ip();
2023-09-18 09:11:13 +08:00
$user->save();
//设置token
2023-11-10 18:09:36 +08:00
$userInfo = UserTokenService::setToken($user->id, 0);
2023-09-18 09:11:13 +08:00
return [
2023-09-19 10:39:28 +08:00
'uid' => $userInfo['user_id'],
2023-09-18 09:11:13 +08:00
'phone' => $userInfo['phone'],
'token' => $userInfo['token'],
];
} catch (\Exception $e) {
2023-09-18 17:04:42 +08:00
//记录日志
Log::error($e->getMessage());
2023-09-18 09:11:13 +08:00
self::setError($e->getMessage());
return false;
}
}
2023-11-10 18:09:36 +08:00
// 账号/手机号登录,手机号验证码
public static function loginJwt($params): bool|array
{
try {
$where = ['phone' => $params['account']];
2023-11-13 15:18:31 +08:00
$user = User::field(['id', 'phone', 'avatar', 'nick_name'])->where($where)->findOrEmpty();
2023-11-10 18:09:36 +08:00
//更新登录信息
$user->last_login_time = time();
$user->last_login_ip = request()->ip();
$user->save();
//设置token
2023-11-13 15:18:31 +08:00
$userInfo = JwtTokenService::createToken($user->id, $user->toArray());
2023-11-10 18:09:36 +08:00
return [
'uid' => $userInfo['uid'],
'token' => $userInfo['token']
];
} catch (\Exception $e) {
//记录日志
Log::error($e->getMessage());
self::setError($e->getMessage());
return false;
}
}
2023-09-18 09:11:13 +08:00
// 退出登录
public static function logout($userInfo): bool
{
//token不存在不注销
if (!isset($userInfo['token'])) {
return false;
}
//设置token过期
return UserTokenService::expireToken($userInfo['token']);
}
// 验证token
public static function verifyToken($params): bool|array
{
try {
$userInfo = (new UserTokenCache())->getUserInfo($params['token']);
2023-09-19 10:39:28 +08:00
if(empty($userInfo) || $userInfo['user_id'] != $params['uid']){
2023-09-18 09:11:13 +08:00
self::setError('token无效');
return false;
}
return [
2023-09-19 10:39:28 +08:00
'uid' => $userInfo['user_id'],
2023-09-18 09:11:13 +08:00
'phone' => $userInfo['phone'],
'token' => $userInfo['token'],
];
} catch (\Exception $e) {
2023-09-18 17:04:42 +08:00
//记录日志
Log::error($e->getMessage());
2023-09-18 09:11:13 +08:00
self::setError($e->getMessage());
return false;
}
}
2023-11-10 18:09:36 +08:00
// 验证token
public static function verifyJwtToken($params): bool|array
{
try {
$userInfo = JwtTokenService::parseToken($params['token']);
2023-11-13 15:18:31 +08:00
return $userInfo;
2023-11-10 18:09:36 +08:00
return [
'uid' => $userInfo['uid'],
'phone' => $userInfo['phone']
];
} catch (\Exception $e) {
//记录日志
Log::error($e->getMessage());
self::setError($e->getMessage());
return false;
}
}
2023-09-18 09:11:13 +08:00
}