LoginService.php
6.5 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
181
182
183
184
185
186
<?php
/**
* class LoginService, provide PreLogin, DoLogin, DoLogout methods
*/
require_once('LoginConnection.php');
/**
* LoginService
*/
class LoginService {
/**
* @var string
*/
private $loginUrl;
/**
* @var string
*/
private $uuid;
/**
* construct
* @param string $loginUrl
* @param string $uuid
*/
public function __construct($loginUrl, $uuid) {
$this->loginUrl = $loginUrl;
$this->uuid = $uuid;
}
/**
* preLogin
* @param string $userName
* @param string $token
* @return boolean
*/
public function preLogin($userName, $token,$path) {
//echo '----------------------preLogin----------------------' . PHP_EOL;
//echo '[notice] start preLogin!' . PHP_EOL;
$preLogin = new LoginConnection();
$preLogin->init($this->loginUrl);
$preLoginData = array(
'username' => $userName,
'token' => $token,
'functionName' => 'preLogin',
'uuid' => $this->uuid,
'request' => array(
'osVersion' => 'windows',
'deviceType' => 'pc',
'clientVersion' => '1.0',
),
);
$preLogin->POST($preLoginData,$path);
if ($preLogin->returnCode === 0) {
$retData = gzdecode($preLogin->retData, strlen($preLogin->retData));
$retArray = json_decode($retData, true);
if (!isset($retArray['needAuthCode']) || $retArray['needAuthCode'] === true) {
//echo "[error] preLogin return data format error: {$retData}" . PHP_EOL;
// echo '--------------------preLogin End--------------------'. PHP_EOL;
return false;
}
else if ($retArray['needAuthCode'] === false) {
//echo '[notice] preLogin successfully!' . PHP_EOL;
//echo '--------------------preLogin End--------------------' . PHP_EOL;
return true;
}
else {
echo "[error] unexpected preLogin return data: {$retData}" . PHP_EOL;
echo '--------------------preLogin End--------------------' . PHP_EOL;
return false;
}
}
else {
//echo "[error] preLogin unsuccessfully with return code: {$preLogin->returnCode}" . PHP_EOL;
//echo '--------------------preLogin End--------------------' . PHP_EOL;
return false;
}
}
/**
* doLogin
* @param string $userName
* @param string $password
* @param string $token
* @return array
*/
public function doLogin($userName, $password, $token,$path) {
//echo '----------------------doLogin----------------------' . PHP_EOL;
//echo '[notice] start doLogin!' . PHP_EOL;
$doLogin = new LoginConnection();
$doLogin->init($this->loginUrl);
$doLoginData = array(
'username' => $userName,
'token' => $token,
'functionName' => 'doLogin',
'uuid' => $this->uuid,
'request' => array(
'password' => $password,
),
);
$doLogin->POST($doLoginData,$path);
if ($doLogin->returnCode === 0) {
$retData =gzinflate(substr($doLogin->retData,10,-8)); ;//gzdecode($doLogin->retData, strlen($doLogin->retData));
$retArray = json_decode($retData, true);
if (!isset($retArray['retcode']) || !isset($retArray['ucid']) || !isset($retArray['st'])) {
echo "[error] doLogin return data format error: {$retData}" . PHP_EOL;
echo '--------------------doLogin End--------------------' . PHP_EOL;
return null;
}
else if ($retArray['retcode'] === 0) {
//echo '[notice] doLogin successfully!' . PHP_EOL;
//echo '--------------------doLogin End--------------------' . PHP_EOL;
return array(
'ucid' => $retArray['ucid'],
'st' => $retArray['st'],
);
}
else {
echo "[error] doLogin unsuccessfully with retcode: {$retArray['retcode']}" . PHP_EOL;
echo '--------------------doLogin End--------------------' . PHP_EOL;
return null;
}
}
else {
echo "[error] doLogin unsuccessfully with return code: {$doLogin->returnCode}" . PHP_EOL;
echo '--------------------doLogin End--------------------' . PHP_EOL;
return null;
}
}
/**
* doLogout
* @param string $userName
* @param string $token
* @param string $ucid
* @param string $st
* @return boolean
*/
public function doLogout($userName, $token, $ucid, $st,$path) {
//echo '----------------------doLogout----------------------' . PHP_EOL;
//echo '[notice] start doLogout!' . PHP_EOL;
$doLogout = new LoginConnection();
$doLogout->init($this->loginUrl);
$doLogoutData = array(
'username' => $userName,
'token' => $token,
'functionName' => 'doLogout',
'uuid' => $this->uuid,
'request' => array(
'ucid' => $ucid,
'st' => $st,
),
);
$doLogout->POST($doLogoutData,$path);
if ($doLogout->returnCode === 0) {
$retData = gzdecode($doLogout->retData, strlen($doLogout->retData));
$retArray = json_decode($retData, true);
if (!isset($retArray['retcode'])) {
echo "[error] doLogout return data format error: {$retData}" . PHP_EOL;
echo '--------------------doLogout End--------------------' . PHP_EOL;
return false;
}
else if ($retArray['retcode'] === 0 ) {
//echo '[notice] doLogout successfully!' . PHP_EOL;
//echo '--------------------doLogout End--------------------' . PHP_EOL;
return true;
}
else {
//echo "[error] doLogout unsuccessfully with retcode: {$retArray['retcode']}" . PHP_EOL;
//echo '--------------------doLogout End--------------------' . PHP_EOL;
return false;
}
}
else {
echo "[error] doLogout unsuccessfully with return code: {$doLogout->returnCode}" . PHP_EOL;
echo '--------------------doLogout End--------------------' . PHP_EOL;
return false;
}
}
}