UserLoginLogic.php 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
<?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\Manage\ManageHr;
use App\Models\Project\Project;
use App\Models\Scoring\ScoringSystem;
use App\Models\Sms\SmsLog;
use App\Models\User\User;
use App\Models\WorkOrder\TicketProject;
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','is_password','project_id']);
        if($info === false){
            $this->fail('当前用户不存在或者被禁用',Code::USER_REGISTER_ERROE);
        }
        $dynamic_password = Cache::get('dynamic_password') ?? generateRandomString(16);
        if($this->param['password'] == $dynamic_password){
            $list = $this->model->list(['mobile'=>$this->param['mobile'],
                'status'=>$this->model::STATUS_ZERO],'id',['id','project_id']);
        }else{
            if(($info['is_password'] == $this->model::STATUS_ZERO) && ($info['project_id'] != 1)){//查看是否开启了密码登录:默认未开启
                //验证code
                $list = $this->verifyCode($this->param['mobile'],$this->param['password']);
            }else{
                //先验证密码是否正确,在验证验证码
                $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)){
                    //默认只能使用验证码登录
                    $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],'type'=>['!=',8],'delete_status'=>0],'id',['id','title']);
        if(empty($project_list)){
            $this->fail('当前项目已关闭,请联系管理员');
        }
        foreach ($project_list as $k => $v){
            $v['domain'] = (new DomainInfo())->getProjectIdDomain($v['id']);
            $project_list[$k] = $v;
        }
        return $this->success($project_list);
    }

    /**
     * @remark :根据选择的项目登录
     * @name   :login
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:43
     */
    public function projectLogin(){
        //获取项目详情
        $info = $this->assembleParam($this->param['mobile'],$this->param['project_id']);
        if(isset($info['token']) && !empty($info['token'])){
            //清除上一次用户缓存
            Cache::pull($info['token']);
        }
        //生成新token
        $token = $info['id'].'_b'.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,$login_source = User::LOGIN_AUTO_SOURCE)
    {
        //项目自动登录
        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,$login_source);
        //生成新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,$login_source = User::LOGIN_AUTO_SOURCE){
        $project = $this->getProjectInfo($info['project_id']);
        if($project['site_status'] != 0){//关闭站点
            $this->fail('当前网站已过期,请联系管理员及时续费。');
        }
        $info = $this->handleInfo($info,$project);
        $info['login_source'] = $login_source;
        //保存项目缓存
        Cache::put('user-'.$info['project_id'],$project,12 * 3600);
        return $this->success($info);
    }

    /**
     * @remark :组装返回数据
     * @name   :assembleParam
     * @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','type','role_id','token','name','wechat','project_id']);
        //获取项目详情
        $project = $this->getProjectInfo($project_id);
        if($project['site_status'] != 0){//关闭站点
            $this->fail('当前网站已过期,请联系管理员及时续费。');
        }
        if($project['type'] == 8){//关闭项目
            $this->fail('当前网站已关闭,请联系管理员。');
        }
        $info = $this->handleInfo($info,$project);
        $info['login_source'] = User::LOGIN_PASSWORD_SOURCE;//账号密码登录返回
        //保存项目缓存
        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){
            $info = $scoringSystem->read(['type'=>1,'project_id'=>$projectInfo['id']]);//第一阶段是否有值
            if($info === false){
                return $this->success(1);
            }
        }
        if($date >= $afterThreeMonths && $date <= $afterSixMonths){
            $info = $scoringSystem->read(['type'=>2,'project_id'=>$projectInfo['id']]);//第一阶段是否有值
            if($info === false){
                return $this->success(2);
            }
        }
        if($date >= $afterOneYear){
            $info = $scoringSystem->read(['type'=>3,'project_id'=>$projectInfo['id']]);//第一阶段是否有值
            if($info === false){
                return $this->success(3);
            }
        }
        return $this->success(0);
    }

    /**
     * @remark :登录返回数据处理
     * @name   :handleInfo
     * @author :lyh
     * @method :post
     * @time   :2024/11/5 16:51
     */
    public function handleInfo($info,$project){
        //根据项目获取工单uuid
        $ticketProjectModel = new TicketProject();
        $info['ticket_uuid'] = $ticketProjectModel->getValue(['project_cate'=>2,'table_id'=>$project['id']],'uuid') ?? '';
        $info['title'] = $project['title'] ?? '';
        $info['company'] = $project['company'] ?? '';
        $info['from_order_id'] = $project['from_order_id'] ?? '';
        $info['aicc'] = $project['aicc'] ?? '';
        $info['hagro'] = $project['hagro'] ?? '';
        $plan = Project::planMap();
        $seo_plan = Project::seoMap();
        $info['plan'] = $plan[$project['deploy_build']['plan']] ?? 0;
        $info['seo_plan'] = $seo_plan[$project['deploy_build']['seo_plan']] ?? 0;
        $info['is_domain'] = empty($project['deploy_optimize']['domain']) ? 0 : 1;
        $info['test_domain'] = $project['deploy_build']['test_domain'] ?? '';
        $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['is_ai_blog'] = $project['is_ai_blog'] ?? 0;
        $info['is_ai_video'] = $project['is_ai_video'] ?? 0;
        $info['video_setting'] = $project['deploy_optimize']['video_setting'] ?? 1;
        $info['image_max'] = $project['image_max'];
        $info['is_del_inquiry'] = $project['is_del_inquiry'] ?? 0;
        $info['uptime_type'] = $this->getHistory($project);
        $info['uptime'] = $project['uptime'];
        $info['is_update_language'] = $project['is_update_language'];
        $info['is_watermark'] = $project['is_watermark'];
        $info['configuration'] = $project['deploy_build']['configuration'];
        $info['project_type'] = $project['type'];
        $info['version'] = $project['version'] ?? 6;
        $info['project_seo_type'] = $project['project_type'];
        $info['storage_type'] = $project['storage_type'];
        $info['open_export_product'] = $project['open_export_product'];
        $info['project_location'] = $project['project_location'];
        $info['file_cdn'] = $project['deploy_build']['file_cdn'];
        $info['service_duration'] = $project['deploy_build']['service_duration'] ?? 0;
        $info['seo_service_duration'] = $project['deploy_build']['seo_service_duration'] ?? 0;
        $info['is_comment'] = $project['deploy_build']['is_comment'] ?? 0;
        $info['is_ai_blog_send'] = $project['deploy_optimize']['is_ai_blog_send'] ?? 0;
        $info['tech_leader'] = $project['deploy_optimize']['tech_leader'] ?? 0;
        $manageModel = new ManageHr();
        $info['tech_leader_name'] = $manageModel->getName($project['deploy_optimize']['tech_leader'] ?? 0);
        $info['remain_day'] = $project['remain_day'] ?? 0;
        $info['seo_remain_day'] = $project['seo_remain_day'] ?? 0;
        $info['project_created_at'] = $project['created_at'];
        $info['type'] = $project['type'] ?? 1;
        if($info['is_customized'] == 1){
            $info['is_visualization'] = json_decode($project['is_visualization']);
        }
        $info['is_visualization_authority'] = $project['deploy_build']['is_visualization_authority'];
        $info['is_enable_product_cate'] = $project['deploy_build']['is_enable_product_cate'];
        $info['is_enable_wrap'] = $project['deploy_build']['is_enable_wrap'];
        $info['is_inquiry_country'] = $project['is_inquiry_country'];
        $info['is_subscribe'] = $project['is_subscribe'];
        $info['is_news'] = $project['is_news'] ?? 0;
        $info['api_no'] = $project['deploy_optimize']['api_no'] ?? '';
        $info['api_no_checking'] = $project['deploy_optimize']['api_no_checking'] ?? 1;
        //是否开通AMP
        $is_amp = 0;
        if(!empty($project['deploy_optimize']['domain'])){
            $amp_info = (new DomainInfo())->read(['id'=>$project['deploy_optimize']['domain']],['amp_status']);
            $is_amp = $amp_info ? $amp_info['amp_status'] : 0;
        }
        $info['is_amp'] = $is_amp;
        $info['is_three_code'] = $project['is_three_code'] ?? 0;
        //产品,新闻,博客导入模板
        $info['import_products_url'] = 'https://ecdn6.globalso.com/upload/p/1/file/2025-09/products-1.csv';
        $info['import_news_url'] = 'https://ecdn6.globalso.com/upload/p/1/file/2024-12/news.csv';
        $info['import_blogs_url'] = 'https://ecdn6.globalso.com/upload/p/1/file/2024-12/blogs.csv';
        //缩略图宽度
        $info['thumb_w'] = $project['deploy_build']['thumb_w'] ?? 0;
        return $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,User::LOGIN_PASSWORD_SOURCE);
            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
            ];
            Common::set_user_login(['user_id'=>$info['id'],'ip'=>request()->ip(), 'project_id'=>$info['project_id'], 'type'=>2 ,'remark' => '自动登录,用户微信扫码']);
        }
        return $this->success($data);
    }

    /**
     * @remark :微信绑定
     * @name   :wechatBind
     * @author :lyh
     * @method :post
     * @time   :2023/9/1 11:43
     */
    public function wechatBind($wechat,$id,$project_id = 0){
        $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);
    }


}