Common.php
4.88 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
class YHMApi_Common
{
protected static $get_fields = "";
private static $currencyObj = "";
protected static $channel;
private static $platformList = array(
'ios' => 'private_key',
'iphone' => 'private_key',
'android' => 'private_key',
'show' => ''
);
private static $badParameter = array(
'/api', 'q', 'client_secret', 'debug_data', '/api/api'
);
static function delParameter(array $params)
{
$_params = array();
foreach ($params as $key => $val) {
if (in_array($key, self::$badParameter)) {
continue;
}
$_params[$key] = $params[$key];
}
return $_params;
}
static function init(array $params)
{
$clientSecret = $params['client_secret'];
$debugData = (empty($params['debug_data']) || $params['debug_data'] != 'Y') ? 'N' : 'Y';
$params = self::delParameter($params);
self::$channel = $params['client_type'];
$params['private_key'] = self::$platformList[$params['client_type']];
$_params = self::packageSort($params);
$_makeKey = self::makeSign($_params);
$verifySign = self::verifySign($_makeKey, $clientSecret);
$debug = 'N';
if (!empty($params['debug']) && $params['debug'] == 'Y') {
$debug = 'Y';
}
if ($verifySign == false && $debug == 'N') {
$makeData = array(
'server_client_secret' => $_makeKey,
'make_data' => $_params
);
if ($debugData == 'N') {
$makeData = array();
}
//self::echoJson(self::result(500, '数据验证错误.', $makeData));
}
}
/**
* 提取指定的字段
* Enter description here ...
* @param string $fields 要获取的字段
* @param array $data 数据
* @param string $fields_config 默认使用$get_fields,也可以自己指定
*/
static function makeFields($fields, array $data, $fields_config = null)
{
if (empty($fields)) {
$fields = empty($fields_config) ? self::$get_fields : $fields_config;
}
if (!is_string($fields)) {
return array();
}
$fields_list = explode(',', $fields);
$result = array();
foreach ($fields_list as $val) {
$fields_key = explode('.', $val);
if (count($fields_key) == 2) {
list($parent_key, $son_key) = $fields_key;
if (isset($data[$parent_key]) && isset($data[$parent_key][$son_key])) {
$result[$parent_key][$son_key] = $data[$parent_key][$son_key];
}
} elseif (array_key_exists($val, $data)) {
$result[$val] = $data[$val];
}
}
return $result;
}
/**
* 结果数据
*
* @param Integer $code 状态码
* @param mixed $data 数据
* @param String $message 数据信息Tag
*
*/
static function result($code, $message = null, $data = null)
{
$dataString = json_encode($data);
//echo $code.'------'.$message;
return array(
'code' => $code,
'message' => $message,
'data' => $data,
'md5' => md5($dataString)
);
}
static function FormatCurrency($currency)
{
if (empty(self::$currencyObj)) {
self::$currencyObj = new Zend_Currency('zh_CN');
self::$currencyObj->setFormat(array(
'precision' => 0
));
}
return self::$currencyObj->toCurrency($currency);
}
/**
* 排序参数
* @param array $package
* @return array
*/
static function packageSort(array $package)
{
ksort($package);
reset($package);
return $package;
}
/**
* 组合签名
* @param array $package
* @return string
*/
static function makeSign(array $package)
{
$packageList = array();
foreach ($package as $key => $val) {
$packageList[] = trim(trim($key) . '=' . urldecode($val));
}
return strtolower(md5(implode('&', $packageList)));
}
/**
* 校验签名
* @param $submitSign
* @param $makeSign
* @return bool
*/
static function verifySign($submitSign, $makeSign)
{
return strtolower($submitSign) == strtolower($makeSign);
}
/**
* JSON输出
*
* @param String $caption
* @param Integer $code
* @param mixed $content
*/
static function echoJson($data = null)
{
header('Content-type: application/json');
echo json_encode($data);
exit();
}
static function paging()
{
return new Q_Paging();
}
static function getToken($uid)
{
return strrev(md5($uid . 'yohomark9646ghks'));
}
}