Exemple #1
0
 /**
  * 获取component_access_token
  *
  * @return void
  * @author
  **/
 public function getComponentAccessToken($text = null)
 {
     $data = $this->cache->get("niancode/wechat/getComponentAccessToken");
     if ($data === false) {
         $url = self::WECHAT_BASE_URL . "cgi-bin/component/api_component_token";
         $post = ['component_appid' => $this->config->appId, 'component_appsecret' => $this->config->appSecret, 'component_verify_ticket' => $this->getVerifyTicket($text, false)];
         $request = Http::post($url, ['json' => $post]);
         $response = $request->json();
         if (array_key_exists('component_access_token', $response)) {
             $res = $request->json(['object' => true]);
             $access_token = $res->component_access_token;
             if ($access_token) {
                 // 写入日志
                 try {
                     $info['component_access_token'] = $access_token;
                     $info['verify_ticket'] = $text;
                     $new_text = sprintf("%s >>> %s\n", date('Y-m-d H:i:s'), var_export($info, true));
                     file_put_contents(Yii::getAlias('@runtime/logs/platform_access_token.log'), $new_text, FILE_APPEND);
                 } catch (ErrorException $e) {
                     Yii::warning("Token日志写入错误");
                 }
                 $data = new \stdClass();
                 $data->access_token = $access_token;
                 $this->cache->set("niancode/wechat/getComponentAccessToken", $data, 6000);
                 return $access_token;
             }
         }
     } else {
         return $data->access_token;
     }
 }