WalletService.php 9.6 KB

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