Commit 28feeaf4 authored by jiangyipeng's avatar jiangyipeng

Merge branch 'master' of gitlab.linanquan.com:jiangyipeng/doc_phone

parents c5ef172c bdfbbb88
...@@ -13,7 +13,7 @@ class Index extends Controller ...@@ -13,7 +13,7 @@ class Index extends Controller
public function _initialize() public function _initialize()
{ {
$token = input('post.token'); $token = input('post.token');
$old_token =Cache::get('token'); $old_token = cache('token_'.$token);
if($old_token){ if($old_token){
if($token!=$old_token){ if($token!=$old_token){
$rs = json_encode(code([], 10003, 'token不合法')); $rs = json_encode(code([], 10003, 'token不合法'));
......
...@@ -86,6 +86,7 @@ class Doclist extends Model ...@@ -86,6 +86,7 @@ class Doclist extends Model
if ($docid) { if ($docid) {
$where['docid'] = array('=',$docid); $where['docid'] = array('=',$docid);
} }
$rs = $this->where($where)->update($data); $rs = $this->where($where)->update($data);
......
...@@ -45,36 +45,99 @@ class Index extends Controller ...@@ -45,36 +45,99 @@ class Index extends Controller
public function getSsoToken(){ public function getSsoToken(){
$code = input('post.code'); $code = input('post.code');
//var_dump($code);
//$access_token= Session::get('access_token');
$User =new Dingtalk\User(); $User =new Dingtalk\User();
$userid =$User->code($code); $userid =$User->code($code);
//$userid='231115647'; $tokenModel = new TokenModel();
$token = md5(create_unique()); $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'); $expireSecond = config('token_expire_second');
$rs = cache('token',$token ,$expireSecond); $this->removeToken($userid, $client);
if($userid===false){ // 设置新的token
$data['status']=-10001; $rs = cache('token_'.$token, $token, $expireSecond);
$data['errmsg']='登录失败'; $t=cache('token_'.$token);
}else{ 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['status']=0;
$data['errmsg']='登录成功'; $data['errmsg']='登录成功';
$data['userid']=$userid; $data['userid']=$userid;
$data['token']=$token; $data['token']=$token;
} else {
$data['status']=-10001;
$data['errmsg']='登录失败';
} }
// return json($data);
return json(code([$data], 0, 'ok')); return json(code([$data], 0, 'ok'));
} }
// public function create($uid) public function create($uid)
// { {
//
// $tokenModel = new TokenModel(); $tokenModel = new TokenModel();
// $token = $tokenModel->createToken($uid); $token = $tokenModel->createToken($uid);
// if ($token == false) { if ($token == false) {
// return json(code([], $tokenModel->getErrCode(), $tokenModel->getError())); return json(code([], $tokenModel->getErrCode(), $tokenModel->getError()));
// } else { } else {
// return json(code(['token' => $token, 'expire_second' => config('token_expire_second')])); 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 ...@@ -37,9 +37,14 @@ class Token extends Model
* @param $client string 客户端标识phone,pc目前只支持2 种 * @param $client string 客户端标识phone,pc目前只支持2 种
* @return bool|string * @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)) { if (empty($uid)) {
$this->error = '绑定uid的数据不能为空'; $this->error = '绑定uid的数据不能为空';
...@@ -47,21 +52,22 @@ class Token extends Model ...@@ -47,21 +52,22 @@ class Token extends Model
return false; return false;
} }
$token = md5(create_unique()); $token = md5(create_unique(). $bindInfo);
$expireSecond = config('token_expire_second'); $expireSecond = config('token_expire_second');
// 清除token // 清除token
$this->removeToken($uid); $this->removeToken($uid, $client);
// 设置新的token // 设置新的token
$rs = cache('token_'.$token, $expireSecond); $rs = cache('token_'.$token, $bindInfo, $expireSecond);
if ($rs) { if ($rs) {
// 设置我的新缓存记录 // 设置我的新缓存记录
$record = cache('token_'.$uid); $record = cache('token_'.$uid);
$record = json_decode($record, true); $record = json_decode($record, true);
is_array($record) || $record = []; is_array($record) || $record = [];
$record[] = ['token' =>$token]; $record[] = ['token' =>$token, 'client' => $client ];
cache('token_'.$uid, json_encode($record), 0); cache('token_'.$uid, json_encode($record), 0);
return $token; return $token;
} else { } else {
$this->errCode = 10000; $this->errCode = 10000;
...@@ -69,4 +75,61 @@ class Token extends Model ...@@ -69,4 +75,61 @@ class Token extends Model
return false; 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