BaseService.php 4.8 KB

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