Qrcode.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\library\wechat;
  10. use app\common\exception\BaseException;
  11. /**
  12. * 小程序二维码
  13. * Class Qrcode
  14. * @package app\common\library\wechat
  15. */
  16. class Qrcode extends WxBase
  17. {
  18. /**
  19. * 获取小程序码
  20. * @param $scene
  21. * @param null $page
  22. * @param int $width
  23. * @return mixed
  24. * @throws BaseException
  25. */
  26. public function getQrcode($scene, $page = null, $width = 430)
  27. {
  28. // 微信接口url
  29. $access_token = $this->getAccessToken();
  30. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
  31. // 构建请求
  32. $data = compact('scene', 'width');
  33. !is_null($page) && $data['page'] = $page;
  34. // 返回结果
  35. $result = $this->post($url, json_encode($data, JSON_UNESCAPED_UNICODE));
  36. // 记录日志
  37. log_write([
  38. 'describe' => '获取小程序码',
  39. 'params' => $data,
  40. 'result' => strpos($result, 'errcode') ? $result : 'image'
  41. ]);
  42. if (!strpos($result, 'errcode')) {
  43. return $result;
  44. }
  45. $data = json_decode($result, true);
  46. $error = '小程序码获取失败 ' . $data['errmsg'];
  47. if ($data['errcode'] == 41030) {
  48. $error = '小程序页面不存在,请先发布上线后再生成';
  49. }
  50. throw new BaseException(['msg' => $error]);
  51. }
  52. }