UserService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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['register_ip']) && !empty($search['register_ip'])){
  43. $where[] = ['register_ip', '=', $search['register_ip']];
  44. }
  45. if (isset($search['game_id']) && !empty($search['game_id'])) {
  46. $where[] = ['game_id', '=', $search['game_id']];
  47. }
  48. if (isset($search['member_id']) && !empty($search['member_id'])) {
  49. $where[] = ['member_id', '=', $search['member_id']];
  50. }
  51. if (isset($search['first_name']) && !empty($search['first_name'])) {
  52. $where[] = ['first_name', '=', $search['first_name']];
  53. }
  54. if (isset($search['username']) && !empty($search['username'])) {
  55. $where[] = ['username', '=', $search['username']];
  56. }
  57. if (isset($search['like_first_name']) && !empty($search['like_first_name'])) {
  58. $where[] = ['first_name', 'like', "%" . $search['like_first_name'] . "%"];
  59. }
  60. return $where;
  61. }
  62. /**
  63. * @description: 查询单条数据
  64. * @param array $search
  65. * @return \App\Models\User|null
  66. */
  67. public static function findOne(array $search): ?User
  68. {
  69. return self::model()::where(self::getWhere($search))->first();
  70. }
  71. /**
  72. * @description: 查询所有数据
  73. * @param array $search
  74. * @return \Illuminate\Database\Eloquent\Collection
  75. */
  76. public static function findAll(array $search = [])
  77. {
  78. return self::model()::where(self::getWhere($search))->get();
  79. }
  80. /**
  81. * @description: 分页查询
  82. * @param array $search
  83. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  84. */
  85. public static function paginate(array $search = [])
  86. {
  87. $limit = isset($search['limit']) ? $search['limit'] : 15;
  88. $paginator = self::model()::where(self::getWhere($search))->with(['wallet' => function ($query) {
  89. // 使用 select 但确保包含外键
  90. $query->select('id', 'user_id', 'member_id', 'address', 'available_balance');
  91. }])->orderByDesc('created_at')->paginate($limit);
  92. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  93. }
  94. //设置游戏ID
  95. public function setGameID($chatId, $gameId)
  96. {
  97. $gameId = trim($gameId);
  98. if (!$gameId) return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,请输入正确的游戏ID'];
  99. if (User::where('game_id', $gameId)->where('member_id', '<>', $chatId)->first()) {
  100. return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,游戏ID已绑定其他用户'];
  101. }
  102. $user = User::where('member_id', $chatId)->first();
  103. $user->game_id = $gameId;
  104. $user->save();
  105. return ['chat_id' => $chatId, 'text' => "✅ 游戏ID设置成功\n游戏ID:{$gameId}"];
  106. }
  107. /**
  108. * @param Api $telegram
  109. * @param $data
  110. * @param $chatId
  111. * @param $firstName
  112. * @param $messageId
  113. * @return void
  114. * @throws TelegramSDKException
  115. */
  116. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  117. {
  118. $pattern = "/^setLanguage@@.*$/";
  119. if (preg_match($pattern, $data)) {
  120. $language = preg_replace('/^setLanguage@@/', '', $data);
  121. $res = UserService::setLanguage($chatId, $language);
  122. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  123. TelegramWebHook::setReplyKeyboard($chatId, $language);
  124. // $telegram->sendMessage($res);
  125. }
  126. }
  127. public static function getIsBanned($memberId)
  128. {
  129. $user = User::where('member_id', $memberId)->first();
  130. return $user->is_banned == 1;
  131. }
  132. public static function setLanguage($chatId, $language): array
  133. {
  134. $userInfo = self::findOne(['member_id' => $chatId]);
  135. $userInfo->setLanguage($language);
  136. $userInfo->save();
  137. App::setLocale($language);
  138. (new Home)->setMyCommands();
  139. return self::getLanguages($chatId);
  140. }
  141. public static function getLanguages($chatId): array
  142. {
  143. $keyboard = [
  144. [['text' => lang('en'), 'callback_data' => "setLanguage@@en"]],
  145. [['text' => lang('zh'), 'callback_data' => "setLanguage@@zh"]],
  146. [['text' => lang('vi'), 'callback_data' => "setLanguage@@vi"]],
  147. [['text' => lang('❌取消'), 'callback_data' => "message@@close"]],
  148. ];
  149. return [
  150. 'chat_id' => $chatId,
  151. 'text' => lang("请选择您的语言"),
  152. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  153. 'protect_content' => true
  154. ];
  155. }
  156. }