zoukankan      html  css  js  c++  java
  • 登录方法借鉴

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    /**
    * Created by PhpStorm.
    * User: huangyaokui
    * Date: 16/4/23
    * Time: 下午2:17
    */
    class Common_Controller extends CI_Controller
    {
    protected $user_id = 0;
    protected $session_data = [];
    //不用用户认证的方法在$except数组里配置
    protected $except = [];

    public function __construct()
    {
    parent::__construct();

    try{
    $this->authentication();
    }catch (Exception $e){
    $this->errorResponse($e);
    }
    }


    /**
    * 是否需要登录认证
    * @return bool
    */
    private function isCheck()
    {
    $array = array_change_key_case(array_flip($this->except));
    $method = strtolower($this->router->method);

    return array_key_exists($method, $array) ? false : true;
    }

    /**
    * 登录认证
    * @throws Exception
    */
    private function authentication()
    {
    if ( $this->isCheck() ) {
    $token = $this->input->get_post('token', true);

    if ($token) {
    $this->checkCache(trim($token));
    }else{
    throw new Exception('请登录', '401');
    }
    }
    }

    /**
    * 从缓存里查询用户是否登录
    * @param $token
    * @throws Exception
    */
    private function checkCache($token)
    {
    $data = [];
    if ( $this->cache->memcached->is_supported() ){
    $data = $this->cache->memcached->get($token);
    if (!$data) {
    $data = $this->checkData($token);
    }
    }else{
    $data = $this->checkData($token);
    }

    if ($data) {
    $this->user_id = $data['userId'];
    $this->session_data = $data;
    }else{
    throw new Exception('请登录', '401');
    }
    }

    /**
    * 从数据库认证
    * @param $token
    * @throws Exception
    */
    private function checkData($token)
    {
    $this->load->model('token_model');

    $result = $this->token_model->get([
    'where' => [
    'token' => $token
    ],
    'default_limit' => 1,
    'order' => 'created desc'
    ]);

    if ($result) {
    if ($result['0']['expire'] < time()) {
    throw new Exception('请重新登录', '401');
    }

    $this->session_data = json_decode($result['0']['data'], true);
    $this->user_id = $this->session_data['userId'];
    }else{
    throw new Exception('请登录', '401');
    }
    }

    /**
    * 成功返回
    * @param array $data
    */
    protected function successResponse(array $data)
    {
    echo json_encode([
    'data' => $data ?: new stdClass(),
    'status' => 0,
    'message' => ''
    ]);
    exit;
    }

    /**
    * 失败返回
    * @param $code
    */
    protected function errorResponse(Exception $exception)
    {
    echo json_encode([
    'data' => new stdClass(),
    'status' => $exception->getCode(),
    'message' => $exception->getMessage()
    ]);
    exit;
    }
    }

  • 相关阅读:
    JMeter中Ultimate Thread Group插件下载及使用
    UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: ill
    Python向excel中写入数据
    Jmeter命令行执行压力测试
    Atlas中使用UpdatePanel的中文乱码问题
    今日无战事
    在Asp.Net 2.0中应用DataFormatString
    喜欢这里
    VS2005+SQL2005 Reporting Service动态绑定报表(Web)
    关于GridView中自定义分页、单选、多选、排序、自增列的简单应用
  • 原文地址:https://www.cnblogs.com/brady-wang/p/5475432.html
Copyright © 2011-2022 走看看