Base.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\common\service\qrcode;
  3. use app\common\library\easywechat\AppWx;
  4. use Endroid\QrCode\QrCode;
  5. /**
  6. * 二维码服务基类
  7. */
  8. class Base
  9. {
  10. /**
  11. * 构造方法
  12. */
  13. public function __construct()
  14. {
  15. }
  16. /**
  17. * 保存小程序码到文件
  18. */
  19. protected function saveQrcode($app_id, $scene, $page)
  20. {
  21. // 文件目录
  22. $dirPath = root_path('public') . "/temp/{$app_id}/image_wx";
  23. !is_dir($dirPath) && mkdir($dirPath, 0755, true);
  24. // 文件名称
  25. $fileName = 'qrcode_' . md5($app_id . $scene . $page) . '.png';
  26. // 文件路径
  27. $savePath = "{$dirPath}/{$fileName}";
  28. if (file_exists($savePath)) return $savePath;
  29. // 小程序配置信息
  30. $app = AppWx::getApp($app_id);
  31. // 请求api获取小程序码
  32. $response = $app->app_code->getUnlimit($scene, [
  33. 'page' => $page,
  34. 'width' => 430
  35. ]);
  36. // 保存小程序码到文件
  37. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  38. $response->saveAs($dirPath, $fileName);
  39. }
  40. return $savePath;
  41. }
  42. /**
  43. * 保存小程序码到文件
  44. */
  45. protected function saveMpQrcode(\Endroid\QrCode\QrCode $qrcode, $app_id, $scene, $source)
  46. {
  47. // 文件目录
  48. $dirPath = root_path('public') ."/temp/{$app_id}/{$source}";
  49. !is_dir($dirPath) && mkdir($dirPath, 0755, true);
  50. // 文件名称
  51. $fileName = 'qrcode_' . md5($app_id . $scene) . '.png';
  52. // 文件路径
  53. $savePath = "{$dirPath}/{$fileName}";
  54. if (file_exists($savePath)) return $savePath;
  55. // 保存二维码到文件
  56. $qrcode->writeFile($savePath);
  57. return $savePath;
  58. }
  59. /**
  60. * 保存小程序码到文件
  61. */
  62. protected function saveQrcodeToDir($app_id, $page, $savePath, $codeList)
  63. {
  64. // 小程序码参数
  65. foreach ($codeList as $code){
  66. // 小程序配置信息
  67. $app = AppWx::getApp($app_id);
  68. $scene = "pid:{$code['gift_package_id']},cid:{$code['code']}";
  69. // 请求api获取小程序码
  70. $response = $app->app_code->getUnlimit($scene, [
  71. 'page' => $page,
  72. 'width' => 430
  73. ]);
  74. // 保存小程序码到文件
  75. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  76. $response->saveAs($savePath, $code['code'] .'.png');
  77. }
  78. }
  79. return true;
  80. }
  81. /**
  82. * 保存小程序码到文件
  83. */
  84. protected function saveMpQrcodeToDir($page, $savePath, $codeList, $app_id)
  85. {
  86. foreach ($codeList as $code){
  87. $qrcode = new QrCode(base_url().$page.'?package_id='.$code['gift_package_id'].'&code='.$code['code'].'&app_id='.$app_id);
  88. // 保存二维码到文件
  89. $path = "{$savePath}{$code['code']}".'.png';
  90. $qrcode->writeFile($path);
  91. }
  92. return true;
  93. }
  94. /**
  95. * 保存邀请好友小程序码到文件
  96. */
  97. protected function saveInvitQrcodeToDir($app_id, $page, $savePath, $id)
  98. {
  99. // 小程序配置信息
  100. $app = AppWx::getApp($app_id);
  101. $scene = "invitid:$id";
  102. // 请求api获取小程序码
  103. $response = $app->app_code->getUnlimit($scene, [
  104. 'page' => $page,
  105. 'width' => 430
  106. ]);
  107. // 保存小程序码到文件
  108. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  109. $response->saveAs($savePath, $id .'.png');
  110. }
  111. return true;
  112. }
  113. /**
  114. * 保存邀请好友小程序码到文件
  115. */
  116. protected function saveInvitMpQrcodeToDir($page, $savePath, $id, $app_id)
  117. {
  118. $qrcode = new QrCode(base_url(). '/'.$page.'?invitation_id='.$id. '&app_id='.$app_id);
  119. // 保存二维码到文件
  120. $path = "{$savePath}{$id}".'.png';
  121. $qrcode->writeFile($path);
  122. return true;
  123. }
  124. /**
  125. * 获取网络图片到临时目录
  126. */
  127. protected function saveTempImage($app_id, $url, $mark = 'temp')
  128. {
  129. $dirPath = root_path('public') . "temp/{$app_id}/{$mark}";
  130. !is_dir($dirPath) && mkdir($dirPath, 0755, true);
  131. $savePath = $dirPath . '/' . $mark . '_' . md5($url) . '.png';
  132. if (file_exists($savePath)) return $savePath;
  133. $ch = curl_init($url);
  134. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  135. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  136. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  137. $img = curl_exec($ch);
  138. curl_close($ch);
  139. $fp = fopen($savePath, 'w');
  140. fwrite($fp, $img);
  141. fclose($fp);
  142. return $savePath;
  143. }
  144. }