131 lines
4.3 KiB
PHP
131 lines
4.3 KiB
PHP
<?php
|
||
namespace app\api\logic;
|
||
|
||
use app\common\cache\UserTokenCache;
|
||
use app\common\logic\BaseLogic;
|
||
use app\api\service\UserTokenService;
|
||
use app\common\model\auth\Admin;
|
||
use app\common\service\ConfigService;
|
||
use app\common\model\user\User;
|
||
use think\facade\Config;
|
||
use think\facade\Db;
|
||
use think\facade\Log;
|
||
|
||
/**
|
||
* 登录逻辑
|
||
* Class LoginLogic
|
||
* @package app\api\logic
|
||
*/
|
||
class LoginLogic extends BaseLogic
|
||
{
|
||
// 账号密码注册
|
||
public static function register(array $params): bool
|
||
{
|
||
//创建密码和默认头像
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
$adminPassword = create_password($params['phone'], $passwordSalt);
|
||
$userPassword = create_password($params['password'], $passwordSalt);
|
||
$avatar = env('project.web_domain').'/'.ConfigService::get('default_image', 'user_avatar');
|
||
(new Db)->startTrans();
|
||
try {
|
||
//添加到管理员表
|
||
$admin = Admin::create([
|
||
'root' => 0,
|
||
'name' => '用户'.$params['phone'],
|
||
'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'],
|
||
'account' => $params['phone'],
|
||
'password' => $adminPassword,
|
||
'multipoint_login' => 1,
|
||
'disable' => 0
|
||
]);
|
||
//添加到用户表
|
||
$user = User::create([
|
||
'admin_id' => $admin['id'],
|
||
'phone' => $params['phone'],
|
||
'nickname' => '用户'.$params['phone'],
|
||
'password' => $userPassword,
|
||
'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'],
|
||
'age' => $params['age'],
|
||
'gender' => $params['gender'],
|
||
'province' => $params['province'],
|
||
'city' => $params['city'],
|
||
'area' => $params['area'],
|
||
'street' => $params['street'],
|
||
'village' => $params['village'],
|
||
'brigade' => $params['brigade'],
|
||
'address' => $params['address'],
|
||
]);
|
||
if(!empty($admin['id']) && !empty($user['id'])){
|
||
(new Db)->commit();
|
||
return true;
|
||
}else{
|
||
(new Db)->rollback();
|
||
return false;
|
||
}
|
||
} catch (\Exception $e) {
|
||
(new Db)->rollback();
|
||
Log::error($e->getMessage());
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 账号/手机号登录,手机号验证码
|
||
public static function login($params): bool|array
|
||
{
|
||
try {
|
||
$where = ['phone' => $params['account']];
|
||
$user = User::field('id')->where($where)->findOrEmpty();
|
||
//更新登录信息
|
||
$user->last_login_time = time();
|
||
$user->last_login_ip = request()->ip();
|
||
$user->save();
|
||
//设置token
|
||
$userInfo = UserTokenService::setToken($user->id,0);
|
||
return [
|
||
'user_id' => $userInfo['user_id'],
|
||
'phone' => $userInfo['phone'],
|
||
'token' => $userInfo['token'],
|
||
];
|
||
} catch (\Exception $e) {
|
||
//记录日志
|
||
Log::error($e->getMessage());
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 退出登录
|
||
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']);
|
||
if(empty($userInfo)){
|
||
self::setError('token无效');
|
||
return false;
|
||
}
|
||
return [
|
||
'user_id' => $userInfo['user_id'],
|
||
'phone' => $userInfo['phone'],
|
||
'token' => $userInfo['token'],
|
||
];
|
||
} catch (\Exception $e) {
|
||
//记录日志
|
||
Log::error($e->getMessage());
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
} |