<?php
namespace app\common\controller;
//api基类
use think\facade\Session;
class ApiBase extends BaseController
{
protected $config = [
//api返回数据方式
'resultType'=>'json_encode',
];
protected $data = ''; //api最后返回的数据
protected $statusCode = ''; //api返回的状态码
protected $param; //api接收到的参数
public function __construct()
{
parent::__construct();
$this->param = $this->request->param();
unset($this->param['version'],$this->param['controller'],$this->param['action']);
}
//自动加载请求的api版本、类、方法
public function autoLoad()
{
//检查token
$this->checkParam();
//权限控制
//处理请求
$param = $this->request->param();
$version = isset($param['version'])?$param['version']:'';
$controller = isset($param['controller'])?$param['controller']:'';
$action = isset($param['action'])?$param['action']:'index';
if (empty($version) || empty($controller)){
$this->returnResult(['code'=>0,'msg'=>'请查看api文档']);
} else{
//调用对应的分层控制器
$obj = controller($controller,$version);
method_exists($obj,$action)?$obj->$action():$this->_empty($action); //判断方法是否存在
}
}
//返回api结果,所有方法都调用此方法返回结果
protected function returnResult($result='')
{
if ($result['code'] === 0){
$data = ['code'=>$result['code'],'msg'=>$result['msg']];
} elseif ($result['code'] === 1){
$data = ['code'=>$result['code'],'data'=>$result['data']];
}
echo $this->config['resultType']($data,JSON_UNESCAPED_UNICODE);
}
//检查appid
protected function checkAppid()
{
if (!isset($this->param['appid'])){
$this->statusCode = 100;
$this->data = '无效的appid';
return false;
} else {
//在数据库中匹配appid
}
return true;
}
//检查param
protected function checkParam()
{
//判断是否已经登陆过了
if (!isset($this->param['token'])){
$this->checkAppid();
} else {
//验证token是否匹配
$token = Session::get('api_'.$this->param['token']);
//session中存在token信息
if ($token){
if ($token['str'] === $this->param['token']){
//检查token是否过期
if (($token['startTime']-time()) > getConfig('apiDeadline')){
$this->statusCode = 104;
$this->data = 'access token已经过期';
}
}
} else {
$this->statusCode = 101;
$this->data = '无效的access token';
return false;
}
}
return true;
}
//空操作
public function _empty($name)
{
$this->returnResult(['code'=>0,'msg'=>'function \''.$name.'\' does not exist']);
}
}v1文件夹中的一个控制器<?php
namespace app\api\v1;
use app\common\controller\ApiBase;
//后台导航数据api
class Menu extends ApiBase
{
private $MenuMod = null; //导航模型对象
public function __construct()
{
$this->MenuMod = new \app\common\model\Nav();
parent::__construct();
}
//返回后台导航全部详细数据
public function showAllMenu()
{
$limit = isset($this->param['limit'])?$this->param['limit']:10;
$order = isset($this->param['order'])?$this->param['order']:'';
$datas = $this->MenuMod->getInfo(['module'=>2],'',$limit,'',$order);
if ($datas){
$rel = ['code'=>0,'data'=>$datas];
} else {
$rel = ['code'=>1,'msg'=>'没有查到数据'];
}
$this->returnResult($rel);
}
}@thinkphp
ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。