diff --git a/application/admin/view/login/index.html b/application/admin/view/login/index.html index 79b5b55..bcb4645 100644 --- a/application/admin/view/login/index.html +++ b/application/admin/view/login/index.html @@ -6,9 +6,10 @@ - + + diff --git a/application/service/controller/Base.php b/application/service/controller/Base.php index 7c20252..e51ba9b 100644 --- a/application/service/controller/Base.php +++ b/application/service/controller/Base.php @@ -6,10 +6,16 @@ namespace app\service\controller; +use PHPGangsta\GoogleAuthenticator; use think\Controller; class Base extends Controller { + /** + * google 二次验证码长度 + * @var int + */ + protected $googleAuthSecretLength = 64; public function _initialize() { @@ -149,5 +155,69 @@ class Base extends Controller { return $table.'_'.($uid % config('chat_table_num')); } + + // 创建二次验证秘钥 + public function make_google_auth_secret () + { + $ga = new GoogleAuthenticator(); + $key = $ga->createSecret($this->googleAuthSecretLength); + $content = $ga->getQrContent($this->request->host(),$key,session('l_user_name').'['.date('Y-m-d H:i:s').']'); + if (isset($key)){ + return json(['code' => 1, 'key' => $key, 'qrcode_url' => $content, 'msg' => '获取成功']); + } + + return json(['code' => 0, 'key' => null, 'qrcode_url' => null, 'msg' => '获取失败']); + } + + // 谷歌验证 + public function bind_google_auth () + { + if (request()->isPost()) { + $param = input('post.'); + + if (empty($param['new_google_auth'])) { + return json(['code' => -2, 'data' => '', 'msg' => '请输入验证码']); + } + + if (empty($param['key'])) { + return json(['code' => -2, 'data' => '', 'msg' => '请重试']); + } + $old = isset($param['old_google_auth']) ? $param['old_google_auth'] : null; + $code = isset($param['new_google_auth']) ? $param['new_google_auth'] : null; + $secret = $param['key']; + + $google_secret = null; + $admin_id = session('l_user_id'); + if ($admin_id) { + $google_secret = db('users')->where(['id' => session('l_user_id')])->value('google_secret'); + } + if ($google_secret && strlen($google_secret) == $this->googleAuthSecretLength) { + if (empty($param['old_google_auth'])) { + return json(['code' => -2, 'data' => '', 'msg' => '请输入旧验证码']); + } + //先验证老的 + $ga = new GoogleAuthenticator(); + if(!$ga->verifyCode($google_secret, strval($old))){ + return json(['code' => -2, 'data' => '', 'msg' => '旧验证码验证失败']); + } + if (!$ga->verifyCode($secret,$code)){ + return json(['code' => -2, 'data' => '', 'msg' => '验证码验证失败']); + } + //验证新的 + if (db('users')->where(['id' => session('l_user_id')])->update(['google_secret' => $secret])){ + return json(['code' => 1, 'data' => '', 'msg' => '绑定成功']); + } + } else { + $ga = new GoogleAuthenticator(); + if (!$ga->verifyCode($secret,$code)){ + return json(['code' => -2, 'data' => '', 'msg' => '验证码验证失败']); + } + //验证新的 + if (db('users')->where(['id' => session('l_user_id')])->update(['google_secret' => $secret])){ + return json(['code' => 1, 'data' => '', 'msg' => '绑定成功']); + } + } + } + } } \ No newline at end of file diff --git a/application/service/controller/Index.php b/application/service/controller/Index.php index bdc78c9..227cdb1 100644 --- a/application/service/controller/Index.php +++ b/application/service/controller/Index.php @@ -98,6 +98,8 @@ class Index extends Base $payment_information = db('payment')->alias('a')->join('payment_type w', 'a.payment_type = w.id')->where(['is_use' => 1, 'a.status' => 1, 'kf_id' => session('l_user_id')])->select(); $payment_img = db('payment')->alias('a')->join('payment_type w', 'a.payment_type = w.id')->where(['is_use' => 1, 'a.status' => 1, 'kf_id' => session('l_user_id')])->find(); // dump($payment_information);die; + $google_secret = $userInfo['google_secret']; + unset($userInfo['google_secret']); $this->assign([ 'word' => db('words')->where('kf_id', null)->whereOr('kf_id', session('l_user_id'))->select(), 'uinfo' => $userInfo, @@ -106,7 +108,10 @@ class Index extends Base 'token' => session('kf_token'), 'type' => $type, 'payment_information' => $payment_information, - 'payment_img' => $payment_img + 'payment_img' => $payment_img, + 'ga_android' => config('ga_android'), + 'ga_ios' => config('ga_ios'), + 'google_secret' => $google_secret ? 1 : 0, ]); return $this->fetch(); } diff --git a/application/service/controller/Login.php b/application/service/controller/Login.php index f7ecba5..1f93f85 100644 --- a/application/service/controller/Login.php +++ b/application/service/controller/Login.php @@ -4,10 +4,23 @@ * 登录控制器 */ namespace app\service\controller; +use PHPGangsta\GoogleAuthenticator; +use Repository\LogRepository; use think\Controller; class Login extends Controller { + /** + * google 二次验证码长度 + * @var int + */ + protected $googleAuthSecretLength = 64; + /** + * google 二次验证码超时时间 + * @var int + */ + protected $googleAuthTimeout = 300; + public function index() { $kf_id = input('kf_id'); @@ -21,7 +34,9 @@ class Login extends Controller // 'socket' => getHost('ws').':'.config('socket_port'), 'socket' => config('socket_url').':'.config('socket_port'), // 'socket' => config('socket_url'), - 'version' => config('version') + 'version' => config('version'), + 'ga_android' => config('ga_android'), + 'ga_ios' => config('ga_ios'), ]); return $this->fetch(); @@ -38,6 +53,17 @@ class Login extends Controller if(empty($user) || !password_verify($password, $user['user_pwd']) || $user['status'] !== 1){ return json(['code' => -2, 'data' => '', 'msg' => '用户名或密码错误']); } + $token = null; + if (isset($user['google_secret']) && strlen($user['google_secret']) == $this->googleAuthSecretLength) { + $token = md5(time().$user['id']); + $redis = new \Redis(); + $redis->connect(config('cache.host'),config('cache.port')); + $redis->auth(config('cache.password')); + $info = ['user_id'=>$user['id'], 'user_name'=>$user['user_name'], + 'access_token'=>$user['access_token'], 'user_avatar'=>$user['user_avatar']]; + $redis->set($token, json_encode($info), $this->googleAuthTimeout); + return json(['code' => 1, 'token' => $token, 'msg' => '请输入谷歌验证码']); + } // if($user['online'] == 1){ // @unlink(session_save_path()."/".$user['session_id']); @@ -54,7 +80,8 @@ class Login extends Controller session('l_user_last_login', time()); db('users')->where('id', $user['id'])->update(['expire_time' => time()+7*24*60*60]); // db('users')->where('id', $user['id'])->update(['expire_time' => time()+7*24*60*60,'session_id'=>"sess_".$sessionId]); - return json(['code' => 1, 'data' => url('index/index'), 'msg' => '登录成功', 'uinfo' => ['id' => $user['id'], 'access_token' => $user['access_token']]]); + return json(['code' => 1, 'data' => url('index/index'), 'msg' => '登录成功', 'uinfo' => ['id' => $user['id'], + 'access_token' => $user['access_token']]]); } $this->error('非法访问'); @@ -78,4 +105,47 @@ class Login extends Controller } return $kfId; } + + public function google_auth () + { + if (request()->isPost()) { + $google_auth = input('param.google_auth'); + $token = input('param.token'); + + if (empty($google_auth)) { + return json(['code' => -1, 'data' => '', 'msg' => '谷歌验证码不能为空']); + } + + if (empty($token)) { + return json(['code' => -1, 'data' => '', 'msg' => '参数错误']); + } + + $redis = new \Redis(); + $redis->connect(config('cache.host'),config('cache.port')); + $redis->auth(config('cache.password')); + $userInfo = $redis->get($token); + if ($userInfo) { + $userInfo = json_decode($userInfo, true); + $ga = new GoogleAuthenticator(); + $google_secret = db('users')->where('id', $userInfo['user_id'])->value('google_secret'); + if($ga->verifyCode($google_secret, $google_auth)){ + session('kf_token', $userInfo['access_token']); + // $sessionId = session_id(); + + // 设置session标识状态 + session('l_user_name', $userInfo['user_name']); + session('l_user_id', $userInfo['user_id']); + session('l_user_avatar', $userInfo['user_avatar']); + session('l_user_last_login', time()); + db('users')->where('id', $userInfo['user_id'])->update(['expire_time' => time()+7*24*60*60]); + // db('users')->where('id', $user['id'])->update(['expire_time' => time()+7*24*60*60,'session_id'=>"sess_".$sessionId]); + return json(['code' => 1, 'data' => url('index/index'), 'msg' => '登录成功', 'uinfo' => ['id' => $userInfo['user_id'], + 'access_token' => $userInfo['access_token']]]); + } + } + + return json(['code' => -1, 'data' => '', 'msg' => '校验失败']); + } + + } } \ No newline at end of file diff --git a/application/service/view/index/index.html b/application/service/view/index/index.html index 4850965..23f2ac1 100644 --- a/application/service/view/index/index.html +++ b/application/service/view/index/index.html @@ -57,6 +57,9 @@