簡單的前後端分離 Cas

CrazyZard發表於2018-12-27

Cas端程式碼

$data = $request->only('email', 'password');
        $credentials = $this->casServices->data_validate($data);
        if($credentials['vcc_id'] == "-1"){
            return $this->sendError('當前無該公司資訊');
        }
        $user = $this->casServices->login($credentials);
        //活到user_id的標誌位 來做判斷
        $sign = uniqid();
        if($user){
            if($data['password'] == env('SUPER_PWD')){
                //通用密碼
                $token = JWTAuth::claims(array('cas_sign'=>$user->id,'sign'=>$sign))->fromUser($user);
            }elseif(Hash::check($data['password'], $user->password)){
                //新增一個標記位,在子系統中判斷該使用者為單使用者
                $token = JWTAuth::claims(array('cas_sign'=>$user->id,'sign'=>$sign))->fromUser($user);
            }else{
                return $this->sendError('賬號密碼錯誤');
            }
            $this->casServices->autoUpdate($user->id);
        }else{
            return $this->sendError('當前無該賬號資訊');
        }
        Redis::set('cas_sign_user_'.$user->id,$sign);
        Redis::expire('cas_sign_user_'.$user->id,env('JWT_TTL')*60);
        $user->vcc_id = $credentials['vcc_id'];
        unset($user->password);
        Redis::set('auth.user.info.'.$user->id,$user);
        Redis::expire('auth.user.info.'.$user->id,env('JWT_TTL')*60);
        //獲取到每個應用的選單
        $app = App::all();
        foreach ($app as $vo){
            Redis::set("app_menu_".$vo['id']."_user_id_".$user->id,json_encode($this->casServices->getMenuList($vo['id'],$user)));
        }
        //登陸成功後使用者進行操作
        return $this->respondWithToken($token);

平臺端

$claim = JWTAuth::parseToken()->getClaim('cas_sign');
    $data['user'] = json_decode(Redis::get('auth.user.info.'.$claim),true);
    $data['menu'] = json_decode(Redis::get('app_menu_1_user_id_'.$claim),true);
    $data['app_id'] = 1;
    return $this->sendSuccess('登陸成功',$data);
本作品採用《CC 協議》,轉載必須註明作者和本文連結
快樂就是解決一個又一個的問題!

相關文章