此类可作为小程序或app应用接口的基类,我们建议开发者在开发自己的应用的时候,尽量继承这个基类,共用相同的变量和方法,虽然这不是硬性要求。
此类的结构如下:
namespace app;
use app\common\model\TenantDepartment;
use ky\ErrorCode;
use support\Response;
class ApiController extends BaseController
{
protected $captchaKey = 'captchaApi';
public function __construct()
{
parent::__construct();
}
/**
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string|null $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param int $code
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return Response
*/
protected function jSuccess($msg = '操作失败!', $data = '', int $code = 1, array $header = []): Response
{
$msg = dao_trans($msg);
$result = [
'code' => $code,
'msg' => $msg,
'data' => $data
];
return json($result);
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param null $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param int $code
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return Response
*/
protected function jError($msg = '操作失败!', int $code = ErrorCode::BadParam, $data = '', array $header = []): Response
{
$msg = dao_trans($msg);
$result = [
'code' => $code,
'msg' => $msg,
'data' => $data
];
return json($result);
}
protected function getAjax()
{
$json = \request()->rawBody();
return $json ? json_decode($json, true) : [];
}
}