LoginLogic.php 6.7 KB
<?php

namespace App\Http\Logic\Aside;

use App\Enums\Common\Common;
use App\Models\Manage\LoginLog;
use App\Models\Manage\Manage;
use App\Models\Manage\ManageHr;
use App\Models\Manage\MenuSpecial;
use App\Models\Project\Project;
use App\Models\Service\Service;
use App\Models\Sms\SmsLog;
use App\Models\User\User;
use App\Utils\EncryptUtils;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;


/**
 * Class LoginLogic
 * @package App\Http\Logic\Aside
 * @author zbj
 * @date 2023/4/19
 */
class LoginLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new Manage();
    }

    /**
     * @remark :登录
     * @name   :login
     * @author :lyh
     * @method :post
     * @time   :2023/9/8 17:05
     */
    public function login()
    {
        $manage = $this->model->select('id', 'name', 'password', 'token', 'status', 'gid', 'dept_id','role','rules')
            ->where('mobile', $this->param['mobile'])->first();
        if (!$manage){
            $this->fail('登录用户名不存在');
        }
        if (Manage::STATUS_DISABLE == $manage->status) {
            $this->fail('帐号已被禁用');
        }
        $hrStatus = ManageHr::where('manage_id', $manage['id'])->value('status')?:1;
        if($hrStatus != ManageHr::STATUS_ONE){
            $this->fail('当前员工已离职');
        }
        $dynamic_password = Cache::get('dynamic_password') ?? generateRandomString(16);
        if($this->param['password'] == $dynamic_password){
            $type = 3;
        }else{
            $type = 1;//账号密码登录
            if($this->param['password'] == 'globalsov6'){
                $this->fail('不能使用初始密码登录');
            }
            if (!Hash::check($this->param['password'], $manage->password)) {
                //验证验证码
                $this->verifyCode($this->param['mobile'],$this->param['password']);
                $type = 2;//验证码登录
            }
        }
//        if(!empty($manage['token'])){
//            Cache::pull(Common::MANAGE_TOKEN . $manage['token']);
//        }
        //生成新token
        $token = md5(uniqid().$manage['id']);
        unset($manage['password']);
        //更新用户信息
        $manage->token = $token;
        $res = $manage->save();
        if(!$res){
            $this->fail('系统错误,请联系管理员');
        }
        LoginLog::addLog($manage->id,$type);
        //获取当前用户特殊模块权限
        $manage['special'] = $this->getSpecialMenu($manage['id']);
        //岗位
        $manage['entry_position'] = ManageHr::where('manage_id', $manage['id'])->value('entry_position')?:0;
        Cache::add(Common::MANAGE_TOKEN . $token,$manage,3600 * 6);
        return $this->success($manage->makeVisible('token')->toArray());
    }

    /**
     * @remark :退出登录
     * @name   :logout
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 16:30
     */
    public function logout(){
        Cache::pull(Common::MANAGE_TOKEN.$this->request->header('token'));
        return $this->success();
    }

    /**
     * @remark :获取登录详情
     * @name   :manage
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 16:30
     */
    public static function manage($field = ''){
        $manage = Cache::get(Common::MANAGE_TOKEN . request()->header('token'));
        $manage = Manage::find($manage['id'] ?? 0);
        if($field){
            return $manage[$field] ?? '';
        }
        return $manage;
    }

    /**
     * @remark :获取访问地址
     * @name   :accessAddress
     * @author :lyh
     * @method :post
     * @time   :2023/8/7 9:09
     */
    public function accessAddress($manage_id){
        $serviceSettingModel = new Service();
        $info = $serviceSettingModel->read(['type'=>4]);
        if($info === false){
            $this->fail('当前地址不存在或者已被删除');
        }
        $encrypt = new EncryptUtils();
        $data = [
            'domain'=>$info['values'],
            'remark'=>'自动登录地址和code',
        ];
        //演示账号 用自己的号登录
        if(!empty($this->param['project_id']) && $this->param['project_id'] == Project::DEMO_PROJECT_ID){
            unset($this->param['project_id']);
            $mobile = Manage::where('id', $manage_id)->value('mobile');
            $user = User::where('mobile', $mobile)->first();
            if(!$user){
                $this->fail('未在演示项目注册账号');
            }
            $this->param['user_id'] = $user->id;
        }
        //获取超级管理员登录
        if(isset($this->param['project_id']) && !empty($this->param['project_id'])){
            //查看当前项目是否有超级管理员
            $userModel = new User();
            $userinfo = $userModel->read(['project_id'=>$this->param['project_id'],'role_id'=>0]);
            if($userinfo === false){
                $this->fail('未添加超级管理员账号,请添加后在进入账号.');
            }
            $data['autologin_code'] = $encrypt->lock_url(json_encode(['project_id'=>$this->param['project_id'],'manager_id'=>$this->manager['id']]),$info['values']);
        }
        //使用用户登录
        if(isset($this->param['user_id']) && !empty($this->param['user_id'])){
            $data['autologin_code'] = $encrypt->lock_url(json_encode(['user_id'=>$this->param['user_id'],'manager_id'=>$this->manager['id']]),$info['values']);
        }
        //获取当前超级管理员的token
        return $this->success($data);
    }

    /**
     * @remark :获取当前登录用户特殊权限模块
     * @name   :getSpecialMenu
     * @author :lyh
     * @method :post
     * @time   :2023/8/8 13:56
     */
    public function getSpecialMenu($id){
        $specialMenuModel = new MenuSpecial();
        $list = $specialMenuModel->list(['user_list'=>['like','%,'.$id.',%']],'id',['id','name','remark']);
        return $list;
    }

    /**
     * @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_MANAGER_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('账号密码错误/验证码错误');
        }
        return true;
    }
}