This commit is contained in:
unknown 2023-09-18 17:51:25 +08:00
parent 152d150774
commit ce3a570a25
5 changed files with 54 additions and 39 deletions

View File

@ -22,7 +22,6 @@ class LoginController extends BaseApiController
public function register(): Json
{
$params = (new RegisterValidate())->post()->goCheck();
$params['appid'] = $this->request->header('appid');
$result = LoginLogic::register($params);
if (true === $result) {
return $this->success('注册成功', [], 1, 1);

View File

@ -21,18 +21,18 @@ class VerifySignMiddleware
return JsonService::fail('缺少请求头参数', [], 0);
}
//获取子系统信息
$system = System::field('system_appid,system_appkey,system_url,system_status')->where('system_appid',$appid)->where('system_url',$url)->findOrEmpty();
$system = System::field('app_id,app_key,url,status')->where('app_id',$appid)->where('url',$url)->findOrEmpty();
if($system->isEmpty()){
return JsonService::fail('应用ID无效', [], 0);
}
if($url != $system['system_url']){
if($url != $system['url']){
return JsonService::fail('请求来源异常', [], 0);
}
if($system['system_status'] != 0){
if($system['status'] != 0){
return JsonService::fail('应用已被禁用或注销', [], 0);
}
//验证签名
$checkSign = ApiSignService::verifySign(['appid'=>$appid,'url'=>$url,'timestamp'=>$timestamp,'sign'=>$sign],$system['system_appkey']);
$checkSign = ApiSignService::verifySign(['appid'=>$appid,'url'=>$url,'timestamp'=>$timestamp,'sign'=>$sign],$system['app_key']);
if($checkSign['code'] == 0){
return JsonService::fail($checkSign['msg'],[],0);
}

View File

@ -4,10 +4,11 @@ namespace app\api\logic;
use app\common\cache\UserTokenCache;
use app\common\logic\BaseLogic;
use app\api\service\UserTokenService;
use app\common\model\systems\System;
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;
/**
@ -20,34 +21,49 @@ class LoginLogic extends BaseLogic
// 账号密码注册
public static function register(array $params): bool
{
//获取子系统信息
$system = System::field('id')->where('system_appid',$params['appid'])->findOrEmpty();
if($system->isEmpty()){
self::setError('应用appid错误');
return false;
}
//创建密码和默认头像
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
$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 {
User::create([
'user_phone' => $params['phone'],
'user_password' => $password,
'user_avatar' => $avatar,
'user_age' => $params['age'],
'user_gender' => $params['gender'],
'user_province' => $params['province'],
'user_city' => $params['city'],
'user_area' => $params['area'],
'user_street' => $params['street'],
'user_village' => $params['village'],
'user_brigade' => $params['brigade'],
'user_address' => $params['address'],
//添加到管理员表
$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;
@ -58,11 +74,11 @@ class LoginLogic extends BaseLogic
public static function login($params): bool|array
{
try {
$where = ['user_phone' => $params['account']];
$where = ['phone' => $params['account']];
$user = User::field('id')->where($where)->findOrEmpty();
//更新登录信息
$user->user_last_login_time = time();
$user->user_last_login_ip = request()->ip();
$user->last_login_time = time();
$user->last_login_ip = request()->ip();
$user->save();
//设置token
$userInfo = UserTokenService::setToken($user->id,0);

View File

@ -69,8 +69,8 @@ class UserTokenCache extends BaseCache
$user = User::where('id', '=', $userSession->user_id)->find();
$userInfo = [
'user_id' => $user->id,
'phone' => $user->user_phone,
'avatar' => $user->user_avatar,
'phone' => $user->phone,
'avatar' => $user->avatar,
'token' => $token,
'terminal' => $userSession->terminal,
'expire_time' => $userSession->expire_time,

View File

@ -55,19 +55,19 @@ class LoginAccountValidate extends BaseValidate
if (!$userAccountSafeCache->isSafe()) {
return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
}
$userInfo = User::where('user_phone',$data['account'])->field('user_password,user_status')->findOrEmpty();
$userInfo = User::where('phone',$data['account'])->field('password,status')->findOrEmpty();
if ($userInfo->isEmpty()) {
return '用户不存在';
}
if ($userInfo['user_status'] != 0) {
if ($userInfo['status'] != 0) {
return '用户已冻结或删除';
}
if (empty($userInfo['user_password'])) {
if (empty($userInfo['password'])) {
$userAccountSafeCache->record();
return '密码不存在';
}
$passwordSalt = Config::get('project.unique_identification');
if ($userInfo['user_password'] !== create_password($password, $passwordSalt)) {
if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
$userAccountSafeCache->record();
return '密码错误';
}
@ -77,11 +77,11 @@ class LoginAccountValidate extends BaseValidate
public function checkCode($code, $rule, $data): bool|string
{
$userInfo = User::where('user_phone',$data['account'])->field('id,user_status')->findOrEmpty();
$userInfo = User::where('phone',$data['account'])->field('id,status')->findOrEmpty();
if ($userInfo->isEmpty()) {
return '用户不存在';
}
if ($userInfo['user_status'] != 0) {
if ($userInfo['status'] != 0) {
return '用户已冻结或删除';
}
$smsDriver = new SmsDriver();