WalletService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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, $address = '')
  232. {
  233. self::getUserWallet($memberId);
  234. $info = self::findOne(['member_id' => $memberId]);
  235. $address = $address ?? $info->address;
  236. $path = self::rechargeQrCodeExists($address);
  237. if (empty($path)) {
  238. $path = self::createRechargeQrCode($address);
  239. }
  240. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  241. return [
  242. 'coin' => $info->coin,
  243. 'net' => $info->net,
  244. 'address' => $address,
  245. 'path' => $path,
  246. 'full_path' => asset($path)
  247. ];
  248. }
  249. /**
  250. * @description: 获取平台充值钱包地址
  251. * @param {*} $address
  252. * @return {*}
  253. */
  254. public static function getPlatformImageAddress($address)
  255. {
  256. $path = self::rechargeQrCodeExists($address);
  257. if (empty($path)) {
  258. $path = self::createRechargeQrCode($address);
  259. }
  260. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  261. return [
  262. 'coin' => 'USDT',
  263. 'net' => "TRC20",
  264. 'address' => $address,
  265. 'path' => $path,
  266. 'full_path' => asset($path)
  267. ];
  268. }
  269. /**
  270. * @description: 更新余额
  271. * @param {*} $memberId
  272. * @param {*} $amount
  273. * @return {*}
  274. */
  275. public static function updateBalance($memberId, $amount)
  276. {
  277. $data = [];
  278. $self = self::findOne(['member_id' => $memberId]);
  279. $data['before_balance'] = $self->available_balance; // 操作前余额
  280. $self->available_balance += $amount;
  281. $data['after_balance'] = $self->available_balance;
  282. $self->save();
  283. return $data;
  284. }
  285. /**
  286. * @description: 获取用户余额
  287. * @param {int} $memberId 用户ID
  288. * @return {*}
  289. */
  290. public static function getBalance($memberId)
  291. {
  292. $selfInfo = self::findOne(['member_id' => $memberId]);
  293. $userInfo = UserService::findOne(['member_id' => $memberId]);
  294. $balance = number_format($selfInfo->available_balance, 2, '.', '');
  295. $text = lang("用户ID") . ":{$memberId} \n";
  296. $text .= lang('用户名') . ":{$userInfo->getUsername()} \n";
  297. $text .= lang('昵称') . ":{$userInfo->getFirstName()} \n";
  298. $text .= lang('当前余额') . ":{$balance} RMB\n";
  299. $keyboard = [
  300. [
  301. // ['text' => lang('➕充值'), 'callback_data' => "topup@@topup"],
  302. ['text' => lang('➕充值'), 'callback_data' => "topup@@sj_apply"],
  303. ['text' => lang('🧾账单'), 'callback_data' => "topup@@bill"],
  304. ],
  305. [
  306. ['text' => lang('➕ 提现'), 'callback_data' => "withdraw@@qb_show_channel"],
  307. ['text' => lang('🧾 提现账单'), 'callback_data' => "withdraw@@bill"]
  308. ],
  309. [
  310. ['text' => lang('🔑秘钥管理'), 'callback_data' => 'secret@@index'],
  311. ],
  312. ];
  313. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  314. if ($three_payment_switch == 1) {
  315. // $keyboard[] = [
  316. // // ['text' => '➕ 钱宝提现', 'callback_data' => "withdraw@@qb_apply"],
  317. // // ['text' => '🧾 钱宝账单', 'callback_data' => "withdraw@@bank_bill_1"],
  318. // ['text' => '🧾 钱宝提现账单', 'callback_data' => "withdraw@@bank_bill_1"],
  319. // ];
  320. $keyboard[] = [
  321. // ['text' => '➕ 三斤充值', 'callback_data' => "topup@@sj_apply"],
  322. ['text' => lang('🧾 第三方充值提现订单'), 'callback_data' => "topup@@sj_bill_1"]
  323. ];
  324. }
  325. $keyboard[] = [
  326. ['text' => lang('💵今日汇率💰'), 'callback_data' => "todayExchangeRate@@rate"]
  327. ];
  328. return [
  329. 'chat_id' => $memberId,
  330. 'text' => $text,
  331. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  332. 'protect_content' => true
  333. ];
  334. }
  335. // 违规钱包余额处理
  336. public static function violationWallet()
  337. {
  338. $remark = '由于系统漏洞,收益清除,28元指定大小单双玩法!10倍流水即可出款,违规没收';
  339. $smallList = static::$MODEL::where(function($query) {
  340. $query->where('available_balance', '>', 0)
  341. ->where('available_balance', '<', 28);
  342. })->take(100)->get();
  343. foreach($smallList as $k => $v){
  344. $amount = $v->available_balance * -1;
  345. $balanceData = self::updateBalance($v->member_id,$amount);
  346. BalanceLogService::addLog($v->member_id, $amount, $balanceData['before_balance'], $balanceData['after_balance'], '人工扣款', 0, $remark);
  347. $text = "余额变动通知\n";
  348. $text .= "变动后余额:{$balanceData['after_balance']}\n";
  349. $text .= "变动原因:{$remark}\n";
  350. TopUpService::notifyTransferSuccess($v->member_id, $text);
  351. }
  352. // $bigList = static::$MODEL::where('available_balance', '>', 28)->get();
  353. // foreach($bigList as $k => $v){
  354. // $amount = ($v->available_balance - 28) * -1;
  355. // $balanceData = self::updateBalance($v->member_id,$amount);
  356. // BalanceLogService::addLog($v->member_id, $amount, $balanceData['before_balance'], $balanceData['after_balance'], '人工扣款', 0, $remark);
  357. // $text = "余额变动通知\n";
  358. // $text .= "变动后余额:{$balanceData['after_balance']}\n";
  359. // $text .= "变动原因:{$remark}\n";
  360. // TopUpService::notifyTransferSuccess($v->member_id, $text);
  361. // }
  362. }
  363. }