MpWxService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\service;
  13. use app\api\model\Wxapp as WxappModel;
  14. use think\cache\driver\Redis;
  15. class MpWxService extends BaseService
  16. {
  17. public static function delweixinTokenCache(){
  18. $key = 'weixin:token';
  19. $rds = new Redis(config('cache.stores.redis'));
  20. $rds->delete($key);
  21. }
  22. //weixin token
  23. //"{"access_token":"27_vQnXeiUakHGc462ipuJZuNpK45gOdNKflz7GYWT1cVyy1CC6fhDemrvfFQWX3gRaieYU8tOPcZV6qkSHjtQpuK3OhYEq5935HZUcVSinQC8Osls61X9gArFP1sMuyUtblnjBUYX_DoeK4enDGDTaAJAXUK","expires_in":7200}"
  24. public static function weixinTokenCache(){
  25. $key = 'weixin:token';
  26. $rds = new Redis(config('cache.stores.redis'));
  27. $tokenJson = $rds->get($key);
  28. $result = [];
  29. if($tokenJson){
  30. $result = json_decode($tokenJson,true);
  31. }
  32. else{
  33. $result = self::weixinToken();
  34. $expires_in = $result['expires_in'];
  35. $expires = $expires_in-7000;//设置一个比微信过期如7200更早的过期时间
  36. $rds->set($key,json_encode($result),$expires);
  37. }
  38. return $result;
  39. }
  40. //微信token
  41. public static function weixinToken(){
  42. // 获取当前小程序信息
  43. $wxConfig = WxappModel::find(10001);
  44. $params["grant_type"] = "client_credential";
  45. $params["appid"] = $wxConfig['app_id'];
  46. $params["secret"] = $wxConfig['app_secret'];
  47. $url = "https://api.weixin.qq.com/cgi-bin/token?";
  48. $url = $url.http_build_query($params);
  49. $ret = self::curl_request($url);
  50. $retArr = json_decode($ret,true);
  51. return $retArr;
  52. // $access_token = $retArr["access_token"];
  53. // $expires_in = $retArr["expires_in"];
  54. // $result = array($access_token,$expires_in);
  55. // return $result;
  56. }
  57. public static function curl_request($url,$post='',$cookie='', $returnCookie=0){
  58. $curl = curl_init();
  59. curl_setopt($curl, CURLOPT_URL, $url);
  60. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  61. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  62. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  63. //设置https
  64. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
  65. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
  66. if($post) {
  67. curl_setopt($curl, CURLOPT_POST, 1);
  68. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  69. }
  70. if($cookie) {
  71. curl_setopt($curl, CURLOPT_COOKIE, $cookie);
  72. }
  73. curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
  74. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  75. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  76. $data = curl_exec($curl);
  77. if (curl_errno($curl)) {
  78. return curl_error($curl);
  79. }
  80. curl_close($curl);
  81. if($returnCookie){
  82. list($header, $body) = explode("\r\n\r\n", $data, 2);
  83. preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
  84. $info['cookie'] = substr($matches[1][0], 1);
  85. $info['content'] = $body;
  86. return $info;
  87. }else{
  88. return $data;
  89. }
  90. }
  91. /**
  92. * 调用接口
  93. *
  94. * @param string $api_name 接口名称
  95. * @param array $params 请求参数
  96. * @return array|mixed
  97. */
  98. public static function doAnalysisAction($api_name, $params)
  99. {
  100. $weixinToken = MpWxService::weixinTokenCache();
  101. $accessToken = $weixinToken['access_token'];
  102. $url = "https://api.weixin.qq.com/datacube/{$api_name}?access_token={$accessToken}";
  103. $ret = curl_post($url, json_encode($params));
  104. $arr = json_decode($ret, true);
  105. if (isset($arr['errcode']) && $arr['errcode'] == 40001) {
  106. MpWxService::delweixinTokenCache();
  107. }
  108. return $arr;
  109. // $data = $arr['list'] ?? [];
  110. // return $data;
  111. }
  112. }