WalletService.php 14 KB

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