UserService.php 7.1 KB

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