79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* Created by 北京捷讯佳音.
|
|
* User: sam
|
|
* Date: 2019/2/15
|
|
* Time: 11:17
|
|
*/
|
|
|
|
namespace app\index\controller;
|
|
|
|
use think\Controller;
|
|
use think\response\Json;
|
|
|
|
class Base extends Controller
|
|
{
|
|
// 初始化
|
|
public function _initialize()
|
|
{
|
|
$para = input('param.');
|
|
if (empty($para)) {
|
|
return json(['code' => 302, 'msg' => '请求失败.']);
|
|
}
|
|
$return = $this->_checkAccessTokenIsValid($para);
|
|
|
|
if ($return['code'] != 200) {
|
|
return json($return);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 验证令牌.
|
|
* @param array $param 参数集合
|
|
* @return array
|
|
*/
|
|
private static function _checkAccessTokenIsValid(&$param)
|
|
{
|
|
if (empty($param['access_token'])) {
|
|
return ['code' => 400, 'msg' => 'access_token不能为空.'];
|
|
}
|
|
|
|
if (empty($param['type'])) {
|
|
return ['code' => 450, 'msg' => 'type不能为空.'];
|
|
}
|
|
|
|
if($param['type'] == 'kf') {
|
|
$result = db('users')->field('id,user_name,sex,user_avatar,access_token,expire_time')
|
|
->where(['access_token' => $param['access_token']])
|
|
->find();
|
|
if (empty($result)) {
|
|
return ['code' => 450, 'msg' => 'access_token不存在.'];
|
|
}
|
|
|
|
if ($result['access_token'] != $param['access_token']) {
|
|
return ['code' => 450, 'msg' => 'access_token错误.'];
|
|
}
|
|
|
|
if ($result['expire_time'] < time()) {
|
|
return ['code' => 450, 'msg' => 'access_token已过期.'];
|
|
}
|
|
|
|
$param['user'] = [
|
|
'uid' => "KF".$result['id'],
|
|
'user_name' => $result['user_name'],
|
|
'sex' => $result['sex'],
|
|
'avatar_url' => $result['user_avatar']
|
|
];
|
|
}else{
|
|
$userInfo = $param['access_token'];
|
|
if(is_string($userInfo)){
|
|
$userInfo = json_decode($userInfo,true);
|
|
}
|
|
$param['user'] = $userInfo['user'];
|
|
}
|
|
unset($param['access_token']);
|
|
|
|
return ['code' => 200, 'msg' => '验证成功.'];
|
|
}
|
|
} |