WalletService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * @param $memberId
  157. * @return void
  158. * @throws TelegramSDKException
  159. */
  160. public static function newUserRegisterActivity($memberId)
  161. {
  162. $start_date = '2025-12-23'; // 活动开始
  163. $end_date = '2026-01-31'; // 活动结束
  164. $date = date('Y-m-d');
  165. if ($date >= $start_date && $date <= $end_date) {
  166. $user = UserService::findOne(['member_id' => $memberId]);
  167. $amount = 28;//活动金额
  168. // 有用户名的账号
  169. if ($user && $user->getUsername()) {
  170. $wallets = self::findOne(['member_id' => $memberId]);
  171. $before_balance = $wallets->available_balance;
  172. $after_balance = $before_balance + $amount;
  173. $wallets->available_balance = $after_balance;
  174. $wallets->save();
  175. BalanceLogService::addLog($memberId, $amount, $before_balance, $after_balance, '人工充值', 0, '双旦活动注册赠送28');
  176. $returnMsg = WalletService::getBalance($user->getMemberId());
  177. $telegram = new Api(config('services.telegram.token'));
  178. $telegram->sendMessage($returnMsg);
  179. }
  180. }
  181. }
  182. /**
  183. * @description: 获取用户的钱包
  184. * @param {int} $memberId
  185. * @return {*}
  186. */
  187. public static function getUserWallet( $memberId)
  188. {
  189. $wallets = self::findAll(['member_id' => $memberId]);
  190. if ($wallets->isEmpty()) {
  191. $wallets = self::createVirtualWallets($memberId);
  192. }
  193. $wallets->map(function ($wallet) {
  194. $wallet->available_balance = removeZero($wallet->available_balance);
  195. $wallet->frozen_balance = removeZero($wallet->frozen_balance);
  196. // $wallet->total_balance = $wallet->available_balance + $wallet->frozen_balance;
  197. return $wallet;
  198. });
  199. return $wallets;
  200. }
  201. /**
  202. * @description: 获取用户充值钱包地址
  203. * @param {*} $memberId
  204. * @return {*}
  205. */
  206. public static function getRechargeImageAddress($memberId)
  207. {
  208. self::getUserWallet($memberId);
  209. $info = self::findOne(['member_id' => $memberId]);
  210. $path = self::rechargeQrCodeExists($info->address);
  211. if (empty($path)) {
  212. $path = self::createRechargeQrCode($info->address);
  213. }
  214. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  215. return [
  216. 'coin' => $info->coin,
  217. 'net' => $info->net,
  218. 'address' => $info->address,
  219. 'path' => $path,
  220. 'full_path' => asset($path)
  221. ];
  222. }
  223. /**
  224. * @description: 获取平台充值钱包地址
  225. * @param {*} $address
  226. * @return {*}
  227. */
  228. public static function getPlatformImageAddress($address)
  229. {
  230. $path = self::rechargeQrCodeExists($address);
  231. if (empty($path)) {
  232. $path = self::createRechargeQrCode($address);
  233. }
  234. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  235. return [
  236. 'coin' => 'USDT',
  237. 'net' => "TRC20",
  238. 'address' => $address,
  239. 'path' => $path,
  240. 'full_path' => asset($path)
  241. ];
  242. }
  243. /**
  244. * @description: 更新余额
  245. * @param {*} $memberId
  246. * @param {*} $amount
  247. * @return {*}
  248. */
  249. public static function updateBalance($memberId, $amount)
  250. {
  251. $data = [];
  252. $self = self::findOne(['member_id' => $memberId]);
  253. $data['before_balance'] = $self->available_balance; // 操作前余额
  254. $self->available_balance += $amount;
  255. $data['after_balance'] = $self->available_balance;
  256. $self->save();
  257. return $data;
  258. }
  259. /**
  260. * @description: 获取用户余额
  261. * @param {int} $memberId 用户ID
  262. * @return {*}
  263. */
  264. public static function getBalance($memberId)
  265. {
  266. $selfInfo = self::findOne(['member_id' => $memberId]);
  267. $userInfo = UserService::findOne(['member_id' => $memberId]);
  268. $balance = number_format($selfInfo->available_balance, 2, '.', '');
  269. $text = lang("用户ID") . ":{$memberId} \n";
  270. $text .= lang('用户名') . ":{$userInfo->getUsername()} \n";
  271. $text .= lang('昵称') . ":{$userInfo->getFirstName()} \n";
  272. $text .= lang('当前余额') . ":{$balance} RMB\n";
  273. $keyboard = [
  274. [
  275. // ['text' => lang('➕充值'), 'callback_data' => "topup@@topup"],
  276. ['text' => lang('➕充值'), 'callback_data' => "topup@@sj_apply"],
  277. ['text' => lang('🧾账单'), 'callback_data' => "topup@@bill"],
  278. ],
  279. [
  280. ['text' => lang('➕ 提现'), 'callback_data' => "withdraw@@qb_show_channel"],
  281. ['text' => lang('🧾 提现账单'), 'callback_data' => "withdraw@@bill"]
  282. ],
  283. [
  284. ['text' => lang('🔑秘钥管理'), 'callback_data' => 'secret@@index'],
  285. ],
  286. ];
  287. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  288. if ($three_payment_switch == 1) {
  289. // $keyboard[] = [
  290. // // ['text' => '➕ 钱宝提现', 'callback_data' => "withdraw@@qb_apply"],
  291. // // ['text' => '🧾 钱宝账单', 'callback_data' => "withdraw@@bank_bill_1"],
  292. // ['text' => '🧾 钱宝提现账单', 'callback_data' => "withdraw@@bank_bill_1"],
  293. // ];
  294. $keyboard[] = [
  295. // ['text' => '➕ 三斤充值', 'callback_data' => "topup@@sj_apply"],
  296. ['text' => lang('🧾 第三方充值提现订单'), 'callback_data' => "topup@@sj_bill_1"]
  297. ];
  298. }
  299. $keyboard[] = [
  300. ['text' => lang('💵今日汇率💰'), 'callback_data' => "todayExchangeRate@@rate"]
  301. ];
  302. return [
  303. 'chat_id' => $memberId,
  304. 'text' => $text,
  305. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  306. 'protect_content' => true
  307. ];
  308. }
  309. }