RESTful接口的ThinkPHP3.2实现

RESTful

什么是RESTful接口呢?这里引几个文章,可以先行阅读

我们为什么要用RESTful

  • 因为我们实现完全的前后端分离,需要一个接口规范,来方便和规范我们的接口规范

Try it!

这里我们实现一个登陆换取TOKEN的例子

  • 首先我们新建一个控制AuthController,注意这个控制器,需要继承RestController
namespace V1\Controller;
use Think\Controller\RestController;//use Rest的控制器
class AuthController extends RestController //继承Rest控制器
{
    //protected $allowMethod  = array('get','post','put','delete'); // REST允许的请求类型列表
    //protected $allowType    = array('json','html'); // REST允许请求的资源类型列表
    public function index(){
      
    }
 }
  • 然后我们写一个方法,这里官方文档说:
image.png

于是我们就可以新建一个方法login_post

public function login_post(){
  echo "test";  
  //TODO
}
  • 然后我们用工具测试一下,我们是否能请求到这个方法,这里我用的是Google Chrome的一个插件,叫做Restlet Client
    image.png

注意这里都是POST请求

  • 成功的输出了test,说明我们可以请求到这个接口。那么我们就可以写一些业务代码了:
private $res=array(
        'code'=>200,
        'msg'=>'ok',
        'data'=>array()
    );
public function login_post(){
        $uuid = $_POST['uuid'];
        $open = $_POST['open'];
        $timestamp = $_POST['timestamp'];
        if ($uuid==''||$open==''||$timestamp==''){
            $this->res['code']=401;
            $this->res['msg']='Incomplete parameters';
            $this->response($this->res,"json");
            exit();
        }
        $now = time();
        if ($now-$timestamp>3600){
            $this->res['code']=401;
            $this->res['msg']='Login Timeout';
            $this->response($this->res,"json");
            exit();
        }
        $user = M('user');
        $where['uuid']=$uuid;
        $where['open']=$open;
        $result = $user->where($where)->select();
        if (empty($result)){
            $this->res['code']=404;
            $this->res['msg']='Invalid UserID or openid';
            $this->response($this->res,'json');
            exit();
        }else{
            $token = self::_applyTokenAndSaveInRedis($uuid,$open);
            $this->res['data']=array(
                'TOKEN'=>$token,
                'Expiretime'=>3600
            );
            $this->response($this->res,'json');
        }
    }
    private static function _applyTokenAndSaveInRedis($uuid,$open,$expiretime=3600){
        $redis=new \Redis();
        $redis->connect('地址','端口');
        $redis->auth('密码');
        $s = rand(10000,99999);
        $string = $open.$uuid.$s.time();
        $token = md5($string);
        $save = $token."*".(time()+$expiretime);
        $redis->set($uuid,$save);
        return $token;
    }

这里的功能,是收取uuidopentimestamp三个参数,然后首先验证参数是完整,然后验证时间戳是否过期,然后链接数据库验证账号,然后保存生成token并保存在Redis里,然后返回token和过期时间。

  • 然后我们验证一下
参数不全
时间戳过期
参数错误
正常返回

结束

var author = {
  name:'丁波',
  GitHub:'dingbo1028',
  University:'BNUZ'
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 文章分类 后台文章分类列表页模板导的详细步骤建立数据表blog_category,并添加相应的文章字段使用php ...
    JoyceZhao阅读 1,771评论 0 14
  • Communicating with APNs The APNs provider API lets you se...
    魔灵FH阅读 5,807评论 0 8
  • 思绪卡了壳 连影子也断断续续 我才发现自己只是个小写的诗人 笔触里的墨容不下整个黑夜 也容不下没有你的秋天 我只是...
    我还是光之帝皇侠阅读 349评论 7 7
  • 用户正在的分享(1) 1. 业务描述: 查看用户正在进行的分享,支持分页 2. 调用方式: url地址:https...
    花园兜阅读 260评论 0 0