一个简单的php接口定义

<?php
class InterfaceBase
{
    /**
     * 获取请求参数
     *
     * @return mixed
     */
    public function getParams()
    {
        $input = json_decode(file_get_contents('php://input'), true);
        return $input;
    }

    /**
     * 获取错误信息
     *
     * @param $errorCode
     * @return mixed|string
     */
    public function getErrorMsg($errorCode)
    {
        $errors =  array(
            '8001' => '账号不存在',
            '8002' => '登录已过期,请重新登录',
            '8003' => '登录密码错误',
            '8004' => '账号已锁定或注销,请联系管理员',
            '8005' => '登录失败,请重试',
            '8006' => '登出失败,Token不能为空',
            '8007' => '登出失败,请重试',
            '8008' => '登录账号不能为空',
            '8009' => '登录密码不能为空',
            '8010' => '原始密码不正确',
            '8011' => '修改密码失败,请重试'
        );
        if(!$errors[$errorCode]){
            return '未知错误:'.$errorCode;
        }
        return $errors[$errorCode];
    }

    /**
     * 返回成功
     *
     * @param $data
     * @param string $errMsg
     * @return mixed|string
     */
    public function success($data, $errMsg='')
    {
        return $this->result(true, 0, $errMsg, $data);
    }

    /**
     * 返回失败
     *
     * @param $errCode
     * @param string $errMsg
     * @param null $data
     * @return mixed|string
     */
    public function fail($errCode, $errMsg='', $data=null)
    {
        return $this->result(false, $errCode, $errMsg, $data);
    }

    /**
     * 返回结果
     *
     * @param bool $success
     * @param int $errCode
     * @param string $errMsg
     * @param null $data
     * @return array
     */
    public function result($success = true, $errCode = 0, $errMsg = '', $data = null)
    {
        if($errCode and !$errMsg){
            $errMsg = $this->getErrorMsg($errCode);
        }
        return array(
            'Success'=>$success,
            'Errcode'=>intval($errCode),
            'Errmsg'=>$errMsg,
            'Data'=>$data
        );
    }

    /**
     * 输出结果
     *
     * @param $data
     */
    public function echoRet($data)
    {
        $responseTmp = $data;
        $responseTmp['Data'] = json_encode($responseTmp['Data']);
        WeUtility::logging('canteen_api.response', $responseTmp);

        echo json_encode($data, 256|64);
        exit;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值