WxProgram.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * WxProgram.php
  4. *
  5. * 微信小程序开发API接口
  6. * - 小程序码
  7. * - 安全内容
  8. * - 微信支付
  9. *
  10. */
  11. namespace echowx;
  12. use think\Exception;
  13. use echowx\utils\HttpCurl;
  14. use echowx\utils\Error;
  15. use echowx\utils\SHA1;
  16. use echowx\utils\Xml;
  17. class WxProgram
  18. {
  19. // 微信API域名
  20. const API_DOMAIN = 'https://api.weixin.qq.com/';
  21. // 开发者中心-配置项-AppID(应用ID)
  22. protected $appId;
  23. // 开发者中心-配置项-AppSecret(应用密钥)
  24. protected $appSecret;
  25. // 微信支付商户号,商户申请微信支付后,由微信支付分配的商户收款账号
  26. protected $payMchId;
  27. // API密钥,微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
  28. protected $payKey;
  29. /**
  30. * 设定配置项
  31. *
  32. * @param 微信小程序配置文件读取
  33. */
  34. public function __construct()
  35. {
  36. $this->appId = config('wxconfig.appId');
  37. $this->appSecret = config('wxconfig.appSecret');
  38. $this->payMchId = config('wxconfig.payMchId');
  39. $this->payKey = config('wxconfig.payKey');
  40. }
  41. /**
  42. * 校验access_token是否过期
  43. * @param string $token
  44. * @return bool
  45. */
  46. public function valid_access_token($token)
  47. {
  48. return $token && isset($token['expires_in']) && ($token['expires_in'] > time() + 1200);
  49. }
  50. /**
  51. * 生成新的access_token
  52. * @return mixed
  53. */
  54. public function new_access_token()
  55. {
  56. $url = self::API_DOMAIN . 'cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
  57. $res = HttpCurl::get($url, 'json');
  58. // 异常处理: 获取access_token网络错误
  59. if ($res === false) {
  60. @error_log('Http Get AccessToken Error.', 0);
  61. return false;
  62. }
  63. // 异常处理: access_token获取失败
  64. if (!isset($res['access_token'])) {
  65. @error_log('Get AccessToken Error: ' . json_encode($res), 0);
  66. return false;
  67. }
  68. $res['expires_in'] += time();
  69. $fp = fopen(dirname(__FILE__) . "/json/access_token.json", "w", 0777);
  70. fwrite($fp, json_encode($res));
  71. fclose($fp);
  72. return $res;
  73. }
  74. /**
  75. * 获取access_token
  76. * @return string
  77. */
  78. public function get_access_token()
  79. {
  80. $token = json_decode(file_get_contents(dirname(__FILE__) . "/json/access_token.json"), true);
  81. // 验证AccessToken是否有效
  82. if (!$this->valid_access_token($token)) {
  83. // 生成新的AccessToken
  84. $token = $this->new_access_token();
  85. if ($token === false) {
  86. return false;
  87. }
  88. }
  89. return $token['access_token'];
  90. }
  91. /**
  92. * MiniProgran - 登录
  93. * 登录凭证校验
  94. * @param code
  95. */
  96. public function auth_code2_session($js_code)
  97. {
  98. $url = self::API_DOMAIN . 'sns/jscode2session?appid=' . $this->appId . '&secret=' . $this->appSecret . '&js_code=' . $js_code . '&grant_type=authorization_code';
  99. $res = HttpCurl::get($url, 'json');
  100. return $res;
  101. }
  102. /**
  103. * MiniProgran - 内容安全
  104. * 检查一段文本是否含有违法违规内容
  105. * @param string
  106. * @return bool
  107. */
  108. public function security_msg_sec_check($content)
  109. {
  110. $access_token = $this->get_access_token();
  111. $url = self::API_DOMAIN . 'wxa/msg_sec_check?access_token=' . $access_token;
  112. $postarray = ['content' => $content];
  113. $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE);
  114. $res = HttpCurl::post($url, $postjson, 'json');
  115. if ($res['errcode'] == 0) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. /**
  122. * MiniProgran - 小程序码
  123. * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。
  124. * @param string
  125. * @return bool
  126. */
  127. public function wxacode_get_unlimited($scene, $page = "", $width = 430, $filepath = '')
  128. {
  129. if (file_exists(root_path("/" . $filepath))) {
  130. return request()->domain() . "/" . $filepath;
  131. }
  132. $access_token = $this->get_access_token();
  133. $url = self::API_DOMAIN . 'wxa/getwxacodeunlimit?access_token=' . $access_token;
  134. $postarray = [
  135. 'scene' => $scene,
  136. 'page' => $page,
  137. 'width' => $width,
  138. ];
  139. $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE);
  140. $res = HttpCurl::post($url, $postjson, 'text');
  141. if (is_null(json_decode($res))) {
  142. $file = fopen($filepath, "w");
  143. fwrite($file, $res);
  144. fclose($file);
  145. return request()->domain() . "/" . $filepath;
  146. } else {
  147. return false;
  148. }
  149. }
  150. }