UserService.php 5.7 KB

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