hyperf框架数据返回格式封装

记录api成功和失败返回格式封装

ResponseService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

namespace App\Service\Utils;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\ResponseInterface;

/**
* API请求返回服务类
*
* Class ResponseService
*
* @package App\Service\Utils
*/
class ResponseService
{
/**
* @var int
*/
private $http_code = 200;

/**
* http头部信息
*
* @var string[]
*/
private $http_headers = [
'Author' => 'Colorado',
];

/**
* 业务返回码
* 组成结构:xx(业务模块) xx(业务子模块) xx(细化编码)
* 举例:110101 11(APP模块)01(鉴权模块)01(登录失败)
*
* @var int
*/
private $business_code = 100000;

/**
* 业务返回消息
*
* @var string
*/
private $business_msg = 'ok';

/**
* @Inject
* @var ResponseInterface
*/
private $response;

/**
* 设置http状态码
*
* @param int $code
*
* @return $this
*/
public function setHttpCode(int $code = 200): self
{
$this->http_code = $code;

return $this;
}

/**
* 设置http头部信息
*
* @param string $name
* @param mixed $value
*
* @return $this
*/
public function setHttpHeader(string $name, $value): self
{
$this->http_headers[$name] = (string)$value;

return $this;
}

/**
* 成功数据返回
*
* @param mixed $data 返回数据
* @param int $business_code 业务返回码
*
* @return ResponseInterface|\Psr\Http\Message\ResponseInterface
*/
public function success($data, int $business_code = 100000)
{
$this->business_code = $business_code;

return $this->response($data);
}

/**
* 失败返回
*
* @param string $error_msg 错误信息
* @param mixed $data 返回数据
* @param int $business_code 错误业务码
*
* @return ResponseInterface|\Psr\Http\Message\ResponseInterface
*/
public function fail(string $error_msg = 'fail', $data = null, int $business_code = 999999)
{
$this->business_code = $business_code;
$this->business_msg = $error_msg;

return $this->response($data);
}

/**
* 返回数据
*
* @param $data
*
* @return ResponseInterface|\Psr\Http\Message\ResponseInterface
*/
private function response($data)
{
$this->response = $this->response->json($this->normalizeData($data))->withStatus($this->http_code);

if (! empty($this->http_headers)) {
foreach ($this->http_headers as $name => $value) {
$this->response = $this->response->withHeader($name, $value);
}
}

return $this->response;
}

/**
* 标准化返回数据格式
*
* @param mixed $data 业务返回数据
*
* @return array
*/
private function normalizeData($data): array
{
return [
'code' => $this->business_code,
'data' => $data,
'message' => $this->business_msg,
'timestamp' => time(),
];
}
}

AbstractController

1
2
3
4
5
6
public function __call($name, $arguments)
{
if (method_exists(ResponseService::class, $name)) {
return make(ResponseService::class)->{$name}(...$arguments);
}
}

Demo

1
2
// 成功
return $this->success($result);