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
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-5-31
* Time: 上午10:29
*/
namespace api\base;
use Yii;
use yii\web\Response;
use yii\web\Controller;
class BaseController extends Controller
{
public $start;
public $end;
public $header_list = [];
public $lang;
public $currency_id;
public $platform_id;
private static $default_header_list = [];
public function init()
{
if ('cli' !== php_sapi_name()) {
$this->header_list = self::$default_header_list;
$this->fzmCrossHeader();
}
}
public function fzmCrossHeader()
{
$this->lang = \Yii::$app->request->headers->get('lang') ?? 'zh-CN';
if ('en' == strtolower($this->lang)) {
$this->lang = 'en-US';
}
if (\Yii::$app->request->headers->get('FZM-PLATFORM-ID')) {
$this->platform_id = \Yii::$app->request->headers->get('FZM-PLATFORM-ID');
}
if (\Yii::$app->request->headers->get('FZM-CURRENCY-ID')) {
$this->currency_id = \Yii::$app->request->headers->get('FZM-CURRENCY-ID');
}
}
public function beforeAction($action)
{
$this->start = microtime(true);
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
public function afterAction($action, $result)
{
$this->end = microtime(true);
return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
}
/**
* 返回成功结果,附加成功code
* @param type $data
* @return array
*/
public static function formatSuccessResult($data = null)
{
return self::formatResult(0, 'ok', $data);
}
/**
* 返回结果,附加部分信息
* @param int $errcode
* @param string $errmsg
* @param array $data
*/
public static function formatResult($errcode, $errmsg, $data = null)
{
$callback = Yii::$app->request->get('callback');
$result = [
'errcode' => $errcode,
'errmsg' => $errmsg,
];
if ($data !== null) {
$result['data'] = Yii::createObject('yii\rest\Serializer')->serialize($data);
}
$response = Yii::$app->response;
$response->format = Response::FORMAT_JSON;
//jsonp数据格式
if (!is_null($callback)) {
Yii::$app->getResponse()->format = Response::FORMAT_JSONP;
$result = [
'data' => $result,
'callback' => $callback,
];
}
return $result;
}
protected function addPaginationHeaders($pagination)
{
$links = [];
foreach ($pagination->getLinks(true) as $rel => $url) {
$links[] = "<$url>; rel=$rel";
}
Yii::$app->getResponse()->getHeaders()
->set('X-Pagination-Total-Count', $pagination->totalCount)
->set('X-Pagination-Page-Count', $pagination->getPageCount())
->set('X-Pagination-Current-Page', $pagination->getPage() + 1)
->set('X-Pagination-Per-Page', $pagination->pageSize)
->set('Link', implode(', ', $links));
}
protected function getRequestedFields()
{
$fields = Yii::$app->request->get('fields');
$expand = Yii::$app->request->get('expand');
return [
preg_split('/\s*,\s*/', $fields, -1, PREG_SPLIT_NO_EMPTY),
preg_split('/\s*,\s*/', $expand, -1, PREG_SPLIT_NO_EMPTY),
];
}
protected function serializeModels(array $models)
{
list ($fields, $expand) = $this->getRequestedFields();
foreach ($models as $i => $model) {
if ($model instanceof Arrayable) {
$models[$i] = $model->toArray($fields, $expand);
} elseif (is_array($model)) {
$models[$i] = ArrayHelper::toArray($model);
}
}
return $models;
}
}