WalletService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Config;
  4. use App\Services\BaseService;
  5. use App\Models\Wallet;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use App\Services\CoinService;
  10. use App\Services\UserService;
  11. use App\Helpers\TronHelper;
  12. use Illuminate\Support\Facades\Log;
  13. use App\Services\BalanceLogService;
  14. use Telegram\Bot\Api;
  15. use Telegram\Bot\Exceptions\TelegramSDKException;
  16. /**
  17. * 用户虚拟币钱包
  18. */
  19. class WalletService extends BaseService
  20. {
  21. public static string $MODEL = Wallet::class;
  22. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId, $callbackId): void
  23. {
  24. switch ($data) {
  25. case "withdraw@@home"://提现管理
  26. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  27. $returnMsg = WalletService::getBalance($chatId);
  28. if ($returnMsg) {
  29. $telegram->sendMessage($returnMsg);
  30. }
  31. break;
  32. case "balanceAlert"://查看余额弹窗
  33. $alertText = WalletService::getBalance($chatId)['text'];
  34. WalletService::alertNotice($callbackId, $alertText);
  35. break;
  36. case "topUp@@home"://充值首页
  37. case "topUp@@home1"://充值首页
  38. $returnMsg = WalletService::getBalance($chatId);
  39. if ($returnMsg) {
  40. if ($data === "topUp@@home1") {
  41. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  42. $telegram->sendMessage($returnMsg);
  43. } else {
  44. $returnMsg['message_id'] = $messageId;
  45. $telegram->editMessageText($returnMsg);
  46. }
  47. }
  48. break;
  49. }
  50. }
  51. /**
  52. * @description: 模型
  53. * @return {string}
  54. */
  55. public static function model(): string
  56. {
  57. return Wallet::class;
  58. }
  59. /**
  60. * @description: 枚举
  61. * @return {*}
  62. */
  63. public static function enum(): string
  64. {
  65. return '';
  66. }
  67. /**
  68. * @description: 获取查询条件
  69. * @param {array} $search 查询内容
  70. * @return {array}
  71. */
  72. public static function getWhere(array $search = []): array
  73. {
  74. $where = [];
  75. if (isset($search['coin']) && !empty($search['coin'])) {
  76. $where[] = ['coin', '=', $search['coin']];
  77. }
  78. if (isset($search['net']) && !empty($search['net'])) {
  79. $where[] = ['net', '=', $search['net']];
  80. }
  81. if (isset($search['address']) && !empty($search['address'])) {
  82. $where[] = ['address', '=', $search['address']];
  83. }
  84. if (isset($search['id']) && !empty($search['id'])) {
  85. $where[] = ['id', '=', $search['id']];
  86. }
  87. if (isset($search['member_id']) && !empty($search['member_id'])) {
  88. $where[] = ['member_id', '=', $search['member_id']];
  89. }
  90. return $where;
  91. }
  92. /**
  93. * @description: 查询单条数据
  94. * @param array $search
  95. * @return \App\Models\Coin|null
  96. */
  97. public static function findOne(array $search): ?Wallet
  98. {
  99. return static::$MODEL::where(self::getWhere($search))->first();
  100. }
  101. /**
  102. * @description: 查询所有数据
  103. * @param array $search
  104. * @return \Illuminate\Database\Eloquent\Collection
  105. */
  106. public static function findAll(array $search = [])
  107. {
  108. return static::$MODEL::where(self::getWhere($search))->get();
  109. }
  110. /**
  111. * @description: 分页查询
  112. * @param array $search
  113. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  114. */
  115. public static function paginate(array $search = [])
  116. {
  117. $limit = isset($search['limit']) ? $search['limit'] : 15;
  118. $paginator = static::$MODEL::where(self::getWhere($search))->paginate($limit);
  119. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  120. }
  121. /**
  122. * @description: 为用户创建虚拟钱包
  123. * @param {int} $memberId 用户ID
  124. * @return {*}
  125. */
  126. public static function createVirtualWallets(int $memberId)
  127. {
  128. $coins = CoinService::findAll(['coin' => 'USDT']);
  129. $users = UserService::findOne(['member_id' => $memberId]);
  130. Log::error("WallerService");
  131. Log::error(json_encode($users));
  132. $walletsData = $coins->map(function ($coin) use ($memberId, $users) {
  133. switch ($coin->coin) {
  134. case 'USDT':
  135. $trons = TronHelper::createAddress($memberId);
  136. break;
  137. default:
  138. $trons = [];
  139. }
  140. return [
  141. 'user_id' => $users['id'],
  142. 'member_id' => $memberId,
  143. 'coin' => $coin->coin,
  144. 'net' => $coin->net,
  145. 'address' => $trons['address'] ?? '',
  146. 'private_key' => $trons['private_key'] ?? '',
  147. 'available_balance' => 0,
  148. 'frozen_balance' => 0
  149. ];
  150. })->toArray();
  151. // 批量创建钱包以提高性能
  152. static::$MODEL::insert($walletsData);
  153. // 活动
  154. // self::newUserRegisterActivity($memberId);
  155. return self::findAll(['member_id' => $memberId]);
  156. }
  157. /**
  158. * 重置所有用户钱包
  159. * @return void
  160. */
  161. public static function resetUserWallets()
  162. {
  163. $list = self::findAll();
  164. foreach ($list as $wallet) {
  165. $memberId = $wallet->member_id;
  166. $coin = $wallet->coin;
  167. switch ($coin) {
  168. case 'USDT':
  169. $trons = TronHelper::createAddress($memberId);
  170. break;
  171. default:
  172. $trons = [];
  173. }
  174. $wallet->address = $trons['address'] ?? $wallet->address;
  175. $wallet->private_key = $trons['private_key'] ?? $wallet->private_key;
  176. $wallet->save();
  177. }
  178. }
  179. /**
  180. * 新用户注册活动
  181. * @param $memberId
  182. * @return void
  183. * @throws TelegramSDKException
  184. */
  185. public static function newUserRegisterActivity($memberId)
  186. {
  187. $start_date = '2025-12-23'; // 活动开始
  188. $end_date = '2026-01-31'; // 活动结束
  189. $date = date('Y-m-d');
  190. if ($date >= $start_date && $date <= $end_date) {
  191. $user = UserService::findOne(['member_id' => $memberId]);
  192. $amount = 28;//活动金额
  193. // 有用户名的账号
  194. if ($user && $user->getUsername()) {
  195. $wallets = self::findOne(['member_id' => $memberId]);
  196. $before_balance = $wallets->available_balance;
  197. $after_balance = $before_balance + $amount;
  198. $wallets->available_balance = $after_balance;
  199. $wallets->save();
  200. BalanceLogService::addLog($memberId, $amount, $before_balance, $after_balance, '人工充值', 0, '双旦活动注册赠送28');
  201. $returnMsg = WalletService::getBalance($user->getMemberId());
  202. $telegram = new Api(config('services.telegram.token'));
  203. $telegram->sendMessage($returnMsg);
  204. }
  205. }
  206. }
  207. /**
  208. * @description: 获取用户的钱包
  209. * @param {int} $memberId
  210. * @return {*}
  211. */
  212. public static function getUserWallet( $memberId)
  213. {
  214. $wallets = self::findAll(['member_id' => $memberId]);
  215. if ($wallets->isEmpty()) {
  216. $wallets = self::createVirtualWallets($memberId);
  217. }
  218. $wallets->map(function ($wallet) {
  219. $wallet->available_balance = removeZero($wallet->available_balance);
  220. $wallet->frozen_balance = removeZero($wallet->frozen_balance);
  221. // $wallet->total_balance = $wallet->available_balance + $wallet->frozen_balance;
  222. return $wallet;
  223. });
  224. return $wallets;
  225. }
  226. /**
  227. * @description: 获取用户充值钱包地址
  228. * @param {*} $memberId
  229. * @return {*}
  230. */
  231. public static function getRechargeImageAddress($memberId)
  232. {
  233. self::getUserWallet($memberId);
  234. $info = self::findOne(['member_id' => $memberId]);
  235. $path = self::rechargeQrCodeExists($info->address);
  236. if (empty($path)) {
  237. $path = self::createRechargeQrCode($info->address);
  238. }
  239. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  240. return [
  241. 'coin' => $info->coin,
  242. 'net' => $info->net,
  243. 'address' => $info->address,
  244. 'path' => $path,
  245. 'full_path' => asset($path)
  246. ];
  247. }
  248. /**
  249. * @description: 获取平台充值钱包地址
  250. * @param {*} $address
  251. * @return {*}
  252. */
  253. public static function getPlatformImageAddress($address)
  254. {
  255. $path = self::rechargeQrCodeExists($address);
  256. if (empty($path)) {
  257. $path = self::createRechargeQrCode($address);
  258. }
  259. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  260. return [
  261. 'coin' => 'USDT',
  262. 'net' => "TRC20",
  263. 'address' => $address,
  264. 'path' => $path,
  265. 'full_path' => asset($path)
  266. ];
  267. }
  268. /**
  269. * @description: 更新余额
  270. * @param {*} $memberId
  271. * @param {*} $amount
  272. * @return {*}
  273. */
  274. public static function updateBalance($memberId, $amount)
  275. {
  276. $data = [];
  277. $self = self::findOne(['member_id' => $memberId]);
  278. $data['before_balance'] = $self->available_balance; // 操作前余额
  279. $self->available_balance += $amount;
  280. $data['after_balance'] = $self->available_balance;
  281. $self->save();
  282. return $data;
  283. }
  284. /**
  285. * @description: 获取用户余额
  286. * @param {int} $memberId 用户ID
  287. * @return {*}
  288. */
  289. public static function getBalance($memberId)
  290. {
  291. $selfInfo = self::findOne(['member_id' => $memberId]);
  292. $userInfo = UserService::findOne(['member_id' => $memberId]);
  293. $balance = number_format($selfInfo->available_balance, 2, '.', '');
  294. $text = lang("用户ID") . ":{$memberId} \n";
  295. $text .= lang('用户名') . ":{$userInfo->getUsername()} \n";
  296. $text .= lang('昵称') . ":{$userInfo->getFirstName()} \n";
  297. $text .= lang('当前余额') . ":{$balance} RMB\n";
  298. $keyboard = [
  299. [
  300. // ['text' => lang('➕充值'), 'callback_data' => "topup@@topup"],
  301. ['text' => lang('➕充值'), 'callback_data' => "topup@@sj_apply"],
  302. ['text' => lang('🧾账单'), 'callback_data' => "topup@@bill"],
  303. ],
  304. [
  305. ['text' => lang('➕ 提现'), 'callback_data' => "withdraw@@qb_show_channel"],
  306. ['text' => lang('🧾 提现账单'), 'callback_data' => "withdraw@@bill"]
  307. ],
  308. [
  309. ['text' => lang('🔑秘钥管理'), 'callback_data' => 'secret@@index'],
  310. ],
  311. ];
  312. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  313. if ($three_payment_switch == 1) {
  314. // $keyboard[] = [
  315. // // ['text' => '➕ 钱宝提现', 'callback_data' => "withdraw@@qb_apply"],
  316. // // ['text' => '🧾 钱宝账单', 'callback_data' => "withdraw@@bank_bill_1"],
  317. // ['text' => '🧾 钱宝提现账单', 'callback_data' => "withdraw@@bank_bill_1"],
  318. // ];
  319. $keyboard[] = [
  320. // ['text' => '➕ 三斤充值', 'callback_data' => "topup@@sj_apply"],
  321. ['text' => lang('🧾 第三方充值提现订单'), 'callback_data' => "topup@@sj_bill_1"]
  322. ];
  323. }
  324. $keyboard[] = [
  325. ['text' => lang('💵今日汇率💰'), 'callback_data' => "todayExchangeRate@@rate"]
  326. ];
  327. return [
  328. 'chat_id' => $memberId,
  329. 'text' => $text,
  330. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  331. 'protect_content' => true
  332. ];
  333. }
  334. // 违规钱包余额处理
  335. public static function violationWallet()
  336. {
  337. $remark = '由于系统漏洞,收益清除,28元指定大小单双玩法!10倍流水即可出款,违规没收';
  338. $smallList = static::$MODEL::where(function($query) {
  339. $query->where('available_balance', '>', 0)
  340. ->where('available_balance', '<', 28);
  341. })->take(100)->get();
  342. foreach($smallList as $k => $v){
  343. $amount = $v->available_balance * -1;
  344. $balanceData = self::updateBalance($v->member_id,$amount);
  345. BalanceLogService::addLog($v->member_id, $amount, $balanceData['before_balance'], $balanceData['after_balance'], '人工扣款', 0, $remark);
  346. $text = "余额变动通知\n";
  347. $text .= "变动后余额:{$balanceData['after_balance']}\n";
  348. $text .= "变动原因:{$remark}\n";
  349. TopUpService::notifyTransferSuccess($v->member_id, $text);
  350. }
  351. // $bigList = static::$MODEL::where('available_balance', '>', 28)->get();
  352. // foreach($bigList as $k => $v){
  353. // $amount = ($v->available_balance - 28) * -1;
  354. // $balanceData = self::updateBalance($v->member_id,$amount);
  355. // BalanceLogService::addLog($v->member_id, $amount, $balanceData['before_balance'], $balanceData['after_balance'], '人工扣款', 0, $remark);
  356. // $text = "余额变动通知\n";
  357. // $text .= "变动后余额:{$balanceData['after_balance']}\n";
  358. // $text .= "变动原因:{$remark}\n";
  359. // TopUpService::notifyTransferSuccess($v->member_id, $text);
  360. // }
  361. }
  362. }