WxBase.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\library\wechat;
  13. use think\facade\Cache;
  14. use cores\traits\ErrorTrait;
  15. use app\common\library\helper;
  16. use app\common\exception\BaseException;
  17. /**
  18. * 微信api基类
  19. * Class wechat
  20. * @package app\library
  21. */
  22. class WxBase
  23. {
  24. use ErrorTrait;
  25. protected $appId;
  26. protected $appSecret;
  27. /**
  28. * 构造函数
  29. * WxBase constructor.
  30. * @param $appId
  31. * @param $appSecret
  32. */
  33. public function __construct($appId = null, $appSecret = null)
  34. {
  35. $this->setConfig($appId, $appSecret);
  36. }
  37. protected function setConfig($appId = null, $appSecret = null)
  38. {
  39. !empty($appId) && $this->appId = $appId;
  40. !empty($appSecret) && $this->appSecret = $appSecret;
  41. }
  42. /**
  43. * 获取access_token
  44. * @return mixed
  45. * @throws \cores\exception\BaseException
  46. */
  47. protected function getAccessToken()
  48. {
  49. $cacheKey = $this->appId . '@access_token';
  50. if (!Cache::get($cacheKey)) {
  51. // 请求API获取 access_token
  52. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
  53. $result = $this->get($url);
  54. $response = $this->jsonDecode($result);
  55. if (array_key_exists('errcode', $response)) {
  56. throwError("access_token获取失败,错误信息:{$result}");
  57. }
  58. // 记录日志
  59. log_record([
  60. 'name' => '获取access_token',
  61. 'url' => $url,
  62. 'appId' => $this->appId,
  63. 'result' => $result
  64. ]);
  65. // 写入缓存
  66. Cache::set($cacheKey, $response['access_token'], 6000);
  67. }
  68. return Cache::get($cacheKey);
  69. }
  70. /**
  71. * 模拟GET请求 HTTPS的页面
  72. * @param string $url 请求地址
  73. * @param array $data
  74. * @return string $result
  75. * @throws \cores\exception\BaseException
  76. */
  77. protected function get(string $url, array $data = [])
  78. {
  79. // 处理query参数
  80. if (!empty($data)) {
  81. $url = $url . '?' . http_build_query($data);
  82. }
  83. $curl = curl_init();
  84. curl_setopt($curl, CURLOPT_URL, $url);
  85. curl_setopt($curl, CURLOPT_HEADER, 0);
  86. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  87. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
  88. $result = curl_exec($curl);
  89. if ($result === false) {
  90. throwError(curl_error($curl));
  91. }
  92. curl_close($curl);
  93. return $result;
  94. }
  95. /**
  96. * 模拟POST请求
  97. * @param string $url 请求地址
  98. * @param mixed $data 请求数据
  99. * @param false $useCert 是否引入微信支付证书
  100. * @param array $sslCert 证书路径
  101. * @return mixed|bool|string
  102. * @throws \cores\exception\BaseException
  103. */
  104. protected function post(string $url, $data = [], bool $useCert = false, array $sslCert = [])
  105. {
  106. $header = [
  107. 'Content-type: application/json;'
  108. ];
  109. $curl = curl_init();
  110. curl_setopt($curl, CURLOPT_URL, $url);
  111. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  112. curl_setopt($curl, CURLOPT_HEADER, false);
  113. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  114. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  115. curl_setopt($curl, CURLOPT_POST, TRUE);
  116. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  117. if ($useCert == true) {
  118. // 设置证书:cert 与 key 分别属于两个.pem文件
  119. curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
  120. curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem']);
  121. curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
  122. curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem']);
  123. }
  124. $result = curl_exec($curl);
  125. if ($result === false) {
  126. throwError(curl_error($curl));
  127. }
  128. curl_close($curl);
  129. return $result;
  130. }
  131. /**
  132. * 模拟POST请求 [第二种方式, 用于兼容微信api]
  133. * @param $url
  134. * @param array $data
  135. * @return mixed
  136. * @throws \cores\exception\BaseException
  137. */
  138. protected function post2($url, array $data = [])
  139. {
  140. $header = [
  141. 'Content-Type: application/x-www-form-urlencoded'
  142. ];
  143. $ch = curl_init();
  144. curl_setopt($ch, CURLOPT_URL, $url);
  145. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  146. curl_setopt($ch, CURLOPT_POST, true);
  147. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  148. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
  150. $result = curl_exec($ch);
  151. if ($result === false) {
  152. throwError(curl_error($ch));
  153. }
  154. curl_close($ch);
  155. return $result;
  156. }
  157. /**
  158. * 数组转json
  159. * @param $data
  160. * @return string
  161. */
  162. protected function jsonEncode($data): string
  163. {
  164. return helper::jsonEncode($data, JSON_UNESCAPED_UNICODE);
  165. }
  166. /**
  167. * json转数组
  168. * @param $json
  169. * @return mixed
  170. */
  171. protected function jsonDecode($json)
  172. {
  173. return helper::jsonDecode($json);
  174. }
  175. }