UserService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. $where[] = ['from', '<>', 2];
  41. if (isset($search['id']) && !empty($search['id'])) {
  42. $where[] = ['users.id', '=', $search['id']];
  43. }
  44. if (isset($search['register_ip']) && !empty($search['register_ip'])) {
  45. $where[] = ['users.register_ip', '=', $search['register_ip']];
  46. }
  47. if (isset($search['member_id']) && !empty($search['member_id'])) {
  48. $where[] = ['users.member_id', '=', $search['member_id']];
  49. }
  50. if (isset($search['first_name']) && !empty($search['first_name'])) {
  51. $where[] = ['users.first_name', '=', $search['first_name']];
  52. }
  53. if (isset($search['username']) && !empty($search['username'])) {
  54. $where[] = ['users.username', '=', $search['username']];
  55. }
  56. if (isset($search['like_first_name']) && !empty($search['like_first_name'])) {
  57. $where[] = ['users.first_name', 'like', "%" . $search['like_first_name'] . "%"];
  58. }
  59. if (isset($search['user_code']) && !empty($search['user_code'])) {
  60. $where[] = ['users.user_code', '=', $search['user_code']];
  61. }
  62. if (isset($search['agent_user_code']) && !empty($search['agent_user_code'])) {
  63. $where[] = ['users.agent_user_code', '=', $search['agent_user_code']];
  64. }
  65. if (isset($search['level']) && !empty($search['level'])) {
  66. $where[] = ['users.level', '=', $search['level']];
  67. }
  68. if (!empty($search['start_time'])) {
  69. $startTime = $search['start_time'] . " 00:00:00";
  70. $where[] = ['users.created_at', '>=', $startTime];
  71. }
  72. if (!empty($search['end_time'])) {
  73. $endTime = $search['end_time'] . " 23:59:59";
  74. $where[] = ['users.created_at', '<=', $endTime];
  75. }
  76. if (!empty($search['recharge_channel_group_id'])) {
  77. $where[] = ['users.recharge_channel_group_id', '=', $search['recharge_channel_group_id']];
  78. }
  79. return $where;
  80. }
  81. /**
  82. * @description: 查询单条数据
  83. * @param array $search
  84. * @return \App\Models\User|null
  85. */
  86. public static function findOne(array $search): ?User
  87. {
  88. return static::$MODEL::where(self::getWhere($search))->first();
  89. }
  90. /**
  91. * @description: 查询所有数据
  92. * @param array $search
  93. * @return \Illuminate\Database\Eloquent\Collection
  94. */
  95. public static function findAll(array $search = [])
  96. {
  97. return static::$MODEL::where(self::getWhere($search))->get();
  98. }
  99. /**
  100. * @description: 分页查询
  101. * @param array $search
  102. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  103. */
  104. public static function paginate(array $search = [], $order = 'desc', $by = 'created_at')
  105. {
  106. $limit = isset($search['limit']) ? $search['limit'] : 15;
  107. $page = isset($search['page']) ? $search['page'] : 1;
  108. $where = self::getWhere($search);
  109. $total = static::$MODEL::where($where)->count();
  110. $query = static::$MODEL::with(['wallet','level','agent'])
  111. ->leftJoin('wallets', 'users.member_id', '=', 'wallets.member_id')
  112. ->where($where);
  113. if ($by == 'available_balance') $by = "wallets.{$by}";
  114. else {
  115. $by = "users.{$by}";
  116. }
  117. $list = $query->orderBy($by, $order)
  118. ->select("users.*")
  119. ->forPage($page, $limit)
  120. ->get();
  121. return ['total' => $total, 'data' => $list];
  122. }
  123. //设置游戏ID
  124. public function setGameID($chatId, $gameId)
  125. {
  126. $gameId = trim($gameId);
  127. if (!$gameId) return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,请输入正确的游戏ID'];
  128. if (User::where('game_id', $gameId)->where('member_id', '<>', $chatId)->first()) {
  129. return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,游戏ID已绑定其他用户'];
  130. }
  131. $user = User::where('member_id', $chatId)->first();
  132. $user->game_id = $gameId;
  133. $user->save();
  134. return ['chat_id' => $chatId, 'text' => "✅ 游戏ID设置成功\n游戏ID:{$gameId}"];
  135. }
  136. /**
  137. * @param Api $telegram
  138. * @param $data
  139. * @param $chatId
  140. * @param $firstName
  141. * @param $messageId
  142. * @return void
  143. * @throws TelegramSDKException
  144. */
  145. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  146. {
  147. $pattern = "/^setLanguage@@.*$/";
  148. if (preg_match($pattern, $data)) {
  149. $language = preg_replace('/^setLanguage@@/', '', $data);
  150. $res = UserService::setLanguage($chatId, $language);
  151. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  152. TelegramWebHook::setReplyKeyboard($chatId, $language);
  153. // $telegram->sendMessage($res);
  154. }
  155. }
  156. public static function getIsBanned($memberId)
  157. {
  158. $user = User::where('member_id', $memberId)->first();
  159. return $user->is_banned == 1;
  160. }
  161. public static function setLanguage($chatId, $language): array
  162. {
  163. $userInfo = self::findOne(['member_id' => $chatId]);
  164. $userInfo->setLanguage($language);
  165. $userInfo->save();
  166. App::setLocale($language);
  167. (new Home)->setMyCommands();
  168. return self::getLanguages($chatId);
  169. }
  170. public static function getLanguages($chatId): array
  171. {
  172. $keyboard = [
  173. [['text' => lang('en'), 'callback_data' => "setLanguage@@en"]],
  174. [['text' => lang('zh'), 'callback_data' => "setLanguage@@zh"]],
  175. [['text' => lang('vi'), 'callback_data' => "setLanguage@@vi"]],
  176. [['text' => lang('❌取消'), 'callback_data' => "message@@close"]],
  177. ];
  178. return [
  179. 'chat_id' => $chatId,
  180. 'text' => lang("请选择您的语言"),
  181. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  182. 'protect_content' => true
  183. ];
  184. }
  185. }