WalletService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Wallet;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Services\CoinService;
  9. use App\Services\UserService;
  10. use App\Helpers\TronHelper;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Services\BalanceLogService;
  13. /**
  14. * 用户虚拟币钱包
  15. */
  16. class WalletService extends BaseService
  17. {
  18. /**
  19. * @description: 模型
  20. * @return {string}
  21. */
  22. public static function model() :string
  23. {
  24. return Wallet::class;
  25. }
  26. /**
  27. * @description: 枚举
  28. * @return {*}
  29. */
  30. public static function enum() :string
  31. {
  32. return '';
  33. }
  34. /**
  35. * @description: 获取查询条件
  36. * @param {array} $search 查询内容
  37. * @return {array}
  38. */
  39. public static function getWhere(array $search = []) :array
  40. {
  41. $where = [];
  42. if(isset($search['coin']) && !empty($search['coin'])){
  43. $where[] = ['coin', '=', $search['coin']];
  44. }
  45. if(isset($search['net']) && !empty($search['net'])){
  46. $where[] = ['net', '=', $search['net']];
  47. }
  48. if(isset($search['address']) && !empty($search['address'])){
  49. $where[] = ['address', '=', $search['address']];
  50. }
  51. if(isset($search['id']) && !empty($search['id'])){
  52. $where[] = ['id', '=', $search['id']];
  53. }
  54. if(isset($search['member_id']) && !empty($search['member_id'])){
  55. $where[] = ['member_id', '=', $search['member_id']];
  56. }
  57. return $where;
  58. }
  59. /**
  60. * @description: 查询单条数据
  61. * @param array $search
  62. * @return \App\Models\Coin|null
  63. */
  64. public static function findOne(array $search): ?Wallet
  65. {
  66. return self::model()::where(self::getWhere($search))->first();
  67. }
  68. /**
  69. * @description: 查询所有数据
  70. * @param array $search
  71. * @return \Illuminate\Database\Eloquent\Collection
  72. */
  73. public static function findAll(array $search = [])
  74. {
  75. return self::model()::where(self::getWhere($search))->get();
  76. }
  77. /**
  78. * @description: 分页查询
  79. * @param array $search
  80. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  81. */
  82. public static function paginate(array $search = [])
  83. {
  84. $limit = isset($search['limit'])?$search['limit']:15;
  85. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  86. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  87. }
  88. /**
  89. * @description: 为用户创建虚拟钱包
  90. * @param {int} $memberId 用户ID
  91. * @return {*}
  92. */
  93. public static function createVirtualWallets(int $memberId)
  94. {
  95. $coins = CoinService::findAll(['coin' => 'USDT']);
  96. $users = UserService::findOne(['member_id' => $memberId]);
  97. $walletsData = $coins->map(function($coin) use ($memberId,$users) {
  98. switch($coin->coin){
  99. case 'USDT':
  100. $trons = TronHelper::createAddress($memberId);
  101. break;
  102. default:
  103. $trons = [];
  104. }
  105. return [
  106. 'user_id' => $users['id'],
  107. 'member_id' => $memberId,
  108. 'coin' => $coin->coin,
  109. 'net' => $coin->net,
  110. 'address' => $trons['address']??'',
  111. 'private_key' => $trons['private_key']??'',
  112. 'available_balance' => 0,
  113. 'frozen_balance' => 0
  114. ];
  115. })->toArray();
  116. // 批量创建钱包以提高性能
  117. self::model()::insert($walletsData);
  118. return self::findAll(['member_id' => $memberId]);
  119. }
  120. /**
  121. * @description: 获取用户的钱包
  122. * @param {int} $memberId
  123. * @return {*}
  124. */
  125. public static function getUserWallet(int $memberId)
  126. {
  127. $wallets = self::findAll(['member_id' => $memberId]);
  128. if($wallets->isEmpty()){
  129. $wallets = self::createVirtualWallets($memberId);
  130. }
  131. $wallets->map(function($wallet) {
  132. $wallet->available_balance = removeZero($wallet->available_balance);
  133. $wallet->frozen_balance = removeZero($wallet->frozen_balance);
  134. // $wallet->total_balance = $wallet->available_balance + $wallet->frozen_balance;
  135. return $wallet;
  136. });
  137. return $wallets;
  138. }
  139. /**
  140. * @description: 获取用户充值钱包地址
  141. * @param {*} $memberId
  142. * @return {*}
  143. */
  144. public static function getRechargeImageAddress($memberId)
  145. {
  146. self::getUserWallet($memberId);
  147. $info = self::findOne(['member_id' => $memberId]);
  148. $path = self::rechargeQrCodeExists($info->address);
  149. if(empty($path)){
  150. $path = self::createRechargeQrCode($info->address);
  151. }
  152. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  153. return [
  154. 'coin' => $info->coin,
  155. 'net' => $info->net,
  156. 'address' => $info->address,
  157. 'path' => $path,
  158. 'full_path' => asset($path)
  159. ];
  160. }
  161. /**
  162. * @description: 获取平台充值钱包地址
  163. * @param {*} $address
  164. * @return {*}
  165. */
  166. public static function getPlatformImageAddress($address)
  167. {
  168. $path = self::rechargeQrCodeExists($address);
  169. if(empty($path)){
  170. $path = self::createRechargeQrCode($address);
  171. }
  172. // $host = config('app.url'); // 通常在 .env 中配置 APP_URL
  173. return [
  174. 'coin' => 'USDT',
  175. 'net' => 'TRC20',
  176. 'address' => $address,
  177. 'path' => $path,
  178. 'full_path' => asset($path)
  179. ];
  180. }
  181. /**
  182. * @description: 更新余额
  183. * @param {*} $memberId
  184. * @param {*} $amount
  185. * @return {*}
  186. */
  187. public static function updateBalance($memberId , $amount)
  188. {
  189. $data = [];
  190. $self = self::findOne(['member_id' => $memberId]);
  191. $data['before_balance'] = $self->available_balance; // 操作前余额
  192. $self->available_balance += $amount;
  193. $data['after_balance'] = $self->available_balance;
  194. $self->save();
  195. return $data;
  196. }
  197. /**
  198. * @description: 获取用户余额
  199. * @param {int} $memberId 用户ID
  200. * @return {*}
  201. */
  202. public static function getBalance($memberId)
  203. {
  204. $selfInfo = self::findOne(['member_id' => $memberId]);
  205. $userInfo = UserService::findOne(['member_id' => $memberId]);
  206. $text = '';
  207. $text .= "用户ID:{$memberId} \n";
  208. $text .= "用户名:{$userInfo->first_name} \n";
  209. $text .= "当前余额:{$selfInfo->available_balance} \n";
  210. return [
  211. 'chat_id' => $memberId,
  212. 'text' => $text,
  213. 'protect_content' => true
  214. ];
  215. }
  216. }