基于tp框架下的app接口实现

首先添加一个接口的基础控制器,用于把数据转化为需要的格式(json,xml),可以命名为ApiBaseController ,

<?php
// +----------------------------------------------------------------------
// | TEA [ The customer with the product, we won't change any day. ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.lechezai.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Elinx <654753115@qq.com> Last modified time:2016-01-02
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;
/**
 * Api 公共控制器 
 */
class ApiBaseController extends Controller {

    // private $appid      = ''; // 签名id
    // private $appsecret  = ''; // 签名值

    // public function _empty() {//空操作
    //     $this->apiReturn(4001,'invalid action');
    // }
    // // 初始化服务端接口
    // protected function _initialize() {
    //     if (ACTION_NAME != 'gettoken') {
    //         //签名认证
    //         $this->checkSignature();
    //     }
    // }

	
    /**
    @@ 窝是没有意思的分隔线 @_@
    */

    /**
     * 签名认证
     */
    private function checkSignature() {
        // $_GET['token'] = '4c8f01d06e1735f88d1a97f63c87c9150a95c864';
        if(!isset($_GET['token'])){

            $this->apiReturn(4001,'invalid token');

        } else if (!S($_GET['token'])){
            $this->apiReturn(4001,'invalid token');

        }
    }
    // 获取token
    public function gettoken(){
        $appid      = I('appid');       // 签名id
        $appsecret  = I('appsecret');   // 签名值
        //检查是否为空
        if (empty($appid) || empty($appsecret)) {
            $this->apiReturn(4001,'need appid or appsecrect');
        }
        // 获取远程api数据
        $api = M('api')->where(array('api_appid'=>$appid, 'api_appsecret'=>$appsecret))->find();
        // 判断appid、appsecret是否合法
        if (!$api) {
            $this->apiReturn(4001,'invalid!');
        } else {
            // 判断是否被禁用
            if ($api['status'] != 1) {
                $this->apiReturn(300,'apikey invalid!');
            } else {
                $this->appid        = $api['api_appid'];
                $this->appsecret    = $api['api_appsecret'];
            }
        }
        // 获取缓存中token
        $ori_str = S($this->appid.'_'.$this->appsecret);
        if($ori_str){ // 不进行重新获取,把以前的token返回
            $this->apiReturn(200, 'success', array('token'=>$ori_str));
            // S($ori_str, null); // 重新获取并把原有的删除
        } else {
            //这里是token产生的机制  您也可以自己定义
            $nonce  = createNoncestr(32);
            $tmpArr = array($nonce,$this->appid,$this->appsecret);
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
            // echo $tmpStr;
            //这里做了缓存 'a'=>b 和'b'=>a格式的缓存
            S($this->appid.'_'.$this->appsecret,$tmpStr,7200);  
            S($tmpStr,$this->appid.'_'.$this->appsecret,7200);// 缓存token值
            //返回token值
            $data   = array('token'=>$tmpStr);
            $this->apiReturn(200,'success',$data);
        }
    }
    /**
     * [apiReturn 用于给app提供接口使用 带有请求结果状态表示,和结果提示,默认返回json]
     * @param  [number] $status  [请求结果的状态标识,设定后要在文档中给予说明]
     * @param  string $message [请求结果的提示语句]
     * @param  [array] $data    [请求返回的数据,app前端需要的数据]
     * @param  [string] $type    [要返回的数据类型,支持json,xml,默认返回json]
     * @return [json或xml]          [返回数据]
     */
    protected function apiReturn($status,$message='',$data=null,$type='json'){
        if(!is_numeric($status) || !is_string($message) ){
            $this->apiReturn('400','参数错误');
        }
        $res            = array();
        $res['status']  = $status;
        $res['message'] = $message;
        $res['data']    = $data;
        if(in_array($type, array('json','xml'))){
            $this->ajaxReturn($res,$type);
        }else{
            $this->ajaxReturn($res);
        }
    }
    
    // 加载全局配置信息
    protected function load_config(){
    	$attr = M('config_attr')->field('attr_title,config_value')->select();
    	foreach ($attr as $key => $value) {
    		$config[$value['attr_title']] = $value['config_value'];
    	}
    	return $config;
    }
}

然后写接口的控制器继承ApiBaseController 这个控制器,

 

把需要的数据查出来后用apiReturn函数返回就ok了。

需要注意的是$this->apiReturn后,这个方法后面的代码都不执行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值