You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wkcrm/application/admin/controller/ApiCommon.php

59 lines
2.2 KiB

4 years ago
<?php
// +----------------------------------------------------------------------
// | Description: Api基础类验证权限
// +----------------------------------------------------------------------
// | Author:
// +----------------------------------------------------------------------
namespace app\admin\controller;
4 years ago
use think\Cache;
4 years ago
use think\Request;
use think\Db;
use app\common\adapter\AuthAdapter;
use app\common\controller\Common;
class ApiCommon extends Common
{
public function _initialize()
{
parent::_initialize();
/*获取头部信息*/
$header = Request::instance()->header();
$request = Request::instance();
4 years ago
$authKey = trim($header['authkey']);
$sessionId = trim($header['sessionid']);
4 years ago
$paramArr = $request->param();
$platform = $paramArr['platform'] ? '_'.$paramArr['platform'] : ''; //请求平台(mobile,ding)
4 years ago
$cache = Cache::get('Auth_'.$authKey.$platform);
// $cache = cache('Auth_'.$authKey.$platform);
4 years ago
// 校验sessionid和authKey
4 years ago
if (empty($sessionId) || empty($authKey) || empty($cache)) {
4 years ago
header('Content-Type:application/json; charset=utf-8');
$dataTime=date('H:i',time());
exit(json_encode(['code' => 302, 'data' => ['extra' => 1, 'extraTime' => $dataTime], 'msg' => '请先登录!']));
}
//登录有效时间
$cacheConfig = config('cache');
4 years ago
$loginExpire = !empty($cacheConfig['expire']) ? $cacheConfig['expire'] : 86400 * 30;
4 years ago
// 检查账号有效性
$userInfo = $cache['userInfo'];
$map['id'] = $userInfo['id'];
$map['status'] = array('in',['1','2']);
$userData = Db::name('admin_user')->where($map)->find();
if (!$userData) {
header('Content-Type:application/json; charset=utf-8');
exit(json_encode(['code'=>103, 'data' => [], 'msg'=>'账号已被删除或禁用']));
}
session('user_id', $userInfo['id']);
// 更新缓存
4 years ago
Cache::set('Auth_'.$authKey, $cache, $loginExpire);
// cache('Auth_'.$authKey, $cache, $loginExpire, 'UserToken');
4 years ago
// $GLOBALS['userInfo'] = $userInfo;
}
}