WxServiceFacadeRepository.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use GuzzleHttp\Client as httpClient;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\Repositories\Contracts\WxServicesInterface;
  6. /**
  7. *
  8. * @author lilin
  9. *
  10. */
  11. class WxServiceFacadeRepository extends BaseRepository implements WxServicesInterface
  12. {
  13. public function getAccessToken()
  14. {
  15. $this->setLog(self::TYPENAME.'获取公众号 access_token 开始', $this->startTime());
  16. $url = str_replace(['APPID', 'APPSECRET'], [env('MP_APPID'), env('MP_APPSECRET')], self::URL_ACCESS_TOKEN);
  17. $redisKey = config('console.redis_key.access_token');
  18. $rData = Redis::GET($redisKey);
  19. if ($rData){
  20. $this->setLog(self::TYPENAME.'获取公众号 access_token redis 得到', $this->startTime, [$rData]);
  21. return $this->response(['access_token' => $rData]);
  22. }
  23. try {
  24. $client = new httpClient();
  25. $res = $client->request('get', $url, []);
  26. $body = $res->getBody();
  27. $data = json_decode($body);
  28. $this->setLog(self::TYPENAME.'获取公众号 access_token 远程请求 得到', $this->startTime, [$data]);
  29. if (!isset($data->access_token)){
  30. $this->setLog(self::TYPENAME.'getAccessToken 失败 没有找到access_token', $this->startTime, [$data]);
  31. $this->error()->fail();
  32. }else{
  33. Redis::SET($redisKey, $data->access_token);
  34. Redis::EXPIRE($redisKey, $data->expires_in);
  35. return $this->response($data);
  36. }
  37. } catch (\Exception $e) {
  38. $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]);
  39. $this->error()->fail();
  40. }
  41. }
  42. public function getUserInfo(string $openid){
  43. $accessTokenRes = $this->getAccessToken();
  44. $accessToken = $accessTokenRes->getData()->data->access_token;
  45. $url = str_replace(['ACCESS_TOKEN', 'OPENID'], [$accessToken, $openid], self::URL_USER_INFO);
  46. $this->setLog(self::TYPENAME.'getUserInfo 组合的URL:', $this->startTime, ['url'=>$url]);
  47. try {
  48. $client = new httpClient();
  49. $res = $client->request('get', $url, []);
  50. $body = $res->getBody();
  51. $data = json_decode($body);
  52. if (!isset($data->openid)){
  53. $this->setLog(self::TYPENAME.'getUserInfo 失败 没有找到openid', $this->startTime, [$data]);
  54. $this->error()->fail();
  55. }else{
  56. return $this->response($data);
  57. }
  58. } catch (\Exception $e) {
  59. $this->setLog(self::TYPENAME.'getUserInfo 远程请求失败', $this->startTime, [$e->getMessage()]);
  60. $this->error()->fail();
  61. }
  62. }
  63. public function checkSignature(string $signature, string $echostr, string $timestamp, string $nonce)
  64. {
  65. $token = env('MP_TOKEN');
  66. $tmpArr = array($token, $timestamp, $nonce);
  67. sort($tmpArr, SORT_STRING);
  68. $tmpStr = implode( $tmpArr );
  69. $tmpStr = sha1( $tmpStr );
  70. if( $tmpStr == $signature ){
  71. return $echostr;
  72. }else{
  73. return false;
  74. }
  75. }
  76. public function getJsapiTicket()
  77. {
  78. $redisKey = config('console.redis_key.jsapi_ticket');
  79. $rData = Redis::GET($redisKey);
  80. if ($rData){
  81. return $this->response(['ticket' => $rData]);
  82. }
  83. $accessTokenRes = $this->getAccessToken()->getData();
  84. if ($accessTokenRes->code != self::SUCCESS_CODE){
  85. return $accessTokenRes;
  86. }
  87. $accessToken = $accessTokenRes->data->access_token;
  88. $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_JSAPI_TICKET);
  89. try {
  90. $client = new httpClient();
  91. $res = $client->request('get', $url, []);
  92. $body = $res->getBody();
  93. $data = json_decode($body);
  94. if (!isset($data->ticket)){
  95. $this->setLog(self::TYPENAME.'getJsapiTicket 失败 没有找到ticket', $this->startTime, [$data]);
  96. return $this->error()->fail();
  97. }else{
  98. Redis::SET($redisKey, $data->ticket);
  99. Redis::EXPIRE($redisKey, $data->expires_in);
  100. return $this->response($data);
  101. }
  102. } catch (\Exception $e) {
  103. $this->setLog(self::TYPENAME.'getJsapiTicket 远程请求失败', $this->startTime, [$e->getMessage()]);
  104. $this->error()->fail();
  105. }
  106. }
  107. public function getJsapiSign(string $url)
  108. {
  109. $jsapiTicketRes = $this->getJsapiTicket()->getData();
  110. if ($jsapiTicketRes->code != self::SUCCESS_CODE){
  111. return $jsapiTicketRes;
  112. }
  113. $ticket = $jsapiTicketRes->data->ticket;
  114. $arrs = [
  115. 'noncestr' => uniqid(),
  116. 'jsapi_ticket' => $ticket,
  117. 'timestamp' => time(),
  118. 'url' => $url,
  119. ];
  120. ksort($arrs);
  121. $resultArr = [];
  122. foreach ($arrs as $k=>$v){
  123. $resultArr[] = $k.'='.$v;
  124. }
  125. $signStr = implode('&', $resultArr);
  126. $arrs['sign'] = sha1($signStr);
  127. return $this->response($arrs);
  128. }
  129. public function sendTplMsg(string $touser, string $templateId, string $jumpUrl, array $data)
  130. {
  131. //请求的url
  132. $accessTokenRes = $this->getAccessToken()->getData();
  133. if ($accessTokenRes->code != self::SUCCESS_CODE){
  134. return $accessTokenRes;
  135. }
  136. $accessToken = $accessTokenRes->data->access_token;
  137. $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_SEND_TPL_MSG);
  138. $postData = [
  139. 'touser' => $touser,
  140. 'template_id' => $templateId,
  141. 'url' => $jumpUrl,
  142. 'data' => $data
  143. ];
  144. try {
  145. $client = new httpClient();
  146. $res = $client->request('POST', $url, ['body' => json_encode($postData, JSON_UNESCAPED_UNICODE), 'headers' => ['content-type' => 'application/json']]);
  147. $body = $res->getBody();
  148. $data = json_decode($body);
  149. return $this->response($data);
  150. } catch (\Exception $e) {
  151. $this->setLog(self::TYPENAME.'发送模板信息 远程请求失败', $this->startTime, [$e->getMessage()]);
  152. $this->error()->fail();
  153. }
  154. }
  155. public function sendMessage(string $toUser, string $msgType, array $msg)
  156. {
  157. $this->setLog(self::TYPENAME.'sendMessage', $this->startTime, ['toUser'=>$toUser, 'msgType'=>$msgType, 'msg'=>$msg]);
  158. //请求的url
  159. $accessTokenRes = $this->getAccessToken()->getData();
  160. if ($accessTokenRes->code != self::SUCCESS_CODE){
  161. return $accessTokenRes;
  162. }
  163. $accessToken = $accessTokenRes->data->access_token;
  164. $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_SEND_MESSAGE);
  165. $postData = [
  166. 'touser' => $toUser,
  167. 'msgtype' => $msgType,
  168. 'text' => $msg
  169. ];
  170. try {
  171. $client = new httpClient();
  172. $res = $client->request('POST', $url, ['body' => json_encode($postData, JSON_UNESCAPED_UNICODE), 'headers' => ['content-type' => 'application/json']]);
  173. $body = $res->getBody();
  174. $data = json_decode($body);
  175. $this->setLog(self::TYPENAME.'sendMessage 返回结果', $this->startTime, [$data]);
  176. return $this->response($data);
  177. } catch (\Exception $e) {
  178. $this->setLog(self::TYPENAME.'发送信息 远程请求失败', $this->startTime, [$e->getMessage()]);
  179. $this->error()->fail();
  180. }
  181. }
  182. public function createMenu(array $menu)
  183. {
  184. $accessTokenRes = $this->getAccessToken()->getData();
  185. if ($accessTokenRes->code != self::SUCCESS_CODE){
  186. return $accessTokenRes;
  187. }
  188. $accessToken = $accessTokenRes->data->access_token;
  189. $url = str_replace(['ACCESS_TOKEN'], [$accessToken], self::URL_SEND_CREATE_MENU);
  190. try {
  191. $client = new httpClient();
  192. $res = $client->request('POST', $url, ['body' => json_encode($menu, JSON_UNESCAPED_UNICODE), 'headers' => ['content-type' => 'application/json']]);
  193. $body = $res->getBody();
  194. $data = json_decode($body);
  195. return $this->response($data);
  196. } catch (\Exception $e) {
  197. $this->setLog(self::TYPENAME.'发送信息 远程请求失败', $this->startTime, [$e->getMessage()]);
  198. $this->error()->fail();
  199. }
  200. }
  201. }