-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGTClient.php
162 lines (143 loc) · 5.55 KB
/
GTClient.php
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
<?php
require_once(dirname(__FILE__) . '/' . 'utils/GTHttpManager.php');
require_once(dirname(__FILE__) . '/' . 'utils/GTConfig.php');
require_once(dirname(__FILE__) . '/' . 'request/user/GTAliasRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/auth/GTAuthRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/user/GTTagSetRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/user/GTBadgeSetRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/user/GTUserQueryRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTPushRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTPushBatchRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTAudienceRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTSettings.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTStrategy.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTNotification.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTRevoke.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTPushChannel.php');
require_once(dirname(__FILE__) . '/' . 'request/push/ios/GTIos.php');
require_once(dirname(__FILE__) . '/' . 'request/push/ios/GTAps.php');
require_once(dirname(__FILE__) . '/' . 'request/push/ios/GTAlert.php');
require_once(dirname(__FILE__) . '/' . 'request/push/ios/GTMultimedia.php');
require_once(dirname(__FILE__) . '/' . 'request/GTApiRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/android/GTAndroid.php');
require_once(dirname(__FILE__) . '/' . 'request/push/android/GTUps.php');
require_once(dirname(__FILE__) . '/' . 'request/push/android/GTThirdNotification.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTCondition.php');
require_once(dirname(__FILE__) . '/' . 'request/user/GTUserQueryRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTPushRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTPushBatchRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTAudienceRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/user/GTTagBatchSetRequest.php');
require_once(dirname(__FILE__) . '/' . 'request/push/GTCondition.php');
require_once(dirname(__FILE__) . '/' . 'GTPushApi.php');
require_once(dirname(__FILE__) . '/' . 'GTStatisticsApi.php');
require_once(dirname(__FILE__) . '/' . 'GTPushApi.php');
require_once(dirname(__FILE__) . '/' . 'GTUserApi.php');
/**
* 个推客户端,用于进行推送、用户设置、报表查询等操作
* 每new一个GTclient对象,会进行一次鉴权,建议用户保存GTclient对象重复使用。
**/
class GTClient
{
//第三方 标识
private $appkey;
//第三方 密钥
private $masterSecret;
//鉴权token
private $authToken;
//第三方 appid
private $appId;
//是否使用https连接 以该标志为准
private $useSSL = null;
//用户配置或者内置域名列表
private $domainUrlList = null;
//是否指定域名。如果指定,使用过程中域名不会发生变化
private $isAssigned = false;
//推送api
private $pushApi = null;
//报表api
private $statisticsApi = null;
//用户api
private $userApi = null;
public function __construct($domainUrl, $appkey, $appId, $masterSecret, $ssl = NULL)
{
$this->appkey = $appkey;
$this->masterSecret = $masterSecret;
$this->appId = $appId;
$this->pushApi = new GTPushApi($this);
$this->statisticsApi = new GTStatisticsApi($this);
$this->userApi = new GTUserApi($this);
$domainUrl = trim($domainUrl);
if ($ssl == NULL && $domainUrl != NULL && strpos(strtolower($domainUrl), "https:") === 0) {
$ssl = true;
}
$this->useSSL = ($ssl == NULL ? false : $ssl);
if ($domainUrl == NULL || strlen($domainUrl) == 0) {
$this->domainUrlList = GTConfig::getDefaultDomainUrl($this->useSSL);
} else {
if (GTConfig::isNeedOSAsigned()) {
$this->isAssigned = true;
}
$this->domainUrlList = array($domainUrl);
}
//鉴权
try {
$this->auth();
} catch (Exception $e) {
echo $e->getMessage();
}
}
public function getAuthToken()
{
return $this->authToken;
}
public function setAuthToken($authToken)
{
$this->authToken = $authToken;
}
public function pushApi()
{
return $this->pushApi;
}
public function statisticsApi()
{
return $this->statisticsApi;
}
public function userApi()
{
return $this->userApi;
}
public function getHost()
{
return $this->domainUrlList[0];
}
public function setDomainUrlList($domainUrlList)
{
$this->domainUrlList = $domainUrlList;
}
public function getAppId()
{
return $this->appId;
}
public function auth()
{
$auth = new GTAuthRequest();
$auth->setAppkey($this->appkey);
$timeStamp = $this->getMicroTime();
$sign = hash("sha256", $this->appkey . $timeStamp . $this->masterSecret);
$auth->setSign($sign);
$auth->setTimestamp($timeStamp);
$rep = $this->userApi()->auth($auth);
if ($rep["code"] == 0) {
$this->authToken = $rep["data"]["token"];
return true;
}
return false;
}
function getMicroTime()
{
list($usec, $sec) = explode(" ", microtime());
$time = ($sec . substr($usec, 2, 3));
return $time;
}
}