zoukankan      html  css  js  c++  java
  • thinkphp在app接口开发过程中的通讯数据的封装

    在为app开发接口过程中,我们必不可少的要为app前端工程师们提供返回的数据,如何灵活快速又易懂的返回他们需要的数据是非常关键的。

    其实thinkphp已经把很多我们要用到的都写出来了,我们只需要稍作修改即可灵活的返回我们需要的数据。

    首先,修改Thinkphp/Library/Controller.class.php  在其中增加一个函数 apiReturn();

     /**
     * [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,$type){
    
            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);
            }
            
        }

    增加了这样一个函数后,我们就可以很轻松的在任意控制器下使用了

    举个例子 在thinkphp的Home分组下的Index控制器里我们新增一个test方法

    <?php
    namespace HomeController;
    use ThinkController;
    class IndexController extends Controller {
        public function index(){
            $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        }
        public function  test(){
            $data = array(
                'id'=>2,
                'username'=>'明之暗夜',
                'info'=>array('age'=>24,'address'=>'学府路','url'=>'http://cnblogs.com/dmm888')
            );
            if($data){
                $this->apiReturn(200,'读取用户信息成功',$data);
            }
        }
        
    
    }

     在浏览器访问这个接口时可以看到返回的信息

    {"status":200,"message":"u8bfbu53d6u7528u6237u4fe1u606fu6210u529f","data":{"id":2,"username":"u660eu4e4bu6697u591c","info":{"age":24,"address":"u5b66u5e9cu8def","url":"http://cnblogs.com/dmm888"}}}

     这里的数字200可以自定义的 但是我们要在接口说明文档中给予说明。

    要返回xml只要在后面添加参数就可以

    <?php
    namespace HomeController;
    use ThinkController;
    class IndexController extends Controller {
        public function index(){
            $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        }
        public function  test(){
            $data = array(
                'id'=>2,
                'username'=>'明之暗夜',
                'info'=>array('age'=>24,'address'=>'学府路','url'=>'http://cnblogs.com/dmm888')
            );
            if($data){
                $this->apiReturn(200,'读取用户信息成功',$data,xml);
            }
        }
        
    
    }

    返回的数据如下

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
    <think>
    <status>200</status>
    <message>读取用户信息成功</message>
    <data>
    <id>2</id>
    <username>明之暗夜</username>
    <info>
    <age>24</age>
    <address>学府路</address>
    <url>http://cnblogs.com/dmm888</url>
    </info>
    </data>
    </think>

    还有很多可以拓展的 就不一一列举了  如果有什么好的意见可以直接给我提 

  • 相关阅读:
    CMDB
    Linux文档编辑
    Linux文件管理
    Scrapy框架全
    爬虫基础
    C#中的泛型-1
    网络协议
    PDF抽取文字 C# with Adobe API
    PDF转图片 C# with Adobe API
    浅谈Exchange 2013开发-如何操作邮件的附件
  • 原文地址:https://www.cnblogs.com/dmm888/p/4395755.html
Copyright © 2011-2022 走看看