Qrcode.php 1.4 KB

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