setLog(self::TYPENAME.'获取公众号 access_token 开始', $this->startTime()); $url = str_replace(['APPID', 'APPSECRET'], [env('MP_APPID'), env('MP_APPSECRET')], self::URL_ACCESS_TOKEN); $redisKey = config('console.redis_key.access_token'); $rData = Redis::GET($redisKey); if ($rData){ $this->setLog(self::TYPENAME.'获取公众号 access_token redis 得到', $this->startTime, [$rData]); return $this->response(['access_token' => $rData]); } try { $client = new httpClient(); $res = $client->request('get', $url, []); $body = $res->getBody(); $data = json_decode($body); $this->setLog(self::TYPENAME.'获取公众号 access_token 远程请求 得到', $this->startTime, [$data]); if (!isset($data->access_token)){ $this->setLog(self::TYPENAME.'getAccessToken 失败 没有找到access_token', $this->startTime, [$data]); $this->error()->fail(); }else{ Redis::SET($redisKey, $data->access_token); Redis::EXPIRE($redisKey, $data->expires_in); return $this->response($data); } } catch (\Exception $e) { $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } public function getUserInfo(string $openid){ $accessTokenRes = $this->getAccessToken(); $accessToken = $accessTokenRes->getData()->data->access_token; $url = str_replace(['ACCESS_TOKEN', 'OPENID'], [$accessToken, $openid], self::URL_USER_INFO); $this->setLog(self::TYPENAME.'getUserInfo 组合的URL:', $this->startTime, ['url'=>$url]); try { $client = new httpClient(); $res = $client->request('get', $url, []); $body = $res->getBody(); $data = json_decode($body); if (!isset($data->openid)){ $this->setLog(self::TYPENAME.'getUserInfo 失败 没有找到openid', $this->startTime, [$data]); $this->error()->fail(); }else{ return $this->response($data); } } catch (\Exception $e) { $this->setLog(self::TYPENAME.'getUserInfo 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } public function checkSignature(string $signature, string $echostr, string $timestamp, string $nonce) { $token = env('MP_TOKEN'); $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return $echostr; }else{ return false; } } public function getJsapiTicket() { $redisKey = config('console.redis_key.jsapi_ticket'); $rData = Redis::GET($redisKey); if ($rData){ return $this->response(['ticket' => $rData]); } $accessTokenRes = $this->getAccessToken()->getData(); if ($accessTokenRes->code != self::SUCCESS_CODE){ return $accessTokenRes; } $accessToken = $accessTokenRes->data->access_token; $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_JSAPI_TICKET); try { $client = new httpClient(); $res = $client->request('get', $url, []); $body = $res->getBody(); $data = json_decode($body); if (!isset($data->ticket)){ $this->setLog(self::TYPENAME.'getJsapiTicket 失败 没有找到ticket', $this->startTime, [$data]); return $this->error()->fail(); }else{ Redis::SET($redisKey, $data->ticket); Redis::EXPIRE($redisKey, $data->expires_in); return $this->response($data); } } catch (\Exception $e) { $this->setLog(self::TYPENAME.'getJsapiTicket 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } public function getJsapiSign(string $url) { $jsapiTicketRes = $this->getJsapiTicket()->getData(); if ($jsapiTicketRes->code != self::SUCCESS_CODE){ return $jsapiTicketRes; } $ticket = $jsapiTicketRes->data->ticket; $arrs = [ 'noncestr' => uniqid(), 'jsapi_ticket' => $ticket, 'timestamp' => time(), 'url' => $url, ]; ksort($arrs); $resultArr = []; foreach ($arrs as $k=>$v){ $resultArr[] = $k.'='.$v; } $signStr = implode('&', $resultArr); $arrs['sign'] = sha1($signStr); return $this->response($arrs); } public function sendTplMsg(string $touser, string $templateId, string $jumpUrl, array $data) { //请求的url $accessTokenRes = $this->getAccessToken()->getData(); if ($accessTokenRes->code != self::SUCCESS_CODE){ return $accessTokenRes; } $accessToken = $accessTokenRes->data->access_token; $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_SEND_TPL_MSG); $postData = [ 'touser' => $touser, 'template_id' => $templateId, 'url' => $jumpUrl, 'data' => $data ]; try { $client = new httpClient(); $res = $client->request('POST', $url, ['body' => json_encode($postData, JSON_UNESCAPED_UNICODE), 'headers' => ['content-type' => 'application/json']]); $body = $res->getBody(); $data = json_decode($body); return $this->response($data); } catch (\Exception $e) { $this->setLog(self::TYPENAME.'发送模板信息 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } public function sendMessage(string $toUser, string $msgType, array $msg) { $this->setLog(self::TYPENAME.'sendMessage', $this->startTime, ['toUser'=>$toUser, 'msgType'=>$msgType, 'msg'=>$msg]); //请求的url $accessTokenRes = $this->getAccessToken()->getData(); if ($accessTokenRes->code != self::SUCCESS_CODE){ return $accessTokenRes; } $accessToken = $accessTokenRes->data->access_token; $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_SEND_MESSAGE); $postData = [ 'touser' => $toUser, 'msgtype' => $msgType, 'text' => $msg ]; try { $client = new httpClient(); $res = $client->request('POST', $url, ['body' => json_encode($postData, JSON_UNESCAPED_UNICODE), 'headers' => ['content-type' => 'application/json']]); $body = $res->getBody(); $data = json_decode($body); $this->setLog(self::TYPENAME.'sendMessage 返回结果', $this->startTime, [$data]); return $this->response($data); } catch (\Exception $e) { $this->setLog(self::TYPENAME.'发送信息 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } public function createMenu(array $menu) { $accessTokenRes = $this->getAccessToken()->getData(); if ($accessTokenRes->code != self::SUCCESS_CODE){ return $accessTokenRes; } $accessToken = $accessTokenRes->data->access_token; $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_SEND_CREATE_MENU); try { $client = new httpClient(); $res = $client->request('POST', $url, ['body' => json_encode($menu, JSON_UNESCAPED_UNICODE), 'headers' => ['content-type' => 'application/json']]); $body = $res->getBody(); $data = json_decode($body); return $this->response($data); } catch (\Exception $e) { $this->setLog(self::TYPENAME.'发送信息 远程请求失败', $this->startTime, [$e->getMessage()]); $this->error()->fail(); } } }