123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace App\Repositories\Eloquent;
- use GuzzleHttp\Client as httpClient;
- use Illuminate\Support\Facades\Redis;
- use App\Repositories\Contracts\WxServicesInterface;
- /**
- *
- * @author lilin
- *
- */
- class WxServiceFacadeRepository extends BaseRepository implements WxServicesInterface
- {
- public function getAccessToken()
- {
- $this->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();
- }
-
- }
- }
|