BaseService.php 3.8 KB

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