UserService.php 5.1 KB

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