/** * 获取access_token 字符串 * @return string */ public function access_token() { $token_file = FileStatic::mkDir(APPPATH . 'cache/wechat') . $this->appid . '.json'; $get = FALSE; // 是否需要再次远程获取 if (!file_exists($token_file)) { $get = TRUE; } else { $json = json_decode(file_get_contents($token_file)); if ($json->expires < time()) { $get = TRUE; } else { return $json->access_token; } } if ($get) { $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s'; $json = json_decode($this->curlGet(sprintf($url, $this->appid, $this->appSecret))); if (!empty($json->errcode)) { // 获取失败直接停止 die('get access_token fail!'); } $data = array('expires' => time() + $json->expires_in, 'access_token' => $json->access_token); file_put_contents($token_file, json_encode($data)); return $json->access_token; } return ''; }
protected function accessToken() { $token_file = FileStatic::mkDir(APPPATH . 'cache/wechat') . $this->CorpID . '.json'; if (file_exists($token_file) && !empty($json = json_decode(file_get_contents($token_file))) && $json->expires > time()) { return $json->access_token; } $url = sprintf('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s', $this->CorpID, $this->Secret); $json = json_decode($this->curlGet($url)); if (!empty($json->errcode)) { // 获取失败直接停止 die('get access_token fail!'); } $data = ['expires' => time() + $json->expires_in, 'access_token' => $json->access_token]; file_put_contents($token_file, json_encode($data)); return $json->access_token; }
/** * 获取JS票据 * @param string $access_token * @return string */ public function jsapi_ticket($access_token) { // 从没有获取过access_token或者已经过期 $path = FileStatic::cache_file(); if (file_exists($path)) { $ticket = json_decode(file_get_contents($path)); if ($ticket->expires > time()) { return $ticket->ticket; } } $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi'; $json = json_decode($this->curlGet(sprintf($url, $access_token))); if ($json->errcode) { die('get jsapi_ticket fail!'); } else { $ticket = array('expires' => time() + $json->expires_in, 'ticket' => $json->ticket); file_put_contents($path, json_encode($ticket)); return $json->ticket; } return ''; }
private function filePath($level = LogLevel::ERROR) { return FileStatic::mkdir('logs/' . $level, 1) . date('Y-m-d') . '.log'; }
/** * 缓存文件夹结构规则 * @param string $uri * @return string */ protected function cache_dir_rule() { return FileStatic::mkdir(APPPATH . "cache/{$this->cache_path}/{$this->cmdq->d}/{$this->cmdq->c}"); }