php公共请求返回接口类

现在前后端分离,就得用到接口,写接口就得先定义好语言包,接口调用公共类.以下写了一个post请求,数据格式是json方式,返回也是json数据

如图postman

php公共请求返回接口类

定义一个公共类和语言包

<?php
require('L.php');

class AdminApiController
{
    public function json($arr, $output = true)
    {

        $content = json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        if (! $output) {
            return $content;
        }

        // 跨域处理
        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
        $allow_origin = [
            'http://www.xxxxx.com',//生产环境
            'http://www.test.com',//测试环境
        ];
        if (in_array($origin, $allow_origin)) {
            header('Access-Control-Allow-Origin: ' . $origin);
        }

        header('Content-Type: application/json; charset=utf-8');
        exit($content);
    }


    /***
     * add by wangming
     * 正确时返回
     * @param $result
     * @param bool $output
     * @return string
     */
    public function successResult($result, $output = true)
    {
        //$arr['data'] = $result;
        //$arr['code'] = 200;
        return $this->json($result, $output);
    }

    /***
     * add by wangming
     * 出错时返回方法
     * @param $result
     * @param bool $output
     * @return string
     */
    public function errorResult($result, $output = true)
    {
        $arr = [];
        if (is_numeric($result))
        {
            $arr['code'] = intval($result);
            $arr['msg'] = L::t($result);
        }elseif (is_array($result))
        {
            $arr = $result;
        }
        if (!isset($arr['code']))
        {
            $arr['code'] = intval(ErrorCode::UNKNOWN_ERROR_TYPE);
            $arr['msg'] = is_string($result)?$result:L::t(ErrorCode::UNKNOWN_ERROR_TYPE);
        }
        return $this->json($arr, $output);
    }

    /**
     * POST提交数据
     * @param $fields string|array raw|form-data
     * @param $header array HTTP头
     * @param $timeout integer 服务端处理超时(秒)
     * @return mixed
     */
    public function curlPost($url, $fields = '', $header = ['Content-Type: application/json; charset=utf-8'], $timeout = 3)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}

?>

 

L.php

 

<?php

/**
 * $Author: wolf
 * 提示返回码
 * $Id: L.php 17217 2019-04-06 10:29:00
 */


class L
{
    private static $dictionary = [
        10000 => '正常输出',

        10001 => '数据格式错误',
        10002 => '参数access_token不能为空',
        10003 => '无效的access_token',
        10004 => '过期的access_token',
        10005 => 'token缺失或者错误',
        10006 => 'token失效',
        10007 => 'refresh token错误',
        10008 => '过期的refresh_token',
        10009 => '数据插入失败',
        10010 => '数据更新失败',
        10011 => '数据删除失败',
        10012 => '参数类型错误',
        10013 => '参数user_account不能为空',
        10014 => '接口解析出错'
    ];

    public static function t($code)
    {
        return isset(self::$dictionary[$code])?self::$dictionary[$code]:$code;
    }

    public function __invoke($code)
    {
        return isset(self::$dictionary[$code]) ? self::$dictionary[$code] : $code;
    }
}


?>

请指导,学习架构师之路