Commit 554837ab authored by huanle's avatar huanle

0528 Token

parent 6cbc2cc1
......@@ -45,36 +45,99 @@ class Index extends Controller
public function getSsoToken(){
$code = input('post.code');
//var_dump($code);
//$access_token= Session::get('access_token');
$User =new Dingtalk\User();
$userid =$User->code($code);
//$userid='231115647';
$token = md5(create_unique());
$tokenModel = new TokenModel();
$client = null;
$exist= $this->getUserToken($userid,$client);
if (!empty($exist)) {
// 更新过期时间
$tokenModel->updateExpire($exist, config('token_expire_second'));
return code(['token' => $exist, 'expire_second' => config('token_expire_second')]);
}
$token = md5(create_unique(). $code);
$expireSecond = config('token_expire_second');
$rs = cache('token',$token ,$expireSecond);
if($userid===false){
$data['status']=-10001;
$data['errmsg']='登录失败';
}else{
$this->removeToken($userid, $client);
// 设置新的token
$rs = cache('token_'.$token, $token, $expireSecond);
$t=cache('token_'.$token);
if ($rs) {
// 设置我的新缓存记录
$record = cache('token_'.$userid);
$record = json_decode($record, true);
is_array($record) || $record = [];
$record[] = ['token' =>$token, 'client' => $client ];
cache('token_'.$userid, json_encode($record), 0);
$data['status']=0;
$data['errmsg']='登录成功';
$data['userid']=$userid;
$data['token']=$token;
} else {
$data['status']=-10001;
$data['errmsg']='登录失败';
}
// return json($data);
return json(code([$data], 0, 'ok'));
}
// public function create($uid)
// {
//
// $tokenModel = new TokenModel();
// $token = $tokenModel->createToken($uid);
// if ($token == false) {
// return json(code([], $tokenModel->getErrCode(), $tokenModel->getError()));
// } else {
// return json(code(['token' => $token, 'expire_second' => config('token_expire_second')]));
// }
// }
public function create($uid)
{
$tokenModel = new TokenModel();
$token = $tokenModel->createToken($uid);
if ($token == false) {
return json(code([], $tokenModel->getErrCode(), $tokenModel->getError()));
} else {
return json(code(['token' => $token, 'expire_second' => config('token_expire_second')]));
}
}
/**
* 清除token
* @param int $uid 用户ID
* @param null $client 客户端类型
* @return bool
*/
public function removeToken($uid, $client = null)
{
$oldToken = cache('token_'.$uid);
if ($oldToken !== false) {
$oldToken = json_decode($oldToken, true);
$_oldToken = [];
foreach ($oldToken as $key=>$value) {
// 清除所有或者客户端类型为指定的
if ($client == null or $value['client'] == $client) {
cache('token_'.$value['token'], null);
} else {
$_oldToken[] = $value;
}
}
return cache('token_'.$uid, json_encode(array_values($_oldToken)));
}
return true;
}
/**
* 获取用户token
* @param strign $uid 用户id
* @param string $client 类型
* @return bool
*/
public function getUserToken($uid, $client)
{
$tokens = cache('token_'.$uid);
var_dump($tokens);
$tokens = json_decode($tokens, true);
if (empty($tokens)) {
return false;
}
foreach ($tokens as $token) {
if (isset($token['client']) and $token['client']==$client) {
$_value = cache('token_'. $token['token']);
if (!empty($_value)) {
return $token['token'];
}
}
}
return false;
}
}
<?php
/**
* Created by PhpStorm.
* User: liupei
* Date: 2018/1/30
* Time: 14:25
*/
namespace app\login\controller;
use think\Controller;
use app\login\model\Token as TokenModel;
/**
* 处理用户token
* @package app\access\controller
*/
class Token extends Controller
{
/**
* 检测token是否合法
* @return array
*/
public function check()
{
$token = input('post.token');
$tokenModel = new TokenModel();
$checkResult = $tokenModel->check($token);
if ($checkResult == false) {
return json(code([], $tokenModel->getErrCode(), $tokenModel->getError()));
} else {
return json(code(['bind_info' => json_decode($checkResult)]));
}
}
public function login(){
$code = input('post.code');
//var_dump($code);
//$access_token= Session::get('access_token');
$User =new Dingtalk\User();
$userid =$User->code($code);
$result= $this->create($userid);
var_dump($result);die();
if($userid===false){
$data['status']=-10001;
$data['errmsg']='登录失败';
}else{
$data['status']=0;
$data['errmsg']='登录成功';
$data['userid']=$userid;
}
// return json($data);
return json(code([$data], 0, 'ok'));
}
public function create($uid)
{
//$client = data('client', 'pc');// 客户端标识phone,pc,pc_msg目前只支持3 种
$tokenModel = new TokenModel();
// if ($getOld == 1) {
// $exist = $tokenModel->getUserToken($uid, $client);
// if (!empty($exist)) {
// // 更新过期时间
// $tokenModel->updateExpire($exist, config('token_expire_second'));
// return code(['token' => $exist, 'expire_second' => config('token_expire_second')]);
// }
// }
$token = $tokenModel->createToken($uid);
if ($token == false) {
return json(code([], $tokenModel->getErrCode(), $tokenModel->getError()));
} else {
return json(code(['token' => $token, 'expire_second' => config('token_expire_second')]));
}
}
}
\ No newline at end of file
......@@ -37,9 +37,14 @@ class Token extends Model
* @param $client string 客户端标识phone,pc目前只支持2 种
* @return bool|string
*/
public function createToken($uid)
public function createToken($uid, $bindInfo, $client)
{
if (empty($bindInfo)) {
$this->error = '绑定token的数据不能为空';
$this->errCode = 1001;
return false;
}
if (empty($uid)) {
$this->error = '绑定uid的数据不能为空';
......@@ -47,21 +52,22 @@ class Token extends Model
return false;
}
$token = md5(create_unique());
$token = md5(create_unique(). $bindInfo);
$expireSecond = config('token_expire_second');
// 清除token
$this->removeToken($uid);
$this->removeToken($uid, $client);
// 设置新的token
$rs = cache('token_'.$token, $expireSecond);
$rs = cache('token_'.$token, $bindInfo, $expireSecond);
if ($rs) {
// 设置我的新缓存记录
$record = cache('token_'.$uid);
$record = json_decode($record, true);
is_array($record) || $record = [];
$record[] = ['token' =>$token];
$record[] = ['token' =>$token, 'client' => $client ];
cache('token_'.$uid, json_encode($record), 0);
return $token;
} else {
$this->errCode = 10000;
......@@ -69,4 +75,61 @@ class Token extends Model
return false;
}
}
/**
* 清除token
* @param int $uid 用户ID
* @param null $client 客户端类型
* @return bool
*/
public function removeToken($uid, $client = null)
{
$oldToken = cache('token_'.$uid);
if ($oldToken !== false) {
$oldToken = json_decode($oldToken, true);
$_oldToken = [];
foreach ($oldToken as $key=>$value) {
// 清除所有或者客户端类型为指定的
if ($client == null or $value['client'] == $client) {
cache('token_'.$value['token'], null);
} else {
$_oldToken[] = $value;
}
}
return cache('token_'.$uid, json_encode(array_values($_oldToken)));
}
return true;
}
public function getUserToken($uid, $client)
{
$tokens = cache('token_'.$uid);
$tokens = json_decode($tokens, true);
if (empty($tokens)) {
return false;
}
foreach ($tokens as $token) {
if (isset($token['client']) and $token['client']==$client) {
$_value = cache('token_'. $token['token']);
if (!empty($_value)) {
return $token['token'];
}
}
}
return false;
}
/**
* 更新缓存过期时间
* @param string $token
* @param int $expireSecond
* @return bool|mixed
*/
public function updateExpire($token, $expireSecond)
{
$info = cache('token_'.$token);
if ($info != false) {
return cache('token_'.$token, $info, $expireSecond);
}
return false;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment