RESTful接口的ThinkPHP3.2實(shí)現(xiàn)

RESTful

什么是RESTful接口呢?這里引幾個(gè)文章,可以先行閱讀

我們?yōu)槭裁匆肦ESTful

  • 因?yàn)槲覀儗?shí)現(xiàn)完全的前后端分離,需要一個(gè)接口規(guī)范,來方便和規(guī)范我們的接口規(guī)范

Try it!

這里我們實(shí)現(xiàn)一個(gè)登陸換取TOKEN的例子

  • 首先我們新建一個(gè)控制AuthController,注意這個(gè)控制器,需要繼承RestController
namespace V1\Controller;
use Think\Controller\RestController;//use Rest的控制器
class AuthController extends RestController //繼承Rest控制器
{
    //protected $allowMethod  = array('get','post','put','delete'); // REST允許的請(qǐng)求類型列表
    //protected $allowType    = array('json','html'); // REST允許請(qǐng)求的資源類型列表
    public function index(){
      
    }
 }
  • 然后我們寫一個(gè)方法,這里官方文檔說:
image.png

于是我們就可以新建一個(gè)方法login_post

public function login_post(){
  echo "test";  
  //TODO
}
  • 然后我們用工具測(cè)試一下,我們是否能請(qǐng)求到這個(gè)方法,這里我用的是Google Chrome的一個(gè)插件,叫做Restlet Client
    image.png

注意這里都是POST請(qǐng)求

  • 成功的輸出了test,說明我們可以請(qǐng)求到這個(gè)接口。那么我們就可以寫一些業(yè)務(wù)代碼了:
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三個(gè)參數(shù),然后首先驗(yàn)證參數(shù)是完整,然后驗(yàn)證時(shí)間戳是否過期,然后鏈接數(shù)據(jù)庫驗(yàn)證賬號(hào),然后保存生成token并保存在Redis里,然后返回token和過期時(shí)間。

  • 然后我們驗(yàn)證一下
參數(shù)不全
時(shí)間戳過期
參數(shù)錯(cuò)誤
正常返回

結(jié)束

var author = {
  name:'丁波',
  GitHub:'dingbo1028',
  University:'BNUZ'
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • 文章分類 后臺(tái)文章分類列表頁模板導(dǎo)的詳細(xì)步驟建立數(shù)據(jù)表blog_category,并添加相應(yīng)的文章字段使用php ...
    JoyceZhao閱讀 1,764評(píng)論 0 14
  • Communicating with APNs The APNs provider API lets you se...
    魔靈FH閱讀 5,802評(píng)論 0 8
  • 思緒卡了殼 連影子也斷斷續(xù)續(xù) 我才發(fā)現(xiàn)自己只是個(gè)小寫的詩人 筆觸里的墨容不下整個(gè)黑夜 也容不下沒有你的秋天 我只是...
    我還是光之帝皇俠閱讀 348評(píng)論 7 7
  • 用戶正在的分享(1) 1. 業(yè)務(wù)描述: 查看用戶正在進(jìn)行的分享,支持分頁 2. 調(diào)用方式: url地址:https...
    花園兜閱讀 259評(píng)論 0 0