BaseService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Services;
  3. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  4. use Endroid\QrCode\Builder\Builder;
  5. use Endroid\QrCode\Writer\PngWriter;
  6. class BaseService
  7. {
  8. const YES = 1;
  9. const NOT = 0;
  10. /**
  11. * @description: 生成充值二维码
  12. * @param {*} $address 充值地址
  13. * @return {*}
  14. */
  15. public static function createRechargeQrCode($address = '')
  16. {
  17. $content = $address;
  18. $qrSize = 300;
  19. $font = 4;
  20. $textHeight = 20;
  21. $padding = 10;
  22. // 生成二维码图像对象
  23. $result = Builder::create()
  24. ->writer(new PngWriter())
  25. ->data($content)
  26. ->size($qrSize)
  27. ->margin(0)
  28. ->build();
  29. $qrImage = imagecreatefromstring($result->getString());
  30. // 创建画布(加上下方文字区和边距)
  31. $canvasWidth = $qrSize + $padding * 2;
  32. $canvasHeight = $qrSize + $textHeight + $padding * 2;
  33. $image = imagecreatetruecolor($canvasWidth, $canvasHeight);
  34. // 背景白色
  35. $white = imagecolorallocate($image, 255, 255, 255);
  36. imagefill($image, 0, 0, $white);
  37. // 黑色字体
  38. $black = imagecolorallocate($image, 0, 0, 0);
  39. // 合并二维码图像
  40. imagecopy($image, $qrImage, $padding, $padding, 0, 0, $qrSize, $qrSize);
  41. // 写文字
  42. $textWidth = imagefontwidth($font) * strlen($content);
  43. $x = ($canvasWidth - $textWidth) / 2;
  44. $y = $qrSize + $padding + 5;
  45. imagestring($image, $font, $x, $y, $content, $black);
  46. // 生成文件名
  47. $filename = $address. '.png';
  48. $relativePath = 'recharge/' . $filename;
  49. $storagePath = storage_path('app/public/' . $relativePath);
  50. // 确保目录存在
  51. @mkdir(dirname($storagePath), 0777, true);
  52. // 保存图片到文件
  53. imagepng($image, $storagePath);
  54. // 清理
  55. imagedestroy($qrImage);
  56. imagedestroy($image);
  57. // 返回 public 存储路径(可用于 URL)
  58. return 'storage/'.$relativePath; // 或返回 Storage::url($relativePath);
  59. }
  60. /**
  61. * 判断指定地址的二维码是否已生成(已存在文件)
  62. *
  63. * @param string $address 充值地址
  64. * @return
  65. */
  66. public static function rechargeQrCodeExists(string $address)
  67. {
  68. $filename = $address . '.png';
  69. $relativePath = 'recharge/' . $filename;
  70. $storagePath = storage_path('app/public/' . $relativePath);
  71. $path = '';
  72. if(file_exists($storagePath)){
  73. $path = 'storage/'.$relativePath;
  74. }
  75. return $path;
  76. }
  77. /**
  78. * @description: 转成树形数据
  79. * @param {*} $list 初始数据
  80. * @param {*} $pid 父id
  81. * @param {*} $level 层级
  82. * @param {*} $pid_name pid字段名称 默认pid
  83. * @param {*} $id_name 主键id 名称
  84. * @return {*}
  85. */
  86. public static function toTree($list,$pid=0,$level=0,$pid_name='pid',$id_name='id')
  87. {
  88. $arr=[];
  89. $level++;
  90. foreach($list as $k => $v){
  91. if($pid==$v[$pid_name]){
  92. $v['level']=$level;
  93. $v['children']=self::toTree($list,$v[$id_name],$level,$pid_name,$id_name);
  94. $arr[]=$v;
  95. }
  96. }
  97. return $arr;
  98. }
  99. }