IndexController.class.php
4.54 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
<?php
/**
* 微信安全支持 扫描
* 检测 Cookie 统一跳转
*/
namespace Frontend\Controller\Safe;
use Com\Cookie;
use Common\Common\Login;
use Common\Common\User;
use Think\Controller;
use VcySDK\Service;
class IndexController extends Controller
{
/**
* 接收的数据
* @var array
*/
protected $postData = [];
/**
* 企业详情
* @var array
*/
private $epDetail = [];
/**
* cookie 实例
* @var null
*/
private $cookie = null;
public function Index()
{
// 获取企业信息
$this->epDetail = $this->getEpDetail();
// 初始化 Cookie
$this->startCookie();
// 初始化 SDK
$this->initSdk();
// 获取 Cookie
$login = &Login::instance();
$uid = $login->getCookie('uid');
$auth = $login->getCookie('auth');
$lastlogin = $login->getCookie('lastlogin');
$qydomain = $login->getCookie('qyDomain');
// 如果 Cookie 值为空
if (empty($uid) || empty($auth) || empty($lastlogin) ||
// 访问企业和 Cookie 不一样
(strtolower($qydomain) != strtolower($this->epDetail['epEnumber']))) {
$this->returnError('未找到人员');
}
// 获取人员
$userServ = &User::instance();
$user = $userServ->getByUid($uid, true);
// 未找到用户或用户已删除
if (empty($user) || !$userServ->isNormal($user)) {
$this->cookie->destroy();
return false;
}
// 跳转前端首页
$this->redirectFront('/app/page/index/index/index');
}
/**
* corpId 获取企业详情
* @return array|mixed
*/
private function getEpDetail()
{
if (empty($_COOKIE)) {
$this->returnError('非法请求');
}
try {
// 不是常规调用 UC 接口
$sdkService = new Service();
$response = [];
$sdkService->request($response, cfg('UC_APIURL') . '/s/enterprise/detail', [
'corpId' => $_COOKIE['corpid'],
], [
'Content-Type' => 'application/json',
], 'POST');
} catch (\Exception $e) {
$this->returnError('未找到企业');
}
return $response['data'];
}
/**
* 初始化 Cookie
*/
private function startCookie()
{
$domain = cfg('COOKIE_DOMAIN');
$expired = cfg('COOKIE_EXPIRE');
$secret = md5(cfg('COOKIE_SECRET') . $this->epDetail['epEnumber']);
// 初始化
$this->cookie = &Cookie::instance($domain, $expired, $secret);
ob_start([
$this->cookie,
'send',
]);
}
/**
* 初始化 SDk
* @return bool
*/
private function initSdk()
{
$config = array(
'apiUrl' => cfg('UC_APIURL'),
'enumber' => $this->epDetail['epEnumber'],
'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;
}
/**
* 跳转至前端Url
*
* @author zhonglei
*
* @param string $pageUrl 前端地址(#号之后的页面地址,不包含参数)
* @param array $params 参数
* @param string $appdir 应用目录,默认为当前目录
*
* @return void
*/
private function redirectFront($pageUrl, $params = [], $appdir = '')
{
if (!isset($params['ts'])) {
$params['ts'] = MILLI_TIME;
}
if (cfg('STATIC_URL_DEBUG') === false) {
unset($params['ts']);
}
if (empty($appdir)) {
$appdir = APP_DIR;
}
$frontPath = cfg('FRONTEND_PATH');
$url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];
$url .= sprintf(
'/%s/%s/%s/index.html#%s?%s',
$this->epDetail['epEnumber'],
$appdir,
$frontPath,
$pageUrl,
http_build_query($params)
);
redirect($url);
}
/**
* 返回错误
* @param $errmsg
*/
protected function returnError($errmsg)
{
$this->_response(['errmsg' => $errmsg], 'json');
}
}