webman_duanju/app/common/controller/Frontend.php

209 lines
6.6 KiB
PHP

<?php
namespace app\common\controller;
use app\common\library\Auth;
use app\common\library\token\TokenExpirationException;
use app\exception\MyBusinessException;
use support\exception\BusinessException;
use support\Request;
use HttpResponseException;
use support\Response;
class Frontend
{
/**
* 无需登录的方法
* 访问本控制器的此方法,无需会员登录
* @var array
*/
protected array $noNeedLogin = [];
/**
* 默认响应输出类型,支持json/xml/jsonp
* @var string
*/
protected string $responseType = 'application/json';
/**
* 无需鉴权的方法
* @var array
*/
protected array $noNeedPermission = [];
protected Request $request;
/**
* 权限类实例
* @var Auth
*/
protected Auth $auth;
public function getUserId()
{
return $this->auth->getUserInfo()['user_id'];
}
public function __construct()
{
$this->request = request();
$needLogin = !action_in_arr($this->noNeedLogin);
// 初始化会员鉴权实例
$this->auth = Auth::instance();
$token = request()->header('token');
if ($token) {
if(!$this->auth->init($token)) {
$this->error('Token expiration', [], 409);
}
}else {
if ($needLogin) {
$this->error('请登录', [
'type' => $this->auth::NEED_LOGIN
], $this->auth::LOGIN_RESPONSE_CODE);
}
if ($needLogin) {
if (!$this->auth->isLogin()) {
$this->error('请登录', [
'type' => $this->auth::NEED_LOGIN
], $this->auth::LOGIN_RESPONSE_CODE);
}
}
}
}
/**
* 操作成功
* @param string $msg 提示消息
* @param mixed $data 返回数据
* @param int $code 错误码
* @param string|null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
protected function success(string $msg = '', mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = []): void
{
$this->result($msg, $data, $code, $type, $header, $options);
}
protected function successWithData(mixed $data = null, string $msg = '', int $code = 0, string $type = null, array $header = [], array $options = []): void
{
$this->result($msg, $data, $code, $type, $header, $options);
}
/**
* 操作失败
* @param string $msg 提示消息
* @param mixed $data 返回数据
* @param int $code 错误码
* @param string|null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
protected function error(string $msg = '', mixed $data = null, int $code = -1, string $type = null, array $header = [], array $options = []): void
{
$this->result($msg, $data, $code, $type, $header, $options);
}
protected function errorMsg(string $msg = '', mixed $data = null, int $code = -1, string $type = null, array $header = [], array $options = []): void
{
$this->result($msg, $data, $code, $type, $header, $options, 'data', 'msg');
}
/**
* 操作成功
* @param string $msg 提示消息
* @param mixed $data 返回数据
* @param int $code 错误码
* @param string|null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
protected function n_success(array $data, string $msg = 'ok', int $code = 0, string $type = null, array $header = [], array $options = []): void
{
$this->resultApi($data, $msg, $code, $type, $header, $options);
}
/**
* 操作失败
* @param string $msg 提示消息
* @param mixed $data 返回数据
* @param int $code 错误码
* @param string|null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
protected function n_error(string $msg, array $data = [], int $code = -1, string $type = null, array $header = [], array $options = []): void
{
$this->resultApi($data, $msg, $code, $type, $header, $options);
}
/**
* 多种操作结果
*/
public function resultApi(array $data = [], string $msg = 'ok', int $code = 0, string $type = null, array $header = [], array $options = [])
{
$data['code'] = $code;
$data['message'] = $msg;
$data['time'] = time();
if(isset($data['data']['records'])) {
$data['data']['records'] = apiconvertToCamelCase($data['data']['records']);
}
if(isset($data['page']['list'])) {
$data['page']['list'] = apiconvertToCamelCase($data['page']['list']);
}
if(isset($data['data']['list'])) {
$data['data']['list'] = apiconvertToCamelCase($data['data']['list']);
}
if(isset($data['data']['userEntity'])) {
$data['data']['userEntity'] = apiconvertToCamelCase($data['data']['userEntity']);
}
$result = $data;
throw new MyBusinessException(json_encode($result));
}
/**
* 返回 API 数据
* @param string $msg 提示消息
* @param mixed $data 返回数据
* @param int $code 错误码
* @param string|null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
public function result(string $msg, mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = [], string $dataKey = 'data', string $msgKey = 'message')
{
if(isset($data['records'])) {
$data['records'] = apiconvertToCamelCase($data['records']);
}
if(isset($data['page'])) {
$data['page'] = apiconvertToCamelCase($data['page']);
}
if(isset($data['list'])) {
$data['list'] = apiconvertToCamelCase($data['list']);
}
$result = [
'code' => $code,
'message' => $msg,
'time' => time(),
$dataKey => $data,
];
throw new MyBusinessException(json_encode($result));
}
public function ApiDataReturn($data)
{
return json($data);
}
}