Best practices in the current period of response processing based on Yii 2 based on HTTP client extensions (optimized in the previous implementation, this is practiced for the next period of time, http Localization of client components, simplification of returned processing logic, etc.)
1. Get the menu list, as shown in Figure 1
{
"code": 10000,
"message": "获取菜单列表成功",
"data": {
"items": [
{
"id": "974b2b64aac24973dbd39b40fb2cc880",
"item_title": "文章审核",
"item_icon": "https://cmcconsole.chinamcloud.com/imgs/service_side_icon/cmcp/content-audits.png",
"item_link": "",
"item_desc": "",
"item_type": "category",
"item_sort": "1",
"item_pid": "0",
"item_target": "0",
"is_iframe": "1",
"children": []
},
{
"id": "61600e1530e7671346fe6e436195f4b7",
"item_title": "平台管控",
"item_icon": "https://cmcconsole.chinamcloud.com/imgs/service_side_icon/cmcp/plat_cont.png",
"item_link": "",
"item_desc": "",
"item_type": "category",
"item_sort": "2",
"item_pid": "0",
"item_target": "0",
"is_iframe": "1",
"children": []
},
{
"id": "66c8e5ce3261710ccc90841141939c92",
"item_title": "设置",
"item_icon": "https://cmcconsole.chinamcloud.com/imgs/service_side_icon/cmcp/set_conf.png",
"item_link": "",
"item_desc": "",
"item_type": "category",
"item_sort": "3",
"item_pid": "0",
"item_target": "0",
"is_iframe": "1",
"children": []
}
],
"_links": {
"self": {
"href": "http://api.gitlab-php-yii2-app-advanced-cmc.localhost/v1/menus?login_id=2e368664c41b8bf511bcc9c65d86dbc3&login_tid=0c4dba79f6028dcfc519917a45b98bba&page=1"
}
},
"_meta": {
"totalCount": 3,
"pageCount": 1,
"currentPage": 1,
"perPage": 20
}
}
}
2. Check the available Debug data, its data comes from HTTP, as shown in Figure 2
GET https://cmcconsole.flydev.chinamcloud.cn/nav-bar/service-side-bar?service_key=cmcp
Cookie: login_chinamcloud_id=2e368664c41b8bf511bcc9c65d86dbc3; login_chinamcloud_tid=0c4dba79f6028dcfc519917a45b98bba
3. Check the http model file: common/logics/http/cmc_console/menu.php, the code is as follows
* @since 1.0
*/
class Menu extends Model
{
public $service_key;
public $login_id;
public $login_tid;
public function rules()
{
return [
// login_id、login_tid 属性必须有值
[['login_id', 'login_tid'], 'required'],
];
}
public function attributeLabels()
{
return [
'service_key' => \Yii::t('model/http/cmc-console/menu', 'Service Key'),
'login_id' => \Yii::t('model/http/cmc-console/menu', 'Login Id'),
'login_tid' => \Yii::t('model/http/cmc-console/menu', 'Login Tid'),
];
}
/**
* 返回模块的菜单信息
*
* @param string $loginId 用户标识
* @param string $loginTid 登录标识
*
* @return array|bool
*
* 格式如下:
*
* 框架服务控制台的菜单信息
* [
* 'message' => '', //说明
* 'data' => [], //数据
* ]
*
* 失败(将错误保存在 [[yii\base\Model::errors]] 属性中)
* false
*
* @throws ServerErrorHttpException 如果响应状态码不等于20x
* @throws HttpException 如果登录超时
*/
public function getMenu($loginId, $loginTid)
{
$this->service_key = Yii::$app->params['cmcConsole']['serviceKey'];
$this->login_id = $loginId;
$this->login_tid = $loginTid;
$cookie = 'login_chinamcloud_id=' . $this->login_id . '; login_chinamcloud_tid=' . $this->login_tid . '';
$response = Yii::$app->cmcConsoleHttp->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar')
->setData([
'service_key' => $this->service_key,
])
->send();
// 检查响应状态码是否等于20x
if ($response->isOk) {
// 检查业务逻辑是否成功
if ($response->data['code'] === 10000) {
$cmcConsoleData = ['message' => $response->data['message'], 'data' => $response->data['data']['menu_data']];
return $cmcConsoleData;
} elseif ($response->data['code'] === 99998) {
// 登录超时
throw new HttpException(302, Yii::t('error', Yii::t('error', Yii::t('error', '201003'), ['message' => $response->data['message']])), 201003);
} else {
$this->addError('service_key', $response->data['message']);
return false;
}
} else {
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201001'), ['status_code' => $response->statusCode])), 201001);
}
}
}
4. CmcConsoleHttp is a component, configure the HTTP client through the application component, refer to the URL:https://www.yiiframework.com/extension/yiisoft/yii2-httpclient/doc/guide/2.0/zh-cn/usage-setup-client-instance, view the configuration file: common/config/main-local.php
'cmcConsoleHttp' => [
'class' => 'yii\httpclient\Client',
'baseUrl' => 'https://cmcconsole.flydev.chinamcloud.cn',
'transport' => 'yii\httpclient\CurlTransport'
],
5. Check the parameter file: common/config/params-local.php, there are configuration parameters related to the http client
// 框架服务控制台
'cmcConsole' => [
'hostInfo' => 'https://cmcconsole.flydev.chinamcloud.cn', // HOME URL
'baseUrl' => '', // BASE URL
'appKey' => 'assZdqfgOXcIFDvG', // 模块Key
'appSecret' => 'sd3fe4GDSRqrl7mLFyP7ogbk1pDFwzT', // 模块Secret
'serviceKey' => 'cmcp', // 服务标识
],
6. View the method file: api/rests/menu/indexaction.php, the method of calling the http model: getmenu, the code is as follows
// 所有输入数据都有效,获取模块的菜单信息
$menu = $model->getMenu($model->login_id, $model->login_tid);
if ($menu === false) {
if ($model->hasErrors()) {
$firstError = '';
foreach ($model->getFirstErrors() as $message) {
$firstError = $message;
break;
}
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '226020'), ['first_error' => $firstError])), 226020);
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Authority Center HTTP requests fail for unknown reasons.');
}
}
7. At this stage, there are 4 problems, as shown in Figure 3
(1) When multiple services need to be called based on HTTP in the application, the number of application components will continue to increase. Please register too many application components carefully. Application components are like global variables. Too much may increase the difficulty of testing and maintenance. In general, local components can be created when needed.
(2) When adding a new HTTP component, you need to configure the file at the same time: common/config/main-local.php, common/config/params-local.php, which is repeated and redundant.
(3) When the response status code is not equal to 20X, it is considered that the call failed (in most cases, this business logic holds). The server error exception is thrown. At this time, the error exception information is not perfect. Once the call fails, it is difficult to check the cause of the specific error.
(4) When calling the method of the HTTP model, the processing logic of the return is too complicated, and it seems redundant, and it needs to be streamlined.
8. Solve the first problem first, refer to the website:https://www.yiiframework.com/extension/yiisoft/yii2-httpclient/doc/guide/2.0/zh-cn/usage-setup-client-instance, to encapsulate the yii\HttpClient\Client instance as a local component
9. Check the inheritance relationship: yii\httpclient\client » yii\base\component, common\logics\http\cmc_console\menu » common\logics\http\cmc_console\model » \yii\base\model » yii\base\component, so yii\httpclient\client can be used in the model: Common\Logics\Http\CMC_Console\Model The instance is encapsulated as a local component, edit common/logics/http/cmc_console/model.php, and create an HTTP client object
* @since 1.0
*/
class Model extends \yii\base\Model
{
private $_httpClient;
/*
* 创建 HTTP 客户端对象
*
* @return object the created object
* @throws InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]].
*/
public function getHttpClient()
{
if (!is_object($this->_httpClient)) {
$this->_httpClient = Yii::createObject([
'class' => Client::className(),
'baseUrl' => Yii::$app->params['cmcConsole']['hostInfo'] . Yii::$app->params['cmcConsole']['baseUrl'],
'transport' => CurlTransport::className(),
]);
}
return $this->_httpClient;
}
}
10. Edit the HTTP model file: common/logics/http/cmc_console/menu.php, yii::$app->CmcConsoleHttp is replaced with $this->HttpClient
$response = $this->httpClient->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar')
->setData([
'service_key' => $this->service_key,
])
->send();
11. Edit the configuration file: common/config/main-local.php, delete component: cmcconsolehttp
12. Get the menu list, it can still be obtained normally, check the available Debug data, its data comes from http, and the request is unchanged, as shown in Figure 4
13. At this time, the first two problems have been solved. First, the third problem is solved. When the method of the HTTP model is called, the processing logic of the return is too complicated and redundant, and it needs to be simplified. Edit the HTTP model file: common/logics/http/cmc_console/menu.php, when the response status code is equal to 500 When it is not equal to 20X, it is considered that the call failed (in most cases, this business logic holds). print response object
$response = $this->httpClient->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar-1')
->setData([
'service_key' => $this->service_key,
])
->send();
print_r($response);
exit;
yii\httpclient\Response Object
(
[client] => yii\httpclient\Client Object
(
[baseUrl] => https://cmcconsole.flydev.chinamcloud.cn
[formatters] => Array
(
[urlencoded] => yii\httpclient\UrlEncodedFormatter Object
(
[encodingType] => 1
[charset] =>
)
)
[parsers] => Array
(
)
[requestConfig] => Array
(
)
[responseConfig] => Array
(
)
[contentLoggingMaxSize] => 2000
[_transport:yii\httpclient\Client:private] => yii\httpclient\CurlTransport Object
(
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[_headers:yii\httpclient\Message:private] => Array
(
[0] => HTTP/1.1 500 Internal Server Error
[1] => Server: nginx
[2] => Date: Wed, 11 Dec 2019 05:59:37 GMT
[3] => Content-Type: text/html; charset=UTF-8
[4] => Transfer-Encoding: chunked
[5] => Connection: keep-alive
[6] => Access-Control-Allow-Origin:
[7] => Access-Control-Allow-Methods: *
[8] => Access-Control-Allow-Credentials: true
[9] => Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
)
[_cookies:yii\httpclient\Message:private] =>
[_content:yii\httpclient\Message:private] => An Error occurred while handling another error:
yii\base\InvalidRouteException: Unable to resolve the request "site/error". in /home/www/cmc_console/vendor/yiisoft/yii2/base/Module.php:537
Stack trace:
#0 /home/www/cmc_console/vendor/yiisoft/yii2/web/ErrorHandler.php(97): yii\base\Module->runAction('site/error')
#1 /home/www/cmc_console/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#2 /home/www/cmc_console/vendor/sentry/Raven/ErrorHandler.php(86): yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: Raven_ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 {main}
Previous exception:
yii\base\InvalidRouteException: Unable to resolve the request: nav-bar/service-side-bar-1 in /home/www/cmc_console/vendor/yiisoft/yii2/base/Controller.php:143
Stack trace:
#0 /home/www/cmc_console/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('service-side-ba...', Array)
#1 /home/www/cmc_console/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('nav-bar/service...', Array)
#2 /home/www/cmc_console/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#3 /home/www/cmc_console/web/index.php(13): yii\base\Application->run()
#4 {main}
Next yii\web\NotFoundHttpException: 页面未找到。 in /home/www/cmc_console/vendor/yiisoft/yii2/web/Application.php:115
Stack trace:
#0 /home/www/cmc_console/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 /home/www/cmc_console/web/index.php(13): yii\base\Application->run()
#2 {main}
[_data:yii\httpclient\Message:private] =>
[_format:yii\httpclient\Message:private] =>
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
14. Edit the HTTP model file: common/logics/http/cmc_console/menu.php, when the response status code is equal to 500 When it is not equal to 20X, it is considered that the call failed (in most cases, this business logic holds). print response data
$response = $this->httpClient->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar-1')
->setData([
'service_key' => $this->service_key,
])
->send();
print_r($response->data);
exit;
Array
(
[0] => An Error occurred while handling another error:
yii\base\InvalidRouteException: Unable to resolve the request "site/error". in /home/www/cmc_console/vendor/yiisoft/yii2/base/Module.php:537
Stack trace:
#0 /home/www/cmc_console/vendor/yiisoft/yii2/web/ErrorHandler.php(97): yii\base\Module->runAction('site/error')
#1 /home/www/cmc_console/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#2 /home/www/cmc_console/vendor/sentry/Raven/ErrorHandler.php(86): yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: Raven_ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 {main}
Previous exception:
yii\base\InvalidRouteException: Unable to resolve the request: nav-bar/service-side-bar-1 in /home/www/cmc_console/vendor/yiisoft/yii2/base/Controller.php:143
Stack trace:
#0 /home/www/cmc_console/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('service-side-ba...', Array)
#1 /home/www/cmc_console/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('nav-bar/service...', Array)
#2 /home/www/cmc_console/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#3 /home/www/cmc_console/web/index.php(13): yii\base\Application->run()
#4 {main}
Next yii\web\NotFoundHttpException: 页面未找到。 in /home/www/cmc_console/vendor/yiisoft/yii2/web/Application.php:115
Stack trace:
#0 /home/www/cmc_console/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 /home/www/cmc_console/web/index.php(13): yii\base\Application->run()
#2 {main}
)
15. Edit the HTTP model file: common/logics/http/cmc_console/menu.php, when the response status code is equal to 500 When it is not equal to 20X, it is considered that the call failed (in most cases, this business logic holds). The server error exception is thrown. At this time, the error exception information is not perfect. Once the call fails, it is difficult to check the specific error cause
$response = $this->httpClient->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar-1')
->setData([
'service_key' => $this->service_key,
])
->send();
// 检查响应状态码是否等于20x
if ($response->isOk) {
// 检查业务逻辑是否成功
if ($response->data['code'] === 10000) {
$cmcConsoleData = ['message' => $response->data['message'], 'data' => $response->data['data']['menu_data']];
return $cmcConsoleData;
} elseif ($response->data['code'] === 99998) {
// 登录超时
throw new HttpException(302, Yii::t('error', Yii::t('error', Yii::t('error', '201003'), ['message' => $response->data['message']])), 201003);
} else {
$this->addError('service_key', $response->data['message']);
return false;
}
} else {
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201001'), ['status_code' => $response->statusCode])), 201001);
}
{
"name": "Internal Server Error",
"message": "框架服务控制台HTTP请求失败,状态码:500",
"code": 201001,
"status": 500,
"type": "yii\\web\\ServerErrorHttpException"
}
16. Edit the HTTP model file: common/logics/http/cmc_console/menu.php, when the response status code is equal to 500 When it is not equal to 20X, it is considered that the call fails (in most cases, this business logic holds). Throw the server error exception and improve the error exception information. Once the call fails, it is easier to check the specific error cause, as shown in Figure 5
$response = $this->httpClient->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar-1')
->setData([
'service_key' => $this->service_key,
])
->send();
// 检查响应状态码是否等于20x
if ($response->isOk) {
// 检查业务逻辑是否成功
if ($response->data['code'] === 10000) {
$cmcConsoleData = ['message' => $response->data['message'], 'data' => $response->data['data']['menu_data']];
return $cmcConsoleData;
} elseif ($response->data['code'] === 99998) {
// 登录超时
throw new HttpException(302, Yii::t('error', Yii::t('error', Yii::t('error', '201003'), ['message' => $response->data['message']])), 201003);
} else {
$this->addError('service_key', $response->data['message']);
return false;
}
} else {
$code = isset($response->data['code']) ? $response->data['code'] : 0; // 返回码
$message = isset($response->data[0]) ? $response->data[0] : ''; // 说明
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201001'), ['status_code' => $response->statusCode, 'code' => $code, 'message' => $message])), 201001);
}
{
"name": "Internal Server Error",
"message": "框架服务控制台HTTP请求失败,状态码:500,返回码:0,说明:An Error occurred while handling another error:\nyii\\base\\InvalidRouteException: Unable to resolve the request \"site/error\". in /home/www/cmc_console/vendor/yiisoft/yii2/base/Module.php:537\nStack trace:\n#0 /home/www/cmc_console/vendor/yiisoft/yii2/web/ErrorHandler.php(97): yii\\base\\Module->runAction('site/error')\n#1 /home/www/cmc_console/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\\web\\ErrorHandler->renderException(Object(yii\\web\\NotFoundHttpException))\n#2 /home/www/cmc_console/vendor/sentry/Raven/ErrorHandler.php(86): yii\\base\\ErrorHandler->handleException(Object(yii\\web\\NotFoundHttpException))\n#3 [internal function]: Raven_ErrorHandler->handleException(Object(yii\\web\\NotFoundHttpException))\n#4 {main}\nPrevious exception:\nyii\\base\\InvalidRouteException: Unable to resolve the request: nav-bar/service-side-bar-1 in /home/www/cmc_console/vendor/yiisoft/yii2/base/Controller.php:143\nStack trace:\n#0 /home/www/cmc_console/vendor/yiisoft/yii2/base/Module.php(528): yii\\base\\Controller->runAction('service-side-ba...', Array)\n#1 /home/www/cmc_console/vendor/yiisoft/yii2/web/Application.php(103): yii\\base\\Module->runAction('nav-bar/service...', Array)\n#2 /home/www/cmc_console/vendor/yiisoft/yii2/base/Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))\n#3 /home/www/cmc_console/web/index.php(13): yii\\base\\Application->run()\n#4 {main}\n\nNext yii\\web\\NotFoundHttpException: 页面未找到。 in /home/www/cmc_console/vendor/yiisoft/yii2/web/Application.php:115\nStack trace:\n#0 /home/www/cmc_console/vendor/yiisoft/yii2/base/Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))\n#1 /home/www/cmc_console/web/index.php(13): yii\\base\\Application->run()\n#2 {main}",
"code": 201001,
"status": 500,
"type": "yii\\web\\ServerErrorHttpException"
}
17. At this time, the first 3 problems have been solved, and there is only a 4th left. Edit the HTTP model file: common/logics/http/cmc_console/menu.php, refer to yii\base\model::validate(), when the response status code is equal to 20x, but the return code is not equal to the agreed successful return code, that is, the business logic fails, and the error is saved in After the yii\base\model::$errors attribute is in the attribute, it does not directly return false, but returns !$this->hasErrors(). In this case, why not throw the exception, the purpose is to make the caller handle this situation more flexibly, and whether the throwing exception is determined by the caller, not the model method itself.
$response = $this->httpClient->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar')
->setData([
'service_key' => $this->service_key,
])
->send();
// 检查响应状态码是否等于20x
$responseCode = isset($response->data['code']) ? $response->data['code'] : 0; // 返回码
$responseMessage = isset($response->data[0]) ? $response->data[0] : ''; // 说明
if ($response->isOk) {
// 检查业务逻辑是否成功
if ($responseCode === 10000) {
return ['message' => $response->data['message'], 'data' => $response->data['data']['menu_data']];
} elseif ($responseCode === 99998) {
// 登录超时
throw new HttpException(302, Yii::t('error', Yii::t('error', Yii::t('error', '201002'), ['code' => $responseCode, 'message' => $response->data['message']])), 201003);
} else {
$this->addError('id', Yii::t('error', Yii::t('error', Yii::t('error', '201002'), ['code' => $responseCode, 'message' => $response->data['message']])));
return !$this->hasErrors();
}
} else {
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201001'), ['status_code' => $response->statusCode, 'code' => $responseCode, 'message' => $responseMessage])), 201001);
}
18. The language files have been adjusted
common/messages/en-cn/error.php
201001 => '框架服务控制台HTTP请求失败,状态码:{status_code},返回码:{code},说明:{message}',
201002 => '框架服务控制台HTTP请求失败,返回码:{code},说明:{message}',
201003 => '',
201010 => '{first_error}',
api/messages/en-cn/error.php
226020 => '{first_error}',
19. Edit method file: api/rests/menu/indexaction.php, the method of calling the http model: getmenu, since the http model file does not return false directly, but returns !$this->hasErrors(), then when False is returned, $model->hasErrors() must be equal to true, so its judgment can be omitted, the code is as follows
// 所有输入数据都有效,获取模块的菜单信息
$menu = $model->getMenu($model->login_id, $model->login_tid);
if (!$menu) {
$firstError = '';
foreach ($model->getFirstErrors() as $message) {
$firstError = $message;
break;
}
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '226020'), ['first_error' => $firstError])), 226020);
}
20. When the response status code is equal to 20X, but the return code is not equal to the agreed successful return code, that is, the business logic fails, the menu list is obtained, and the last problem is solved, as shown in Figure 6
{
"name": "Internal Server Error",
"message": "框架服务控制台HTTP请求失败,返回码:99999,说明:menu_id不能为空",
"code": 226020,
"status": 500,
"type": "yii\\web\\ServerErrorHttpException"
}
21. Since all model files in common/logics/http/cmc_console are based on the same HTTP client component, the processing logic of its response is basically the same. Decides to encapsulate the processing logic of the response as a common method for easy reuse. Edit common/logics/http/cmc_console/model.php
/*
* 响应对象的处理
*
* @param object $response 响应对象
*
* @return array|bool
*
* 格式如下:
*
* 框架服务控制台的菜单信息
* [
* 'message' => '', //说明
* 'data' => [], //数据
* ]
*
* 失败(将错误保存在 [[yii\base\Model::errors]] 属性中)
* false
*
* @throws ServerErrorHttpException 如果响应状态码不等于20x
* @throws HttpException 如果登录超时
*/
public function responseHandler($response)
{
// 检查响应状态码是否等于20x
$responseCode = isset($response->data['code']) ? $response->data['code'] : 0; // 返回码
if ($response->isOk) {
// 检查业务逻辑是否成功
if ($responseCode === 10000) {
return ['message' => $response->data['message'], 'data' => $response->data['data']];
} elseif ($responseCode === 99998) {
// 登录超时
throw new HttpException(302, Yii::t('error', Yii::t('error', Yii::t('error', '201002'), ['code' => $responseCode, 'message' => $response->data['message']])), 201003);
} else {
$this->addError('id', Yii::t('error', Yii::t('error', Yii::t('error', '201002'), ['code' => $responseCode, 'message' => $response->data['message']])));
return !$this->hasErrors();
}
} else {
$responseMessage = isset($response->data[0]) ? $response->data[0] : ''; // 说明
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201001'), ['status_code' => $response->statusCode, 'code' => $responseCode, 'message' => $responseMessage])), 201001);
}
}
22. Edit the http model file: common/logics/http/cmc_console/menu.php, in the method: getMenu In the processing method of the public response object: responseHandler($response)
$response = $this->httpClient->createRequest()
->setHeaders(['Cookie' => $cookie])
->setMethod('get')
->setUrl('nav-bar/service-side-bar')
->setData([
'service_key' => $this->service_key,
])
->send();
return $this->responseHandler($response);





