UserLoginLogic.php 15.0 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\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Models\Scoring\ScoringSystem;
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],'delete_status'=>0],'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 * 12);
        //更新用户信息
        $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);
    }


    /**
     * @remark :自动登录
     * @name   :autologin
     * @author :lyh
     * @method :post
     * @time   :2023/9/18 11:00
     */
    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);
        //生成新token
        $token = md5(uniqid().$info['id']);
        //存储缓存
        $info['token'] = $token;
        $info['manager_id'] = $data['manager_id'];//代表自动登录写入日志
        Cache::add($token,$info,3600 * 12);
        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('账号密码错误/验证码错误');
            }
        }else{
            $this->fail('账号密码错误/验证码错误');
        }
        $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']) ? ((new DomainInfo())->getDomain($project['deploy_optimize']['domain'])) : ($project['deploy_build']['test_domain'] ?? ''));
        $info['is_customized'] = $project['is_customized'];
        $info['is_upload_manage'] = $project['is_upload_manage'];
        $info['is_upgrade'] = $project['is_upgrade'];
        $info['is_show_blog'] = $project['is_show_blog'];
        $info['upload_config'] = $project['upload_config'];
        $info['main_lang_id'] = $project['main_lang_id'];
        $info['image_max'] = $project['image_max'];
        $info['uptime_type'] = $this->getHistory($project);
        $info['uptime'] = $project['uptime'];
        $info['storage_type'] = $project['storage_type'];
        $info['project_location'] = $project['project_location'];
        $info['is_update_language'] = $project['is_update_language'];
        $info['configuration'] = $project['deploy_build']['configuration'];
        $info['project_type'] = $project['type'];
        if($info['is_customized'] == 1){
            $info['is_visualization'] = json_decode($project['is_visualization']);
        }
        //保存项目缓存
        Cache::put('user-'.$info['project_id'],$project,12 * 3600);
        return $this->success($info);
    }

    /**
     * @remark :获取问卷调查记录(阶段记录)
     * @name   :getHistory
     * @author :lyh
     * @method :post
     * @time   :2024/1/20 15:03
     */
    public function getHistory($projectInfo){
        if(!$projectInfo){
            return $this->success(0);
        }
        //建站中直接返回
        if($projectInfo['type'] == 1){
            return $this->success(0);
        }
        //上线项目判断当前属于第几阶段
        if(empty($projectInfo['uptime'])){
            return $this->success(0);
        }
        //获取上线时间30天后
        $after30Days = date('Y-m-d H:i:s', strtotime($projectInfo['uptime'] . ' +30 days'));
        $afterThreeMonths = date('Y-m-d H:i:s', strtotime($projectInfo['uptime'] . ' +3 months'));
        $afterSixMonths = date('Y-m-d H:i:s', strtotime($projectInfo['uptime'] . ' +6 months'));
        $afterOneYear = date('Y-m-d H:i:s', strtotime($projectInfo['uptime'] . ' +1 year'));
        //获取当前时间
        $date = date('Y-m-d H:i:s');
        $scoringSystem = new ScoringSystem();
        if($date <= $after30Days){
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export($date, true) . PHP_EOL, FILE_APPEND);
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export($after30Days, true) . PHP_EOL, FILE_APPEND);
            $info = $scoringSystem->read(['type'=>1]);//第一阶段是否有值
            if($info === false){
                return $this->success(1);
            }
        }
        if($date >= $afterThreeMonths && $date <= $afterSixMonths){
            $info = $scoringSystem->read(['type'=>2]);//第一阶段是否有值
            if($info === false){
                return $this->success(2);
            }
        }
        if($date >= $afterOneYear){
            $info = $scoringSystem->read(['type'=>3]);//第一阶段是否有值
            if($info === false){
                return $this->success(3);
            }
        }
        return $this->success(0);
    }

    /**
     * @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['is_domain'] = empty($project['deploy_optimize']['domain']) ? 0 : 1;
        $info['domain'] = (!empty($project['deploy_optimize']['domain']) ?
            ((new DomainInfo())->getDomain($project['deploy_optimize']['domain'])) : ($project['deploy_build']['test_domain'] ?? ''));
        $info['is_customized'] = $project['is_customized'];
        $info['is_upgrade'] = $project['is_upgrade'];
        $info['is_upload_manage'] = $project['is_upload_manage'];
        $info['is_show_blog'] = $project['is_show_blog'];
        $info['upload_config'] = $project['upload_config'];
        $info['main_lang_id'] = $project['main_lang_id'];
        $info['image_max'] = $project['image_max'];
        $info['uptime_type'] = $this->getHistory($project);
        $info['uptime'] = $project['uptime'];
        $info['is_update_language'] = $project['is_update_language'];
        $info['configuration'] = $project['deploy_build']['configuration'];
        $info['project_type'] = $project['type'];
        $info['storage_type'] = $project['storage_type'];
        $info['project_location'] = $project['project_location'];
        if($info['is_customized'] == 1){
            $info['is_visualization'] = json_decode($project['is_visualization']);
        }
        //保存项目缓存
        Cache::put('user-'.$info['project_id'],$project,12 * 3600);
        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()->toArray();
        if($info['extend_type'] != 0){
            $info['type'] = $info['extend_type'];
        }
        if($info['deploy_build']['configuration'] == null){
            $info['deploy_build']['configuration'] = ["is_SEO" => "0", "is_head"=> "0", "is_home"=> "0", "build_status"=> "0"];
        }else{
            if(!isset($info['deploy_build']['configuration']['is_SEO'])){$info['deploy_build']['configuration']['is_SEO'] = "0";}
            if(!isset($info['deploy_build']['configuration']['is_head'])){$info['deploy_build']['configuration']['is_head'] = "0";}
            if(!isset($info['deploy_build']['configuration']['is_home'])){$info['deploy_build']['configuration']['is_home'] = "0";}
            if(!isset($info['deploy_build']['configuration']['build_status'])){$info['deploy_build']['configuration']['build_status'] = "0";}
        }
        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);
    }


}