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() { $ptStyle = Yii::$app->params['ptStyle']; if (is_numeric($ptStyle) == false) { \common\components\util::fail('个推所属平台未明确'); } $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => $ptStyle], true); if (empty($open)) { \common\components\util::fail('个推所属平台没有找到'); } $expireDate = $open->gtTokenDeadTime ?? ''; $current = date("Y-m-d H:i:s"); if ($current < $expireDate) { $token = $open->gtToken ?? ''; $this->authToken = $token; //\common\components\noticeUtil::push("从数据库获取个推token", '15280215347'); return true; } $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) { $token = $rep["data"]["token"] ?? ''; $expireTime = $rep["data"]["expire_time"] ?? ''; $expireTime = bcdiv($expireTime, 1000); $expireTime = bcsub($expireTime, 3600); $expireDate = date("Y-m-d H:i:s", $expireTime); $open->gtToken = $token; $open->gtTokenDeadTime = $expireDate; $open->save(); //\common\components\noticeUtil::push("从接口获取个推token", '15280215347'); $this->authToken = $token; return true; } return false; } function getMicroTime() { list($usec, $sec) = explode(" ", microtime()); $time = ($sec . substr($usec, 2, 3)); return $time; } }