UserService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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:user_id,member_id,address,available_balance')->paginate($limit);
  86. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  87. }
  88. //设置游戏ID
  89. public function setGameID($chatId, $gameId)
  90. {
  91. $gameId = trim($gameId);
  92. if (!$gameId) return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,请输入正确的游戏ID'];
  93. if (User::where('game_id', $gameId)->where('member_id', '<>', $chatId)->first()) {
  94. return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,游戏ID已绑定其他用户'];
  95. }
  96. $user = User::where('member_id', $chatId)->first();
  97. $user->game_id = $gameId;
  98. $user->save();
  99. return ['chat_id' => $chatId, 'text' => "✅ 游戏ID设置成功\n游戏ID:{$gameId}"];
  100. }
  101. /**
  102. * @param Api $telegram
  103. * @param $data
  104. * @param $chatId
  105. * @param $firstName
  106. * @param $messageId
  107. * @return void
  108. * @throws TelegramSDKException
  109. */
  110. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  111. {
  112. $pattern = "/^setLanguage@@.*$/";
  113. if (preg_match($pattern, $data)) {
  114. $language = preg_replace('/^setLanguage@@/', '', $data);
  115. $res = UserService::setLanguage($chatId, $language);
  116. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  117. TelegramWebHook::setReplyKeyboard($chatId,$language);
  118. // $telegram->sendMessage($res);
  119. }
  120. }
  121. public static function setLanguage($chatId, $language): array
  122. {
  123. $userInfo = self::findOne(['member_id' => $chatId]);
  124. $userInfo->setLanguage($language);
  125. $userInfo->save();
  126. App::setLocale($language);
  127. (new Home)->setMyCommands();
  128. return self::getLanguages($chatId);
  129. }
  130. public static function getLanguages($chatId): array
  131. {
  132. $keyboard = [
  133. [['text' => lang('en'), 'callback_data' => "setLanguage@@en"]],
  134. [['text' => lang('zh'), 'callback_data' => "setLanguage@@zh"]],
  135. [['text' => lang('vi'), 'callback_data' => "setLanguage@@vi"]],
  136. [['text' => lang('❌取消'), 'callback_data' => "message@@close"]],
  137. ];
  138. return [
  139. 'chat_id' => $chatId,
  140. 'text' => lang("请选择您的语言"),
  141. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  142. 'protect_content' => true
  143. ];
  144. }
  145. }