Wechat.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\model;
  13. use think\cache\driver\Redis;
  14. use app\common\model\Wxapp;
  15. use think\facade\Cache;
  16. use app\common\exception\BaseException;
  17. use think\facade\Log;
  18. class Wechat {
  19. //返回配置 // 小程序id
  20. public static function getConfig($storeId = null){
  21. $data = Wxapp::getWxappCache();
  22. return ['app_id'=>$data['app_id'],'app_secret'=>$data['app_secret']];
  23. }
  24. //weixin token
  25. //"{"access_token":"27_vQnXeiUakHGc462ipuJZuNpK45gOdNKflz7GYWT1cVyy1CC6fhDemrvfFQWX3gRaieYU8tOPcZV6qkSHjtQpuK3OhYEq5935HZUcVSinQC8Osls61X9gArFP1sMuyUtblnjBUYX_DoeK4enDGDTaAJAXUK","expires_in":7200}"
  26. public function weixinTokenCache(){
  27. $key = 'ysc:weixin:token';
  28. $rds = new Redis(config('cache.stores.redis'));
  29. $tokenJson = $rds->get($key);
  30. $result = [];
  31. if($tokenJson){
  32. $result = json_decode($tokenJson,true);
  33. }
  34. else{
  35. $result = $this->weixinToken();
  36. $expires_in = $result['expires_in'];
  37. $expires = $expires_in - 7000;//设置一个比微信过期如7200更早的过期时间
  38. $rds->set($key,json_encode($result),$expires);
  39. }
  40. return $result;
  41. }
  42. //微信token
  43. public function weixinToken(){
  44. $config = $this->getConfig();
  45. $params["grant_type"] = "client_credential";
  46. $params["appid"] = $config['app_id'];
  47. $params["secret"] = $config['app_secret'];
  48. $url = "https://api.weixin.qq.com/cgi-bin/token?";
  49. $url = $url.http_build_query($params);
  50. $ret = $this->curl_request($url);
  51. $retArr = json_decode($ret,true);
  52. return $retArr;
  53. }
  54. private function getHttpArray($url,$post_data) {
  55. $ch = curl_init();
  56. curl_setopt($ch, CURLOPT_URL, $url);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  58. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //没有这个会自动输出,不用print_r();也会在后面多个1
  59. curl_setopt($ch, CURLOPT_POST, 1);
  60. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  61. $output = curl_exec($ch);
  62. curl_close($ch);
  63. $out = json_decode($output);
  64. return $out;
  65. }
  66. //GET
  67. public function curl_request($url,$post='',$cookie='', $returnCookie=0){
  68. $curl = curl_init();
  69. curl_setopt($curl, CURLOPT_URL, $url);
  70. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  71. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  72. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  73. //设置https
  74. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
  75. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
  76. if($post) {
  77. curl_setopt($curl, CURLOPT_POST, 1);
  78. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  79. }
  80. if($cookie) {
  81. curl_setopt($curl, CURLOPT_COOKIE, $cookie);
  82. }
  83. curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
  84. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  85. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  86. $data = curl_exec($curl);
  87. if (curl_errno($curl)) {
  88. return curl_error($curl);
  89. }
  90. curl_close($curl);
  91. if($returnCookie){
  92. list($header, $body) = explode("\r\n\r\n", $data, 2);
  93. preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
  94. $info['cookie'] = substr($matches[1][0], 1);
  95. $info['content'] = $body;
  96. return $info;
  97. }else{
  98. return $data;
  99. }
  100. }
  101. /*
  102. POST
  103. */
  104. public function post_curl($posturl, $params, $method = 'POST', $header = '') {
  105. $curl = curl_init();
  106. curl_setopt($curl, CURLOPT_URL, $posturl);
  107. if ($header)
  108. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  109. switch ($method) {
  110. case 'GET':
  111. curl_setopt($curl, CURLOPT_HTTPGET, 1);
  112. case 'POST':
  113. curl_setopt($curl, CURLOPT_POST, 1);
  114. break;
  115. case 'PUT':
  116. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  117. break;
  118. case 'DELETE':
  119. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  120. break;
  121. }
  122. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, False);
  123. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  124. curl_setopt($curl, CURLOPT_HEADER, FALSE);
  125. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  126. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  127. $retdata = curl_exec($curl);
  128. curl_close($curl);
  129. return $retdata;
  130. }
  131. }