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); } }