Qrcode.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\wechat;
  13. use app\common\library\helper;
  14. use cores\exception\BaseException;
  15. /**
  16. * 小程序二维码
  17. * Class Qrcode
  18. * @package app\common\library\wechat
  19. */
  20. class Qrcode extends WxBase
  21. {
  22. /**
  23. * 获取小程序码
  24. * API文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
  25. * @param string $scene 场景值 (例如: uid:10001)
  26. * @param string|null $page 页面地址
  27. * @param int $width 二维码宽度
  28. * @return mixed
  29. * @throws BaseException
  30. */
  31. public function getQrcode(string $scene, string $page = null, int $width = 430)
  32. {
  33. // 微信接口url
  34. $accessToken = $this->getAccessToken();
  35. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  36. // 构建请求
  37. $data = compact('scene', 'width');
  38. !is_null($page) && $data['page'] = $page;
  39. // 返回结果
  40. $result = $this->post($url, helper::jsonEncode($data));
  41. // 记录日志
  42. log_record([
  43. 'name' => '获取小程序码',
  44. 'params' => $data,
  45. 'result' => !strpos($result, 'errcode') ? 'true' : $result
  46. ]);
  47. if (!strpos($result, 'errcode')) {
  48. return $result;
  49. }
  50. $data = helper::jsonDecode($result);
  51. $error = '小程序码获取失败 ' . $data['errmsg'];
  52. if ($data['errcode'] == 41030) {
  53. $error = '小程序页面不存在,请先发布上线后再生成';
  54. }
  55. throwError($error);
  56. }
  57. }