LoginLogic.php 4.8 KB
<?php

namespace App\Http\Logic\Aside;

use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Models\Manage\LoginLog;
use App\Models\Manage\Manage;
use App\Models\Manage\MenuSpecial;
use App\Models\Service\Service;
use App\Models\Sms\SmsLog;
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();
    }


    public function login()
    {
        $manage = $this->model->select('id', 'name', 'password', 'token', 'status', 'gid', 'dept_id')
            ->where('mobile', $this->param['mobile'])->first();
        if (!$manage){
            $this->fail('登录用户名不存在');
        }
        if (Manage::STATUS_DISABLE == $manage->status) {
            $this->fail('帐号已被禁用');
        }
        $type = 1;//账号密码登录
        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']);
        //存储缓存
        $manage['token'] = $token;
        Cache::add(Common::MANAGE_TOKEN . $token,$manage,3600);
        //更新用户信息
        $manage->token = $token;
        $res = $manage->save();
        if(!$res){
            $this->fail('系统错误,请联系管理员');
        }
        LoginLog::addLog($manage->id,$type);
        //获取当前用户特殊模块权限
        $manage['special'] = $this->getSpecialMenu($manage['id']);
        return $this->success($manage->makeVisible('token')->toArray());
    }

    public function logout(){
        Cache::pull(request()->header('token'));
        return $this->success();
    }

    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(){
        $serviceSettingModel = new Service();
        $info = $serviceSettingModel->read(['type'=>4]);
        if($info === false){
            $this->fail('当前地址不存在或者已被删除');
        }
        $encrypt = new EncryptUtils();
        $data = [
            'domain'=>$info['values'],
            'remark'=>'自动登录地址和code',
        ];
        //获取超级管理员登录
        if(isset($this->param['project_id']) && !empty($this->param['project_id'])){
            $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('账号密码错误/验证码错误',Code::USER_LOGIN_ERROE);
            }
        }else{
            $this->fail('账号密码错误/验证码错误',Code::USER_LOGIN_ERROE);
        }
        return true;
    }
}