为接口开发做的基础

This commit is contained in:
2017-09-18 23:29:51 +08:00
parent b174b07425
commit e404954769
9 changed files with 335 additions and 8 deletions

View File

@@ -16,15 +16,31 @@ class Api {
public function __construct() {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept , token");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
$header = getallheaders();
$this->data = array('code' => 0, 'msg' => '', 'time' => time(), 'data' => '');
if (!$this->checkToken()) {
$isCheck = $this->checkToken($header);
$url = request()->module() . '/' . request()->controller() . '/' . request()->action();
if (!$isCheck && 'api/index/gettoken' !== strtolower($url)) {
$this->data['code'] = '301';
$this->data['data'] = '非法请求!';
echo json($this->data);
exit();
}
}
protected function checkToken(){
return true;
protected function checkToken($header){return true;
if (isset($header['Authorization']) && $header['Authorization']) {
$token = authcode($header['Authorization']);
list($appid, $appsecret, $currentTime) = explode('|', $token);
$client = db('Client')->where('appid', $appid)->where('appsecret', $appsecret)->value('id');
if ($client && ($currentTime+86400) < time()) {
return true;
}else{
return false;
}
}else{
return false;
}
}
}

View File

@@ -0,0 +1,20 @@
<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace app\common\model;
/**
* Client模型
*/
class Client extends Base{
protected $auto = array('update_time');
protected $insert = array('create_time');
}

View File

@@ -0,0 +1,36 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\common\validate;
/**
* 设置模型
*/
class Client extends Base{
protected $rule = array(
'appid' => 'require|number|unique:client',
'appsecret' => 'require|alphaNum',
'title' => 'require'
);
protected $message = array(
'appid.require' => 'appid必须',
'appid.unique' => 'appid已经存在',
'appid.number' => 'appid只能为数字',
'appsecret.require' => 'appsecret必须',
'appsecret.alphaNum' => 'appsecret只能为数字和字母',
'title' => '客户端名称必须',
);
protected $scene = array(
'add' => array('appid', 'appsecret', 'title'),
'edit' => array('appid', 'appsecret', 'title')
);
}