WxQrCode.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2019/10/23
  6. * Time: 11:23
  7. */
  8. namespace app\common\services\wechat;
  9. use GuzzleHttp\Client;
  10. class WxQrCode
  11. {
  12. public $param;
  13. public $page;
  14. public $auto_color = false;
  15. public $width = 430;//280-1280
  16. public $line_color = '';
  17. public $is_hyaline = false;
  18. public function mergeQrImage()
  19. {
  20. $res = $this->getWxacode();
  21. if ($res === false) {return ;}
  22. \Log::debug('------------小程序二维码-----------', $res);
  23. return $res;
  24. }
  25. public function setParam($data)
  26. {
  27. $this->param = $data['param'];
  28. $this->page = $data['page'];
  29. if ($data['auto_color']) $this->auto_color = $data['auto_color'];
  30. if ($data['width']) $this->width = $data['width'];
  31. if ($data['line_color']) $this->line_color = $data['line_color'];
  32. if ($data['is_hyaline']) $this->is_hyaline = $data['is_hyaline'];
  33. }
  34. //生成小程序二维码
  35. function getWxacode()
  36. {
  37. $token = $this->getToken();
  38. if ($token === false) {return false;}
  39. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$token;
  40. $json_data = [
  41. "scene" => $this->param,
  42. "page" => $this->page,
  43. "width" => $this->width
  44. ];
  45. if ($this->auto_color)
  46. {
  47. $json_data['auto_color'] = $this->auto_color;
  48. }
  49. if (!empty($this->line_color))
  50. {
  51. $json_data['line_color'] = $this->line_color;
  52. }
  53. if ($this->is_hyaline)
  54. {
  55. $json_data['is_hyaline'] = $this->is_hyaline;
  56. }
  57. $res = self::curl_post($url, json_encode($json_data), $options = array());//请求生成二维码
  58. if (isset($res['errcode'])) {
  59. \Log::debug('===生成小程序二维码获取失败====='. self::class, $res);
  60. return false;
  61. }
  62. return $res;
  63. }
  64. //发送获取token请求,获取token(有效期2小时)
  65. public function getToken()
  66. {
  67. $set = \Setting::get('plugin.min_app');
  68. $paramMap = [
  69. 'grant_type' => 'client_credential',
  70. 'appid' => $set['key'],
  71. 'secret' => $set['secret'],
  72. ];
  73. //获取token的url参数拼接
  74. $strQuery="";
  75. foreach ($paramMap as $k=>$v){
  76. $strQuery .= strlen($strQuery) == 0 ? "" : "&";
  77. $strQuery.=$k."=".urlencode($v);
  78. }
  79. $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?". $strQuery; //获取token的url
  80. $client = new Client;
  81. $res = $client->request('GET', $getTokenUrl);
  82. $data = json_decode($res->getBody()->getContents(), JSON_FORCE_OBJECT);
  83. if (isset($data['errcode'])) {
  84. \Log::debug('===生成小程序二维码获取token失败====='. self::class, $data);
  85. return false;
  86. }
  87. return $data['access_token'];
  88. }
  89. public function curl_post($url = '', $postdata = '', $options = array())
  90. {
  91. $ch = curl_init($url);
  92. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  93. curl_setopt($ch, CURLOPT_POST, 1);
  94. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  95. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  96. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  97. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  98. if (!empty($options)) {
  99. curl_setopt_array($ch, $options);
  100. }
  101. $data = curl_exec($ch);
  102. curl_close($ch);
  103. return $data;
  104. }
  105. }