UserLoginLogic.php 10.3 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\Models\Project\Project;
use App\Models\Sms\SmsLog;
use App\Models\User\ProjectRole as ProjectRoleModel;
use App\Models\User\User;
use App\Models\User\User as UserModel;
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   :verifyAccountPassword
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 14:50
     */
    public function login(){
        //先验证手机号是否在项目中存在
        $info = $this->model->read(['mobile'=>$this->param['mobile'],'status'=>$this->model::STATUS_ZERO],['id']);
        if($info === false){
            $this->fail('当前用户不存在或者被禁用',Code::USER_REGISTER_ERROE);
        }
        //查看当前账号下有几个项目
        $password = base64_encode(md5($this->param['password']));
        $list = $this->model->list(['mobile'=>$this->param['mobile'],
            'password'=>$password,'status'=>$this->model::STATUS_ZERO],'id',['id','project_id']);
        if(empty($list)){
            //验证code
            $list = $this->verifyCode($this->param['mobile'],$this->param['password']);
        }
        //获取所有项目的项目id
        foreach ($list as $v){
            $projectArr[] = $v['project_id'];
        }
        $projectModel = new Project();
        $project_list = $projectModel->list(['id'=>['in',$projectArr]],'id',['id','title']);
        //登录选择项目的有效时间
        Cache::add('login-project-'.$this->param['mobile'],1,300);
        return $this->success($project_list);
    }

    /**
     * @remark :根据选择的项目登录
     * @name   :login
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:43
     */
    public function projectLogin(){
        if(!Cache::get('login-project-'.$this->param['mobile'])){
            $this->fail('当前用户选择项目有限时间已过期');
        }
        //获取项目详情
        $info = $this->assembleParam($this->param['mobile'],$this->param['project_id']);
        if(isset($info['token']) && !empty($info['token'])){
            //清除上一次用户缓存
            Cache::pull($info['token']);
            Cache::pull('login-project-'.$this->param['mobile']);
        }
        //生成新token
        $token = md5(uniqid().$info['id']);
        //存储缓存
        $info['token'] = $token;
        Cache::add($token,$info,3600);
        //更新用户信息
        $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(),'project_id'=>$info['project_id']]);
        return $this->success($info);
    }


    /**
     * 自动登录
     * @author zbj
     * @date 2023/7/25
     */
    public function autologin($data)
    {
        //项目自动登录
        if(isset($data['project_id']) && !empty($data['project_id'])){
            $has_user = $this->model->read(['project_id'=>$data['project_id'],'role_id'=>$this->model::ROLE_MANAGER]);
        }
        //根据特定用户自动登录
        if(isset($data['user_id']) && !empty($data['user_id'])){
            $has_user = $this->model->read(['id'=>$data['user_id'],'status'=>$this->model::STATUS_ZERO]);
        }
        if ($has_user === false) {
            $this->fail('该项目未找到注册账号');
        }
        $info = $this->autoAssembleParam($has_user);
        if(!Cache::get($info['token'])){
            //生成新token
            $token = md5(uniqid().$info['id']);
            //存储缓存
            $info['token'] = $token;
            $info['manager_id'] = $data['manager_id'];//代表自动登录写入日志
            Cache::add($token,$info,3600);
        }
        Common::set_user_login(['user_id'=>$info['id'],'ip'=>request()->ip(),
            'project_id'=>$info['project_id'], 'type'=>1 ,'remark' => '自动登录,操作管理员为:'.$data['manager_id']]);
        return $info;
    }

    /**
     * @remark :验证验证码是否正确或是否过期
     * @name   :verifyCode
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 17:17
     */
    public function verifyCode($mobile,$password){
        //账号密码没通过时,验证验证码
        $smsModel = new SmsLog();
        $smsInfo = $smsModel->formatQuery(['mobile'=>$mobile,'type'=>$smsModel::TYPE_LOGIN])->orderBy('id','desc')->first();
        if(!empty($smsInfo)){
            if(($password != $smsInfo['code']) || ($smsInfo['created_at']  < date('Y-m-d H:i:s',time() - 300))){
                $this->fail('账号密码错误/验证码错误',Code::USER_REGISTER_ERROE);
            }
        }else{
            $this->fail('账号密码错误/验证码错误',Code::USER_REGISTER_ERROE);
        }
        $list = $this->model->list(['mobile'=>$this->param['mobile'],'status'=>$this->model::STATUS_ZERO],['id','project_id']);
        return $this->success($list);
    }

    /**
     * @name   :自动登录组装返回数据
     * @author :lyh
     * @method :post
     * @time   :2023/6/12 15:34
     */
    public function autoAssembleParam($info){
        $project = $this->getProjectInfo($info['project_id']);
        $info['title'] = $project['title'] ?? '';
        $info['company'] = $project['company'] ?? '';
        $info['from_order_id'] = $project['from_order_id'] ?? '';
        $info['aicc'] = $project['aicc'] ?? '';
        $info['hagro'] = $project['hagro'] ?? '';
        $info['plan'] = Project::planMap()[$project['deploy_build']['plan']];
        $info['domain'] = (!empty($project['deploy_optimize']['domain']) ?
            $project['deploy_optimize']['domain'] : ($project['deploy_build']['test_domain'] ?? ''));
        //保存项目缓存
        Cache::put('user-'.$info['project_id'],$project,$minutes = null);
        return $this->success($info);
    }

    /**
     * @remark :组装返回数据
     * @name   :getProjectInfo
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 15:26
     */
    public function assembleParam($mobile,$project_id){
        $info = $this->model->read(['mobile'=>$mobile,'project_id'=>$project_id],['id','mobile','status','role_id','token','name','wechat','project_id']);
        //获取项目详情
        $project = $this->getProjectInfo($project_id);
        $info['title'] = $project['title'] ?? '';
        $info['company'] = $project['company'] ?? '';
        $info['from_order_id'] = $project['from_order_id'] ?? '';
        $info['aicc'] = $project['aicc'] ?? '';
        $info['hagro'] = $project['hagro'] ?? '';
        $info['plan'] = Project::planMap()[$project['deploy_build']['plan']];
        $info['domain'] = (!empty($project['deploy_optimize']['domain']) ?
            $project['deploy_optimize']['domain'] : ($project['deploy_build']['test_domain'] ?? ''));
        //保存项目缓存
        Cache::put('user-'.$info['project_id'],$project,$minutes = null);
        return $this->success($info);
    }


    /**
     * @remark :获取项目数据详情
     * @name   :getProjectInfo
     * @author :lyh
     * @method :post
     * @time   :2023/8/17 10:20
     */
    public function getProjectInfo($id){
        $projectModel = new Project();
        $info = $projectModel->with('payment')->with('deploy_build')
            ->with('deploy_optimize')->with('online_check')->where(['id'=>$id])->first();
        if($info['extend_type'] != 0){
            $info['type'] = $info['extend_type'];
        }
        return $this->success($info);
    }

    /**
     * @remark :扫码登录
     * @name   :wechatLogin
     * @author :lyh
     * @method :post
     * @time   :2023/9/1 11:29
     */
    public function wechatLogin($wechat){
        $info = $this->model->read(['wechat'=>$wechat]);
        if($info === false){
            $data = [
                'code'=>0,
                'message'=>'当前用户未绑定账户,请绑定后登录',
            ];
        }else {
            //获取项目详情
            $info = $this->autoAssembleParam($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,3600);
            //更新用户信息
            $this->model->edit(['token'=>$token],['id'=>$info['id']]);
            $data = [
                'code'=>1,
                'message'=>'登陆成功',
                'data'=>$info
            ];
        }
        return $this->success($data);
    }

    /**
     * @remark :微信绑定
     * @name   :wechatBind
     * @author :lyh
     * @method :post
     * @time   :2023/9/1 11:43
     */
    public function wechatBind($wechat,$id){
        $info = $this->model->read(['wechat'=>$wechat]);
        if($info !== false){
            $data = [
                'code'=>0,
                'message'=>'当前微信已绑定其他微信,请解绑后再操作',
            ];
        }else{
            $this->model->edit(['wechat'=>$wechat],['id'=>(int)$id]);
            $data = [
                'code'=>2,
                'message'=>'绑定成功',
            ];
        }
        return $this->success($data);
    }

    /**
     * @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);
    }
}