UserLoginLogic.php 5.9 KB
<?php

namespace App\Http\Logic\Bside\User;

use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Helper\Common;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\SmsLog;
use App\Models\User\ProjectRole as ProjectRoleModel;
use App\Models\User\User;
use Illuminate\Support\Facades\Cache;

class UserLoginLogic
{
    const USER_STATUS = 0;

    protected $model;
    protected $param;

    public function __construct()
    {
        //验证账号密码
        $this->param = request()->all();
        $this->model = new User();

    }

    /**
     * @remark :登录接口
     * @name   :login
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:43
     */
    public function login(){
        //验证账号密码是否正确
        $info = $this->verifyAccount();
        //验证角色是否被禁用+获取项目详情
        $info = $this->verifyRole($info);
        if(isset($info['token']) && !empty($info['token'])){
            //清除上一次用户缓存
            Cache::pull($info['token']);
        }
        //生成新token
        $token = md5(uniqid().$info['id']);
        //存储缓存
        $info['token'] = $token;
        Cache::add($token,$info);
        //更新用户信息
        $rs = $this->model->edit(['token'=>$token],['id'=>$info['id']]);
        if($rs === false){
            $this->fail('系统错误,请联系管理员');
        }
        //写入日志
        Common::set_user_login(['user_id'=>$info['id'],'ip'=>request()->ip()]);
        return $this->success($info);
    }

    /**
     * 自动登录
     * @author zbj
     * @date 2023/7/25
     */
    public function autologin()
    {
        $current_url = url()->current();
        $refer_url = url()->previous();
        if ($current_url == $refer_url) {
            Common::set_user_login(['user_id'=>0, 'ip'=>request()->ip(), 'project_id' => $this->param['project_id'] ?: 0,'remark' => ' - -|' . $refer_url]);
            echo ' - -';
            exit;
        }
        if (strpos($refer_url, 'www.quanqiusou.cn') === false && strpos($refer_url, 'crm.globalso.com') === false && strpos($refer_url, 'crm.globalso.com') === false) {
            Common::set_user_login(['user_id'=>0, 'ip'=>request()->ip(), 'project_id' => $this->param['project_id'] ?: 0,'remark' => ' 来源错误|' . $refer_url]);
            echo '来源错误';
            exit;
        }
        if (!$this->param['project_id']) {
            Common::set_user_login(['user_id'=>0, 'ip'=>request()->ip(), 'project_id' => $this->param['project_id'] ?: 0,'remark' => ' 没有传入链接|' . $refer_url]);
            echo '没有连接';
            exit;
        }
        $has_user = User::where('project_id', $this->param['project_id'])->orderBy('id', 'asc')->first();
        if (empty($has_user->id)) {
            Common::set_user_login(['user_id'=>0, 'ip'=>request()->ip(), 'project_id' => $this->param['project_id'] ?: 0,'remark' => ' 该项目未找到注册账号|' . $refer_url]);
            echo '该项目未找到注册账号';
            exit;
        }

        return redirect('admin');
    }

    /**
     * @name   :(验证账号、密码或验证码是否正确)verifyAccount
     * @author :lyh
     * @method :post
     * @time   :2023/6/12 15:31
     */
    public function verifyAccount(){
        //密码加密
        $password = base64_encode(md5($this->param['password']));
        $info = $this->model->read(['mobile'=>$this->param['mobile'],'password'=>$password,'status'=>$this::USER_STATUS],
            ['id','mobile','role_id','token','name','project_id']);
        if($info === false){
            //账号密码没通过时,验证验证码验证验证码是否准备
            $last_sms = SmsLog::getLastLog($this->param['mobile'], SmsLog::TYPE_LOGIN);
            if($this->param['password'] != $last_sms->code){
                $this->fail('账号密码错误/验证码错误',Code::USER_REGISTER_ERROE);
            }
            $info = $this->model->read(['mobile'=>$this->param['mobile']],['id','mobile','status','role_id','token','name','project_id']);
            if($info === false){
                $this->fail('当前用户不存在',Code::USER_REGISTER_ERROE);
            }
            if($info['status'] != self::USER_STATUS){
                $this->fail('当前用户被禁用',Code::USER_REGISTER_ERROE);
            }
        }
        return $this->success($info);
    }

    /**
     * @name   :(验证角色是否禁用)verifyRole
     * @author :lyh
     * @method :post
     * @time   :2023/6/12 15:34
     */
    public function verifyRole($info){
        //当前用户角色是否被禁用
        $projectRoleModel = new ProjectRoleModel();
        $role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>$this::USER_STATUS]);
        if($role_info === false){
            $this->fail('当前用户角色被禁用',Code::USER_REGISTER_ERROE);
        }
        $project = (new ProjectLogic())->getInfo($info['project_id']);
        $info['company'] = $project['company'] ?? '';
        $info['plan'] = $project['deploy_build']['plan'][0] ?? '';
        $info['domain'] = !empty($project['deploy_optimize']['domain']) ?
            $project['deploy_optimize']['domain'] : ($project['deploy_build']['test_domain'] ?? '');
        return $this->success($info);
    }


    /**
     * @notes: 请简要描述方法功能
     * @param array $data
     * @return array
     */
    public function success($data = [])
    {
        return $data;
    }
    /**
     * @notes: 错误抛出
     * @param string $code
     * @param string $message
     * @throws AsideGlobalException|BsideGlobalException
     */
    public function fail(string $message = "", string $code = Code::SYSTEM_ERROR)
    {
        if((request()->path()[0]) == \App\Enums\Common\Common::B){
            throw new BsideGlobalException($code, $message);
        }
        throw new AsideGlobalException($code, $message);
    }
}