WalletService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /**
  22. * @description: 模型
  23. * @return {string}
  24. */
  25. public static function model(): string
  26. {
  27. return Wallet::class;
  28. }
  29. /**
  30. * @description: 枚举
  31. * @return {*}
  32. */
  33. public static function enum(): string
  34. {
  35. return '';
  36. }
  37. /**
  38. * @description: 获取查询条件
  39. * @param {array} $search 查询内容
  40. * @return {array}
  41. */
  42. public static function getWhere(array $search = []): array
  43. {
  44. $where = [];
  45. if (isset($search['coin']) && !empty($search['coin'])) {
  46. $where[] = ['coin', '=', $search['coin']];
  47. }
  48. if (isset($search['net']) && !empty($search['net'])) {
  49. $where[] = ['net', '=', $search['net']];
  50. }
  51. if (isset($search['address']) && !empty($search['address'])) {
  52. $where[] = ['address', '=', $search['address']];
  53. }
  54. if (isset($search['id']) && !empty($search['id'])) {
  55. $where[] = ['id', '=', $search['id']];
  56. }
  57. if (isset($search['member_id']) && !empty($search['member_id'])) {
  58. $where[] = ['member_id', '=', $search['member_id']];
  59. }
  60. return $where;
  61. }
  62. /**
  63. * @description: 查询单条数据
  64. * @param array $search
  65. * @return \App\Models\Coin|null
  66. */
  67. public static function findOne(array $search): ?Wallet
  68. {
  69. return self::model()::where(self::getWhere($search))->first();
  70. }
  71. /**
  72. * @description: 查询所有数据
  73. * @param array $search
  74. * @return \Illuminate\Database\Eloquent\Collection
  75. */
  76. public static function findAll(array $search = [])
  77. {
  78. return self::model()::where(self::getWhere($search))->get();
  79. }
  80. /**
  81. * @description: 分页查询
  82. * @param array $search
  83. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  84. */
  85. public static function paginate(array $search = [])
  86. {
  87. $limit = isset($search['limit']) ? $search['limit'] : 15;
  88. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  89. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  90. }
  91. /**
  92. * @description: 为用户创建虚拟钱包
  93. * @param {int} $memberId 用户ID
  94. * @return {*}
  95. */
  96. public static function createVirtualWallets(int $memberId)
  97. {
  98. $coins = CoinService::findAll(['coin' => 'USDT']);
  99. $users = UserService::findOne(['member_id' => $memberId]);
  100. $walletsData = $coins->map(function ($coin) use ($memberId, $users) {
  101. switch ($coin->coin) {
  102. case 'USDT':
  103. $trons = TronHelper::createAddress($memberId);
  104. break;
  105. default:
  106. $trons = [];
  107. }
  108. return [
  109. 'user_id' => $users['id'],
  110. 'member_id' => $memberId,
  111. 'coin' => $coin->coin,
  112. 'net' => $coin->net,
  113. 'address' => $trons['address'] ?? '',
  114. 'private_key' => $trons['private_key'] ?? '',
  115. 'available_balance' => 0,
  116. 'frozen_balance' => 0
  117. ];
  118. })->toArray();
  119. // 批量创建钱包以提高性能
  120. self::model()::insert($walletsData);
  121. // 活动
  122. self::newUserRegisterActivity($memberId);
  123. return self::findAll(['member_id' => $memberId]);
  124. }
  125. // 新用户注册活动
  126. public static function newUserRegisterActivity($memberId)
  127. {
  128. $start_date = '2025-12-23'; // 活动开始
  129. $end_date = '2026-01-31'; // 活动结束
  130. $date = date('Y-m-d');
  131. if($date >= $start_date && $date <= $end_date){
  132. $user = UserService::findOne(['member_id' => $memberId]);
  133. // 有用户名的账号
  134. if($user && $user->getUsername()){
  135. $wallets = self::findOne(['member_id' => $memberId]);
  136. $amount = 28; //活动金额
  137. $before_balance = $wallets->available_balance;
  138. $after_balance = $before_balance + $amount;
  139. $wallets->available_balance = $after_balance;
  140. $wallets->save();
  141. BalanceLogService::addLog($memberId,$amount,$before_balance,$after_balance,'人工充值',0,'双旦活动注册赠送28');
  142. $returnMsg = WalletService::getBalance($user->getMemberId());
  143. try {
  144. $telegram = new Api(config('services.telegram.token'));
  145. $telegram->sendMessage($returnMsg);
  146. } catch (TelegramSDKException $e) {
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * @description: 获取用户的钱包
  153. * @param {int} $memberId
  154. * @return {*}
  155. */
  156. public static function getUserWallet(int $memberId)
  157. {
  158. $wallets = self::findAll(['member_id' => $memberId]);
  159. if ($wallets->isEmpty()) {
  160. $wallets = self::createVirtualWallets($memberId);
  161. }
  162. $wallets->map(function ($wallet) {
  163. $wallet->available_balance = removeZero($wallet->available_balance);
  164. $wallet->frozen_balance = removeZero($wallet->frozen_balance);
  165. // $wallet->total_balance = $wallet->available_balance + $wallet->frozen_balance;
  166. return $wallet;
  167. });
  168. return $wallets;
  169. }
  170. /**
  171. * @description: 获取用户充值钱包地址
  172. * @param {*} $memberId
  173. * @return {*}
  174. */
  175. public static function getRechargeImageAddress($memberId)
  176. {
  177. self::getUserWallet($memberId);
  178. $info = self::findOne(['member_id' => $memberId]);
  179. $path = self::rechargeQrCodeExists($info->address);
  180. if (empty($path)) {
  181. $path = self::createRechargeQrCode($info->address);
  182. }
  183. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  184. return [
  185. 'coin' => $info->coin,
  186. 'net' => $info->net,
  187. 'address' => $info->address,
  188. 'path' => $path,
  189. 'full_path' => asset($path)
  190. ];
  191. }
  192. /**
  193. * @description: 获取平台充值钱包地址
  194. * @param {*} $address
  195. * @return {*}
  196. */
  197. public static function getPlatformImageAddress($address)
  198. {
  199. $path = self::rechargeQrCodeExists($address);
  200. if (empty($path)) {
  201. $path = self::createRechargeQrCode($address);
  202. }
  203. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  204. return [
  205. 'coin' => 'USDT',
  206. 'net' => "TRC20",
  207. 'address' => $address,
  208. 'path' => $path,
  209. 'full_path' => asset($path)
  210. ];
  211. }
  212. /**
  213. * @description: 更新余额
  214. * @param {*} $memberId
  215. * @param {*} $amount
  216. * @return {*}
  217. */
  218. public static function updateBalance($memberId, $amount)
  219. {
  220. $data = [];
  221. $self = self::findOne(['member_id' => $memberId]);
  222. $data['before_balance'] = $self->available_balance; // 操作前余额
  223. $self->available_balance += $amount;
  224. $data['after_balance'] = $self->available_balance;
  225. $self->save();
  226. return $data;
  227. }
  228. /**
  229. * @description: 获取用户余额
  230. * @param {int} $memberId 用户ID
  231. * @return {*}
  232. */
  233. public static function getBalance($memberId)
  234. {
  235. $selfInfo = self::findOne(['member_id' => $memberId]);
  236. $userInfo = UserService::findOne(['member_id' => $memberId]);
  237. $balance = number_format($selfInfo->available_balance, 2, '.', '');
  238. $text = lang("用户ID") . ":{$memberId} \n";
  239. $text .= lang('用户名') . ":{$userInfo->getUsername()} \n";
  240. $text .= lang('昵称') . ":{$userInfo->getFirstName()} \n";
  241. $text .= lang('当前余额') . ":{$balance} RMB\n";
  242. $keyboard = [
  243. [
  244. // ['text' => lang('➕充值'), 'callback_data' => "topup@@topup"],
  245. ['text' => lang('➕充值'), 'callback_data' => "topup@@sj_apply"],
  246. ['text' => lang('🧾账单'), 'callback_data' => "topup@@bill"],
  247. ],
  248. [
  249. ['text' => lang('➕ 提现'), 'callback_data' => "withdraw@@qb_show_channel"],
  250. ['text' => lang('🧾 提现账单'), 'callback_data' => "withdraw@@bill"]
  251. ],
  252. [
  253. ['text' => lang('🔑秘钥管理'), 'callback_data' => 'secret@@index'],
  254. ],
  255. ];
  256. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  257. if ($three_payment_switch == 1) {
  258. // $keyboard[] = [
  259. // // ['text' => '➕ 钱宝提现', 'callback_data' => "withdraw@@qb_apply"],
  260. // // ['text' => '🧾 钱宝账单', 'callback_data' => "withdraw@@bank_bill_1"],
  261. // ['text' => '🧾 钱宝提现账单', 'callback_data' => "withdraw@@bank_bill_1"],
  262. // ];
  263. $keyboard[] = [
  264. // ['text' => '➕ 三斤充值', 'callback_data' => "topup@@sj_apply"],
  265. ['text' => lang('🧾 第三方充值提现订单'), 'callback_data' => "topup@@sj_bill_1"]
  266. ];
  267. }
  268. $keyboard[] = [
  269. ['text' => lang('💵今日汇率💰'), 'callback_data' => "todayExchangeRate@@rate"]
  270. ];
  271. return [
  272. 'chat_id' => $memberId,
  273. 'text' => $text,
  274. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  275. 'protect_content' => true
  276. ];
  277. }
  278. }