Client.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the OtkurBiz/ByteDance.
  9. *
  10. * (c) alim <alim@bulutbazar.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\MiniProgram\QRCode;
  16. use ByteDance\Kernel\BaseClient;
  17. use EasyWeChat\Kernel\Http\StreamResponse;
  18. /**
  19. * Class Client.
  20. *
  21. * @author alim <alim@bulutbazar.com>
  22. */
  23. class Client extends BaseClient
  24. {
  25. /**
  26. * Set User Storage
  27. * 获取小程序/小游戏的二维码。该二维码可通过任意 app 扫码打开,能跳转到开发者指定的对应字节系 app 内拉起小程序/小游戏, 并传入开发者指定的参数。通过该接口生成的二维码,永久有效,暂无数量限制.
  28. * params[]:
  29. * appname 否 toutiao 是打开二维码的字节系 app 名称,默认为今日头条,取值如下表所示
  30. * toutiao 今日头条
  31. * douyin 抖音
  32. * pipixia 皮皮虾
  33. * huoshan 火山小视频
  34. * path 否 小程序/小游戏启动参数,小程序则格式为 encode({path}?{query}),小游戏则格式为 JSON 字符串,默认为空
  35. * width 否 430 二维码宽度,单位 px,最小 280px,最大 1280px,默认为 430px
  36. * line_color 否 {"r":0,"g":0,"b":0} 二维码线条颜色,默认为黑色
  37. * background 否 二维码背景颜色,默认为透明
  38. * set_icon 否 FALSE 是否展示小程序/小游戏 icon,默认不展示
  39. * @throws \ByteDance\Kernel\Exceptions\InvalidConfigException
  40. *
  41. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  42. */
  43. public function create($params)
  44. {
  45. \Yii::warning($params);
  46. $post_params = [
  47. 'appname' => 'douyin',
  48. 'path' => '',
  49. 'width' => 430,
  50. 'set_icon' => true
  51. ];
  52. $post_params['path'] = urlencode($params['path']);
  53. if (empty($params['appname'])) {
  54. $post_params['appname'] = 'douyin';
  55. }
  56. if (isset($params['path'])) {
  57. $post_params['path'] = urlencode($params['path']);
  58. }
  59. if (isset($params['width'])) {
  60. $post_params['width'] = intval($params['width']);
  61. }
  62. $post_params['set_icon'] = $params['set_icon'] ? true : false;
  63. $post_params['access_token'] = $this->getAccessToken()->getToken()['access_token'];
  64. \Yii::warning($post_params);
  65. return $this->getStream('api/apps/qrcode', $post_params);
  66. }
  67. /**
  68. * Get stream.
  69. *
  70. * @param string $endpoint
  71. * @param array $params
  72. *
  73. * @return \ByteDance\Kernel\Http\Response
  74. *
  75. * @throws \ByteDance\Kernel\Exceptions\InvalidConfigException
  76. * @throws \GuzzleHttp\Exception\GuzzleException
  77. */
  78. protected function getStream(string $endpoint, array $params)
  79. {
  80. return $this->requestRaw($endpoint, 'POST', ['json' => $params]);
  81. }
  82. }