UserService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Controllers\api\TelegramWebHook;
  4. use App\Services\BaseService;
  5. use App\Models\User;
  6. use Illuminate\Support\Facades\App;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use Telegram\Bot\Api;
  11. use Telegram\Bot\Exceptions\TelegramSDKException;
  12. use App\Http\Controllers\api\Home;
  13. class UserService extends BaseService
  14. {
  15. /**
  16. * @description: 模型
  17. * @return {string}
  18. */
  19. public static function model(): string
  20. {
  21. return User::class;
  22. }
  23. /**
  24. * @description: 枚举
  25. * @return {*}
  26. */
  27. public static function enum(): string
  28. {
  29. return '';
  30. }
  31. /**
  32. * @description: 获取查询条件
  33. * @param {array} $search 查询内容
  34. * @return {array}
  35. */
  36. public static function getWhere(array $search = []): array
  37. {
  38. $where = [];
  39. if (isset($search['id']) && !empty($search['id'])) {
  40. $where[] = ['id', '=', $search['id']];
  41. }
  42. if (isset($search['game_id']) && !empty($search['game_id'])) {
  43. $where[] = ['game_id', '=', $search['game_id']];
  44. }
  45. if (isset($search['member_id']) && !empty($search['member_id'])) {
  46. $where[] = ['member_id', '=', $search['member_id']];
  47. }
  48. if (isset($search['first_name']) && !empty($search['first_name'])) {
  49. $where[] = ['first_name', '=', $search['first_name']];
  50. }
  51. if (isset($search['username']) && !empty($search['username'])) {
  52. $where[] = ['username', '=', $search['username']];
  53. }
  54. if (isset($search['like_first_name']) && !empty($search['like_first_name'])) {
  55. $where[] = ['first_name', 'like', "%" . $search['like_first_name'] . "%"];
  56. }
  57. return $where;
  58. }
  59. /**
  60. * @description: 查询单条数据
  61. * @param array $search
  62. * @return \App\Models\User|null
  63. */
  64. public static function findOne(array $search): ?User
  65. {
  66. return self::model()::where(self::getWhere($search))->first();
  67. }
  68. /**
  69. * @description: 查询所有数据
  70. * @param array $search
  71. * @return \Illuminate\Database\Eloquent\Collection
  72. */
  73. public static function findAll(array $search = [])
  74. {
  75. return self::model()::where(self::getWhere($search))->get();
  76. }
  77. /**
  78. * @description: 分页查询
  79. * @param array $search
  80. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  81. */
  82. public static function paginate(array $search = [])
  83. {
  84. $limit = isset($search['limit']) ? $search['limit'] : 15;
  85. $paginator = self::model()::where(self::getWhere($search))->with(['wallet' => function($query) {
  86. // 使用 select 但确保包含外键
  87. $query->select('id', 'user_id', 'member_id', 'address', 'available_balance');
  88. }])->paginate($limit);
  89. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  90. }
  91. //设置游戏ID
  92. public function setGameID($chatId, $gameId)
  93. {
  94. $gameId = trim($gameId);
  95. if (!$gameId) return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,请输入正确的游戏ID'];
  96. if (User::where('game_id', $gameId)->where('member_id', '<>', $chatId)->first()) {
  97. return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,游戏ID已绑定其他用户'];
  98. }
  99. $user = User::where('member_id', $chatId)->first();
  100. $user->game_id = $gameId;
  101. $user->save();
  102. return ['chat_id' => $chatId, 'text' => "✅ 游戏ID设置成功\n游戏ID:{$gameId}"];
  103. }
  104. /**
  105. * @param Api $telegram
  106. * @param $data
  107. * @param $chatId
  108. * @param $firstName
  109. * @param $messageId
  110. * @return void
  111. * @throws TelegramSDKException
  112. */
  113. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  114. {
  115. $pattern = "/^setLanguage@@.*$/";
  116. if (preg_match($pattern, $data)) {
  117. $language = preg_replace('/^setLanguage@@/', '', $data);
  118. $res = UserService::setLanguage($chatId, $language);
  119. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  120. TelegramWebHook::setReplyKeyboard($chatId,$language);
  121. // $telegram->sendMessage($res);
  122. }
  123. }
  124. public static function setLanguage($chatId, $language): array
  125. {
  126. $userInfo = self::findOne(['member_id' => $chatId]);
  127. $userInfo->setLanguage($language);
  128. $userInfo->save();
  129. App::setLocale($language);
  130. (new Home)->setMyCommands();
  131. return self::getLanguages($chatId);
  132. }
  133. public static function getLanguages($chatId): array
  134. {
  135. $keyboard = [
  136. [['text' => lang('en'), 'callback_data' => "setLanguage@@en"]],
  137. [['text' => lang('zh'), 'callback_data' => "setLanguage@@zh"]],
  138. [['text' => lang('vi'), 'callback_data' => "setLanguage@@vi"]],
  139. [['text' => lang('❌取消'), 'callback_data' => "message@@close"]],
  140. ];
  141. return [
  142. 'chat_id' => $chatId,
  143. 'text' => lang("请选择您的语言"),
  144. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  145. 'protect_content' => true
  146. ];
  147. }
  148. }