wxlogic.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * @Author: wang jun
  4. * @Date: 2021-12-29 11:12:53
  5. * @Last Modified by: wang jun
  6. * @Last Modified time: 2021-12-29 15:14:22
  7. */
  8. namespace app\index\logic;
  9. class wxlogic
  10. {
  11. /**
  12. * 获取配置
  13. * 20211229
  14. * wj
  15. */
  16. private function getconfig()
  17. {
  18. $config = [
  19. 'appid' => 'wxcacf6eb6e7478e29',
  20. 'appSecret' => '3722c3f95df0569498a0e32ae6e85153',
  21. ];
  22. }
  23. /**
  24. * 获取二维码
  25. * 20211229
  26. * wj
  27. */
  28. public function getqrcode($data, $filename = '', $dirname = "qrcode")
  29. {
  30. if (empty($data) || !is_array($data)) {
  31. return backarr(0, "请求失败");
  32. }
  33. $filename = empty($filename) || !is_string($filename) ? time() : $filename;
  34. $dirname = empty($dirname) || !is_string($dirname) ? "qrcode" : $dirname;
  35. $qr_path = "./Uploads/" . $dirname;
  36. if (!file_exists($qr_path)) {
  37. mkdir($qr_path, 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
  38. }
  39. $filepath = $dirname . "/" . $filename . '.png';
  40. $filename = $filename . '.png';
  41. $file = $qr_path . "/" . $filename;
  42. $access = $this->getAccessToken();
  43. $access_token = $access['access_token'];
  44. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
  45. $data = array(
  46. 'scene' => $data['scene'], //二维码所带参数
  47. 'width' => 200,
  48. 'page' => $data['page'], //二维码跳转路径(要已发布小程序)
  49. 'auto_color' => true,
  50. //'is_hyaline' => true, //透明底色
  51. );
  52. $result = requestCurl($url, "post", json_encode($data)); //请求微信接口
  53. $resultJson = json_decode($result, true);
  54. if (!empty($resultJson) && isset($resultJson['errcode'])) {
  55. $errcode = $resultJson['errcode'];
  56. $errmsg = $resultJson['errmsg'];
  57. return backarr(0, $errmsg);
  58. }
  59. $domainpath = 'https://app.tjzhxx.cn:11443/Uploads/';
  60. $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
  61. if ($res === false) {
  62. return backarr(0, '生成二维码失败');
  63. }
  64. $data = [
  65. 'qrcode' => $domainpath . $filepath,
  66. ];
  67. return backarr(1, 'success', $data);
  68. }
  69. public function getAccessToken()
  70. {
  71. $config = $this->getconfig();
  72. $appid = 'wxcacf6eb6e7478e29';
  73. $appSecret = '3722c3f95df0569498a0e32ae6e85153';
  74. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
  75. $result = requestCurl($url);
  76. $wxResult = json_decode($result, true);
  77. return $wxResult;
  78. }
  79. }