AbstractController.class.php
6.37 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Created by PhpStorm.
* User: zhoutao
* Date: 2018/1/8
* Time: 下午3:54
*/
namespace Apicp\Controller\LiveRoom;
use Com\Cookie;
use Com\Error;
use Common\Common\Cache;
use Common\Service\MainService;
use Common\Service\StudioService;
use Think\Cache\Driver\Redis;
use Think\Controller\RestController;
use Think\Exception;
use VcySDK\Service;
class AbstractController extends RestController
{
/**
* 管理员身份标识存活时长 (秒)
*/
const ADMINER_ID_EXPIRE_TIME = 15;
/**
* 返回结果
*
* @var array
*/
protected $_result = array();
/**
* 是否验证登陆
* @var bool
*/
protected $requireLogin = true;
/**
* cookie
*
* @type null|Cookie
*/
protected $_cookie = null;
/**
* 直播活动详情
* @var null
*/
protected $liveMainDetail = null;
/**
* 直播室详情
* @var null
*/
protected $studioDetail = null;
public function before_action()
{
// 获取直播活动详情
$lmId = I('post.lm_id', 0, 'rintval');
if (empty($lmId)) {
E('_EMPTY_LIVE_ID');
}
$mainServ = new MainService();
$this->liveMainDetail = $mainServ->get($lmId, true);
if (empty($this->liveMainDetail)) {
E('_ERR_LIVE_IS_NOT_EXIST');
}
// 获取直播室详情
$studioServ = new StudioService();
$this->studioDetail = $studioServ->get_by_conds(['lm_id' => $lmId], [], true);
if (empty($this->studioDetail)) {
E('_ERR_MISS_LIVE_STUDIO_DATA');
}
// 延续 Cookie
$this->continueCookie($this->liveMainDetail['domain']);
// 验证 token
$this->validateLoginLiveRoom($this->studioDetail['stream_id']);
// 初始化SDK
$cache = &Cache::instance();
$this->config = $cache->get('Common.EnterpriseConfig', '', ['prefix' => $this->liveMainDetail['domain'] . '_']);
$config = array(
'apiUrl' => cfg('UC_APIURL'),
'enumber' => $this->liveMainDetail['domain'],
'appid' => $this->config['wxqyCorpid'],
'pluginIdentifier' => APP_IDENTIFIER,
'thirdIdentifier' => cfg('SDK_THIRD_IDENTIFIER'),
'logPath' => RUNTIME_PATH . '/Logs/VcySDK/',
'apiSecret' => cfg('API_SECRET'),
'apiSigExpire' => cfg('API_SIG_EXPIRE'),
'fileConvertApiUrl' => cfg('FILE_CONVERT_API_URL')
);
$service = &Service::instance();
$service->initSdk($config);
return true;
}
/**
* 延续 Cookie (不然用户还在后台的话 回去会丢失 Cookie)
* @param $domain
*/
public function continueCookie($domain)
{
// 初始化 Cookie
$cookieDomain = cfg('COOKIE_DOMAIN');
$expired = cfg('COOKIE_EXPIRE');
$secret = md5(cfg('COOKIE_SECRET') . $domain);
$this->_cookie = &Cookie::instance($cookieDomain, $expired, $secret);
ob_start(array($this->_cookie, 'send'));
}
/**
* 后置操作
*
* @param string $action 操作名称
*
* @return bool
*/
public function after_action($action = '')
{
$this->_response();
return true;
}
/**
* 重写输出方法
*
* @param $data
* @param $type
* @param $code
*
* @return bool
*/
public function response($data, $type = 'json', $code = 200)
{
$this->_response($data, $type, $code);
return true;
}
/**
* 验证token
*
* @param int $stream_id 直播推流ID
*
* @return bool
*/
public function validateLoginLiveRoom($stream_id)
{
// 不需登陆
if (!$this->requireLogin) {
return true;
}
// 获取cookie中的token
$cookie_token = $this->_cookie->getx($stream_id);
// 获取redis中的token值
$redisServ = new Redis();
$redis_token = $redisServ->get($stream_id);
if (!empty($cookie_token)) {
// 判断token是否相等
if ($cookie_token == $redis_token) {
// 更新 redis 过期时间
$redisServ->set($stream_id, $redis_token, self::ADMINER_ID_EXPIRE_TIME);
// 更新 Cookie 过期时间
$this->_cookie->setx($stream_id, $redis_token, self::ADMINER_ID_EXPIRE_TIME);
} else {
// 登陆超时重新登录 清理 Cookie
$this->_cookie->remove($this->studioDetail['stream_id']);
E('_ERR_LOGIN_TIME_OUT');
}
} else {
if (!empty($redis_token)) {
// 别的地方登陆了
E('_ERR_OTHER_PLACES_LANDED');
} else {
// 请登录
E('_ERR_PLEASE_LOGIN');
}
}
return true;
}
/**
* 重写输出方法
*
* @param mixed $data 输出数据
* @param string $type 输出类型
* @param int $code 返回状态
*
* @see \Think\Controller\RestController::_response()
*/
protected function _response($data = null, $type = 'json', $code = 200)
{
// 如果需要返回的是异常
if ($data instanceof Exception) {
// 如果是显示给用户的错误
if ($data->is_show()) {
Error::instance()->set_error($data);
} else {
// 如果是系统错误, 则显示默认错误
$this->_set_error('_ERR_DEFAULT');
}
$data = '';
} elseif ($data instanceof \Exception) {
$code = $data->getCode();
$message = $data->getMessage();
if (! empty($message)) {
Error::instance()->set_error($data);
} else {
// 系统报错
$data = '';
$this->_set_error('_ERR_DEFAULT');
}
}
// 输出结果
parent::_response(generate_api_result(null == $data ? $this->_result : $data), $type, $code);
}
/**
* 设置错误信息
*
* @param mixed $message 错误信息
* @param int $code 错误号
*
* @return bool
*/
protected function _set_error($message, $code = 0)
{
Error::instance()->set_error($message, $code);
return true;
}
}