提交 93a33694 authored 作者: chengye's avatar chengye

电费单点登录部署

上级 2b275514
...@@ -3,7 +3,6 @@ namespace app\admin\controller; ...@@ -3,7 +3,6 @@ namespace app\admin\controller;
use app\admin\model\PermissionModel; use app\admin\model\PermissionModel;
use app\admin\model\StaffRoleModel; use app\admin\model\StaffRoleModel;
use think\Controller; use think\Controller;
use think\facade\Session;
use think\facade\Request; use think\facade\Request;
/** /**
...@@ -23,14 +22,24 @@ class Base extends Controller ...@@ -23,14 +22,24 @@ class Base extends Controller
private function check(){ private function check(){
$user = Session::get('user'); $single = new SingleSign();
try {
$user = $single->getUserInfo();
Cookie::set('user',$user['ukey']);
$user['user_id'] = $single->getUserId($user);
session('user', $user);
$sUser['name'] = $user['name'];
$sUser['is_admin'] = $user['is_admin'] === true ? true : false;
$sUser['user_id'] = $user['user_id'];
if(!$user){ //保存加密串到redis
$single->saveKey($user['ukey'],$sUser);
} catch ( \Exception $e){
// dd($e->getMessage().$e->getFile().$e->getLine());
$this->redirect('/login'); $this->redirect('/login');
} }
if(!$user['is_admin']){ if(!$user['is_admin']){
$this->authCheck($user); $this->authCheck($user['user_id']);
} }
} }
......
...@@ -20,7 +20,8 @@ class Login extends Controller ...@@ -20,7 +20,8 @@ class Login extends Controller
{ {
public function index() public function index()
{ {
return $this->fetch('index'); header("Location:http://oa-tmp.gonn.tech/?m=login");
//return $this->fetch('index');
} }
public function login(Request $request,LoginValidate $validate) public function login(Request $request,LoginValidate $validate)
......
<?php
/**
* Created by PhpStorm.
* User: chouchou
* Date: 2020-3-22
* Time: 19:03
*/
namespace app\admin\controller;
use app\admin\model\StaffModel;
use think\Exception;
use think\facade\Cookie;
class SingleSign {
public $instence = null;
public function __construct($ip='192.168.1.70',$port=6379){
if($this->instence === null){
$redis = new \Redis();
$res = $redis->connect($ip,$port);
if($res) $this->instence = $redis;
else throw new Exception("redis connect faild");
}
}
//获取session
public function getSid($prex = 'PHPREDIS_SESSION:'){
$sid = Cookie::get('PHPSESSID');
$sid = $prex.$sid;
$str = $this->instence->get($sid);
if(!$str) throw new Exception("session info not found");
return $str;
}
//从session中读取信息
public function getUserInfo(){
$str = $this->getSid();
$pos = stripos($str,'is_admin|b:') + 11;
$isAdmin = substr($str,$pos,1);
$user['is_admin'] = $isAdmin;
if($user['is_admin'] == true) {
$user['username'] = 'gonnadmin';
} else {
preg_match('/adminuser\|s\:(\d+)\:/',$str,$match);
if($match){
$pos = stripos($str,'adminuser|s:') + strlen('adminuser|s:');
$pos += strlen($match[1]) + 2;
$user['username'] = substr($str,$pos,$match[1]+0);
}
}
$pos = stripos($str,'ukey|s:10:') + 11;
$ukey = substr($str,$pos,10);
$user['ukey'] = $ukey;
preg_match('/[\x80-\xff]+/',$str,$matchb);
if($matchb) $user['name'] = $matchb[0];
return $user;
}
//获取userid
public function getUserId($user=[]){
if(!$user['is_admin']){
$email = $user['username'].'@gonn.com.cn';
$staff = StaffModel::where(['email'=>$email])->find();
if($staff) return $staff->id;
else throw new Exception('user id not found');
} else {
return 8;
}
}
//将加密串保存到redis
public function saveKey($key,$user=[]){
$encryptKey = substr(md5(config('admin.encrypt_key').$key),0,10);
//存储到redis
if(!$this->instence->get($encryptKey)){
$this->instence->set($encryptKey,json_encode($user),3600);
}
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论