Commit 4e7fa389 authored by huanle's avatar huanle

0527

parent 84dcbc37
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
return [
// token过期时间
'token_expire_second' => 86400,
];
<?php <?php
namespace app\login\controller; namespace app\login\controller;
use app\login\model\Token as TokenModel;
use cjango\Dingtalk\User; use cjango\Dingtalk\User;
use src\Dingtalk\Utils; use src\Dingtalk\Utils;
use think\Controller; use think\Controller;
...@@ -8,12 +9,20 @@ use src\Dingtalk; ...@@ -8,12 +9,20 @@ use src\Dingtalk;
use think\Session; use think\Session;
class Index extends Controller class Index extends Controller
{ {
// public function index(){ public function check()
// {
// return $this->fetch(); $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 config(){ public function config(){
$Ding = new Dingtalk(); $Ding = new Dingtalk();
$Token =new Dingtalk\Token(); $Token =new Dingtalk\Token();
...@@ -41,6 +50,9 @@ class Index extends Controller ...@@ -41,6 +50,9 @@ class Index extends Controller
$User =new Dingtalk\User(); $User =new Dingtalk\User();
$userid =$User->code($code); $userid =$User->code($code);
$token =new Token();
$gettoken = $token->create($userid);
if($userid===false){ if($userid===false){
$data['status']=-10001; $data['status']=-10001;
$data['errmsg']='登录失败'; $data['errmsg']='登录失败';
...@@ -51,6 +63,9 @@ class Index extends Controller ...@@ -51,6 +63,9 @@ class Index extends Controller
} }
// return json($data); // return json($data);
return json(code([$data], 0, 'ok')); return json(code([$data], 0, 'ok'));
// var_dump($_SESSION); // var_dump($_SESSION);
}
public function checkToken(){
} }
} }
<?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
<?php
/**
* Created by PhpStorm.
* User: liupei
* Date: 2018/1/30
* Time: 14:34
*/
namespace app\login\model;
use think\Model;
class Token extends Model
{
/**
* 检测token是否合法
* @param $token string 用户token
* @return bool
*/
public function check($token)
{
$info = cache('token_'.$token);
if ($info === false) {
$this->errCode = 10003;
$this->error = '没有token';
return false;
}
return $info;
}
/**
* 生成token并绑定数据,2小时内重新请求自动续期
* @param $uid binint 用户uid
* @param $bindInfo string 要绑定的数据
* @param $client string 客户端标识phone,pc目前只支持2 种
* @return bool|string
*/
public function createToken($uid)
{
if (empty($uid)) {
$this->error = '绑定uid的数据不能为空';
$this->errCode = 1001;
return false;
}
$token = md5(create_unique());
$expireSecond = config('token_expire_second');
// 清除token
$this->removeToken($uid);
// 设置新的token
$rs = cache('token_'.$token, $expireSecond);
if ($rs) {
// 设置我的新缓存记录
$record = cache('token_'.$uid);
$record = json_decode($record, true);
is_array($record) || $record = [];
$record[] = ['token' =>$token];
cache('token_'.$uid, json_encode($record), 0);
return $token;
} else {
$this->errCode = 10000;
$this->error = 'token写入失败';
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