BaseService.php 4.7 KB

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