zoukankan      html  css  js  c++  java
  • API接口笔记

    1.基类

        定义返回信息

        protected $user;                  //用户表
        protected $token;                 //用户token
        protected $isSuccess = FALSE;    //状态是否成功
        protected $msg = '操作成功';       //返回给开发者的数据
        protected $Usermsg;               //返回给用户的数据
        protected $api_code;              //接口名
        protected $code = 0;              //状态码
        protected $results;               //返回数据
    

      

    定义返回成功的快捷信息

    // 输出正确数据
      public function restSuccess($msgCode = '0|操作成功')
      {
        $this->isSuccess = True;
        $messageCode = explode('|',$msgCode);
        @$this->code  = $messageCode[0];
        @$this->msg   = $messageCode[1];
        unset($messageCode);
      }

    定义返回失败的快捷信息

      // 输出错误数据
      public function restError($msgCode = '1|服务器繁忙|输入数据有误')
      {
        $this->isSuccess = False;
        $messageCode = explode('|',$msgCode);
        $this->code  = $messageCode[0];
        @$this->Usermsg = $messageCode[1];
        @$this->msg = $messageCode[2];
        unset($messageCode);
      }

    定义整合的返回信息

    // 整体输出返回数据
      public function response()
      {
          $data['api_code'] = $this->api_code;
          $data['code'] = $this->code;
          $data['isSuccess'] = $this->isSuccess;
          $data['msg'] = $this->msg;
          $data['Usermsg'] = $this->Usermsg;
          $data['results'] = $this->results;
          return $data;
      }

    API健壮性

     // 检测设备
      private function checkDevice()
      {
        if(!isMobile()){
          $this->restError(config('errorMsg.notMobile'));
          echo json_encode($this->response());
          exit();
        }
    
      }
    
      //检测请求方式
      private  function checkMethod()
      {
        if(!$this->request->isPost()){
          $this->restError(config('errorMsg.notMethod'));
          echo json_encode($this->response());
          exit();
        }
      }

    定义构造方法,使用TP5内置方法

    public function _initialize()
    {
          parent::_initialize();
    
          
          $this->api_code = input('api_code');
          
          $this->checkDevice();   // 检查设备
          $this->checkMethod();  //检测请求方式 
          $this->checkLogin(); //检测登录
          //$this->checkApi();   // 检测APi
      }

    2.缓存

    // 浏览帖子
        public function index()
        {    
            $info = cache('postindex');//查找缓存
            
            if(!$info){//如果缓存没有数据
                $list = new Mpost;//查找数据库
                $info = $list->i_ndex();
                cache('postindex',$info,3600);//设置缓存
            }
            $this->assign('arr', $info);
            return view('postd/index');
        }        

    ...

  • 相关阅读:
    计算机注销、热启动、冷启动
    从高处理解android与服务器交互(看懂了做开发就会非常的容易)
    Android—Work—1day
    软件需求分析方法
    Android 常用控件的介绍
    android中Json的一些应用
    java数据传递例子+内存分析
    android中MVP模式
    android的四层体系结构,基于mvc三层结构浅析
    java
  • 原文地址:https://www.cnblogs.com/chenrunxuan/p/6850487.html
Copyright © 2011-2022 走看看