WalletService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. return self::findAll(['member_id' => $memberId]);
  120. }
  121. /**
  122. * @description: 获取用户的钱包
  123. * @param {int} $memberId
  124. * @return {*}
  125. */
  126. public static function getUserWallet(int $memberId)
  127. {
  128. $wallets = self::findAll(['member_id' => $memberId]);
  129. if ($wallets->isEmpty()) {
  130. $wallets = self::createVirtualWallets($memberId);
  131. }
  132. $wallets->map(function ($wallet) {
  133. $wallet->available_balance = removeZero($wallet->available_balance);
  134. $wallet->frozen_balance = removeZero($wallet->frozen_balance);
  135. // $wallet->total_balance = $wallet->available_balance + $wallet->frozen_balance;
  136. return $wallet;
  137. });
  138. return $wallets;
  139. }
  140. /**
  141. * @description: 获取用户充值钱包地址
  142. * @param {*} $memberId
  143. * @return {*}
  144. */
  145. public static function getRechargeImageAddress($memberId)
  146. {
  147. self::getUserWallet($memberId);
  148. $info = self::findOne(['member_id' => $memberId]);
  149. $path = self::rechargeQrCodeExists($info->address);
  150. if (empty($path)) {
  151. $path = self::createRechargeQrCode($info->address);
  152. }
  153. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  154. return [
  155. 'coin' => $info->coin,
  156. 'net' => $info->net,
  157. 'address' => $info->address,
  158. 'path' => $path,
  159. 'full_path' => asset($path)
  160. ];
  161. }
  162. /**
  163. * @description: 获取平台充值钱包地址
  164. * @param {*} $address
  165. * @return {*}
  166. */
  167. public static function getPlatformImageAddress($address)
  168. {
  169. $path = self::rechargeQrCodeExists($address);
  170. if (empty($path)) {
  171. $path = self::createRechargeQrCode($address);
  172. }
  173. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  174. return [
  175. 'coin' => 'USDT',
  176. 'net' => "TRC20",
  177. 'address' => $address,
  178. 'path' => $path,
  179. 'full_path' => asset($path)
  180. ];
  181. }
  182. /**
  183. * @description: 更新余额
  184. * @param {*} $memberId
  185. * @param {*} $amount
  186. * @return {*}
  187. */
  188. public static function updateBalance($memberId, $amount)
  189. {
  190. $data = [];
  191. $self = self::findOne(['member_id' => $memberId]);
  192. $data['before_balance'] = $self->available_balance; // 操作前余额
  193. $self->available_balance += $amount;
  194. $data['after_balance'] = $self->available_balance;
  195. $self->save();
  196. return $data;
  197. }
  198. /**
  199. * @description: 获取用户余额
  200. * @param {int} $memberId 用户ID
  201. * @return {*}
  202. */
  203. public static function getBalance($memberId)
  204. {
  205. $selfInfo = self::findOne(['member_id' => $memberId]);
  206. $userInfo = UserService::findOne(['member_id' => $memberId]);
  207. $balance = number_format($selfInfo->available_balance, 2, '.', '');
  208. $text = '';
  209. $text .= "用户ID:{$memberId} \n";
  210. $text .= "用户名:{$userInfo->username} \n";
  211. $text .= "昵称:{$userInfo->first_name} \n";
  212. $text .= "当前余额:{$balance} RMB\n";
  213. $keyboard = [
  214. [
  215. ['text' => '➕充值', 'callback_data' => "topup@@topup"],
  216. ['text' => '🧾账单', 'callback_data' => "topup@@bill"],
  217. ],
  218. [
  219. ['text' => '➕ 提现', 'callback_data' => "withdraw@@apply"],
  220. ['text' => '🧾 提现账单', 'callback_data' => "withdraw@@bill"]
  221. ],
  222. ];
  223. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  224. if ($three_payment_switch == 1) {
  225. $keyboard[] = [
  226. ['text' => '➕ 钱宝提现', 'callback_data' => "withdraw@@qb_apply"],
  227. // ['text' => '🧾 提现账单', 'callback_data' => "withdraw@@bill"]
  228. ];
  229. }
  230. $keyboard[] = [
  231. ['text' => '💵今日汇率💰', 'callback_data' => "todayExchangeRate@@rate"]
  232. ];
  233. return [
  234. 'chat_id' => $memberId,
  235. 'text' => $text,
  236. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  237. 'protect_content' => true
  238. ];
  239. }
  240. }