WxProgram.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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");
  70. fwrite($fp, json_encode($res));
  71. fclose($fp);
  72. chmod(dirname(__FILE__)."/json/access_token.json", 0777);
  73. return $res;
  74. }
  75. /**
  76. * 获取access_token
  77. * @return string
  78. */
  79. public function get_access_token()
  80. {
  81. $token = json_decode(file_get_contents(dirname(__FILE__)."/json/access_token.json"), true);
  82. // 验证AccessToken是否有效
  83. if (!$this->valid_access_token($token)) {
  84. // 生成新的AccessToken
  85. $token = $this->new_access_token();
  86. if ($token === false) {
  87. return false;
  88. }
  89. }
  90. return $token['access_token'];
  91. }
  92. /**
  93. * MiniProgran - 登录
  94. * 登录凭证校验
  95. * @param code
  96. */
  97. public function auth_code2_session($js_code)
  98. {
  99. $url = self::API_DOMAIN . 'sns/jscode2session?appid=' .$this->appId. '&secret=' .$this->appSecret. '&js_code=' .$js_code. '&grant_type=authorization_code';
  100. $res = HttpCurl::get($url, 'json');
  101. return $res;
  102. }
  103. /**
  104. * MiniProgran - 内容安全
  105. * 检查一段文本是否含有违法违规内容
  106. * @param string
  107. * @return bool
  108. */
  109. public function security_msg_sec_check($content)
  110. {
  111. $access_token = $this->get_access_token();
  112. $url = self::API_DOMAIN . 'wxa/msg_sec_check?access_token=' . $access_token;
  113. $postarray = array('content'=>$content);
  114. $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE);
  115. $res = HttpCurl::post($url, $postjson, 'json');
  116. if ($res['errcode']==0){
  117. return true;
  118. }else{
  119. return false;
  120. }
  121. }
  122. /**
  123. * MiniProgran - 小程序码
  124. * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。
  125. * @param string
  126. * @return bool
  127. */
  128. public function wxacode_get_unlimited($scene, $page="", $width=430, $filepath)
  129. {
  130. $access_token = $this->get_access_token();
  131. $url = self::API_DOMAIN . 'wxa/getwxacodeunlimit?access_token=' . $access_token;
  132. $postarray = array(
  133. 'scene' => $scene,
  134. 'page' => $page,
  135. 'width' => $width
  136. );
  137. $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE);
  138. $res = HttpCurl::post($url, $postjson, 'text');
  139. if (is_null(json_decode($res))) {
  140. $file = fopen($filepath, "w");
  141. fwrite($file, $res);
  142. fclose($file);
  143. return request()->domain() ."/".$filepath;
  144. }else{
  145. return false;
  146. }
  147. }
  148. }