/** * 得到入口令 * * @author xjiujiu <*****@*****.**> * @access public */ public function requestAccessToken($identifier = 'wx-access-token') { $staticCfg = HClass::quickLoadModel('staticcfg'); $record = $staticCfg->getRecordByIdentifier($identifier); if ($record) { $json = json_decode(HString::decodeHtml($record['content']), true); if ($json['end_time'] > $_SERVER['REQUEST_TIME']) { $this->_accessToken = $json['access_token']; return $this; } } $json = HRequest::getContents(strtr(self::$_urlMap['access_token'], array('{appid}' => $this->_appid, '{secret}' => $this->_secret))); $json = json_decode($json, true); if (isset($json['errcode']) && 0 < $json['errcode']) { throw new HRequestException($json['errmsg']); } $json['end_time'] = time() + $json['expires_in']; $data = array('name' => '微信口令ACCESS-TOKEN', 'identifier' => $identifier, 'content' => json_encode($json), 'author' => HSession::getAttribute('id', 'user')); if ($record) { $staticCfg->editByWhere($data, '`id` = ' . $record['id']); } else { $staticCfg->add($data); } $this->_accessToken = $json['access_token']; return $this; }
public function actionIndex() { $access_token = HSession::get('access_token'); $this->render(array('access_token' => $access_token)); }