GenerateShareQrcode.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use Curl\Curl;
  9. use EasyWeChat\Factory;
  10. class GenerateShareQrcode
  11. {
  12. /**
  13. * @param $storeId integer 商城ID
  14. * @param $scene string 二维码参数
  15. * @param int $width 二维码大小
  16. * @param null $page 跳转页面
  17. * @param int $platform 小程序类型 0--微信 1--支付宝
  18. */
  19. public static function getQrcode($storeId, $scene, $width = 430, $page = null)
  20. {
  21. if ($page == null) {
  22. $page = 'pages/index/index';
  23. }
  24. $model = new GenerateShareQrcode();
  25. return $model->wechat($scene, $width, $page);
  26. }
  27. public function wechat($scene, $width = 430, $page = null)
  28. {
  29. /** @var \EasyWeChat\MiniProgram\Application $wechat */
  30. $wechat = \app\modules\client\models\ApiModel::getWechat();
  31. $data = [
  32. 'width' => $width,
  33. ];
  34. if ($page) {
  35. $data['page'] = $page;
  36. }
  37. \Yii::warning("GET WXAPP QRCODE:" . "scene => ".$scene." data => ". json_encode($data));
  38. $response = $wechat->app_code->getUnlimit($scene, $data);
  39. \Yii::warning($response);
  40. // 保存小程序码到文件
  41. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  42. if(!is_dir(\Yii::$app->runtimePath . '/image')) {
  43. mkdir(\Yii::$app->runtimePath . '/image');
  44. }
  45. $file_name = md5(base64_encode($response->getBody())) . '.jpg';
  46. $save_path = \Yii::$app->runtimePath . '/image/';
  47. $response->saveAs($save_path, $file_name);
  48. //返回图片
  49. return [
  50. 'code' => 0,
  51. 'file_path' => $save_path.$file_name,
  52. ];
  53. } else {
  54. //返回文字
  55. return [
  56. 'code' => 1,
  57. 'msg' => $response['errmsg'],
  58. ];
  59. }
  60. }
  61. //保存图片内容到临时文件
  62. private function saveTempImageByContent($content)
  63. {
  64. $save_path = \Yii::$app->runtimePath . '/image/' . md5(base64_encode($content)) . '.jpg';
  65. if(!is_dir(\Yii::$app->runtimePath . '/image')) {
  66. mkdir(\Yii::$app->runtimePath . '/image');
  67. }
  68. $fp = fopen($save_path, 'w');
  69. fwrite($fp, $content);
  70. fclose($fp);
  71. return $save_path;
  72. }
  73. }