UserService.php 4.9 KB

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