UserService.php 7.0 KB

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