Wallet.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Recharge;
  6. use App\Services\BalanceLogService;
  7. use App\Services\ManualAuditService;
  8. use App\Services\WageringService;
  9. use App\Services\MenuService;
  10. use App\Services\RechargeService;
  11. use App\Services\TopUpService;
  12. use Exception;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Validation\ValidationException;
  16. use App\Models\Wallet as WalletModel;
  17. use App\Models\Withdraw;
  18. use App\Models\Config;
  19. use App\Models\PaymentOrder;
  20. use App\Models\Order;
  21. use App\Models\LhcOrder;
  22. use App\Models\Bank;
  23. use App\Models\Address;
  24. use App\Services\Payment\QianBaoService;
  25. class Wallet extends Controller
  26. {
  27. /**
  28. * 获取提现通道
  29. */
  30. public function withdrawChannel()
  31. {
  32. $list = QianBaoService::withdrawChannel();
  33. $data[] = ['label' => 'USDT', 'value' => 'USDT'];
  34. foreach ($list as $key => $item) {
  35. $data[] = ['label' => $item, 'value' => $key];
  36. }
  37. return $this->success($data);
  38. }
  39. //银行卡/支付宝列表
  40. public function bankList()
  41. {
  42. try {
  43. $params = request()->validate([
  44. 'channel' => 'nullable',
  45. 'member_id' => 'nullable',
  46. ]);
  47. $where = [];
  48. if (!empty($params['channel'])) {
  49. $where[] = ['channel', '=', $params['channel']];
  50. }
  51. if (!empty($params['member_id'])) {
  52. $where[] = ['member_id', '=', $params['member_id']];
  53. }
  54. $list = Bank::where($where)->get()->toArray();
  55. return $this->success([
  56. 'list' => $list,
  57. ]);
  58. } catch (\Exception $e) {
  59. return $this->error($e->getMessage());
  60. }
  61. }
  62. //地址列表
  63. public function address()
  64. {
  65. try {
  66. $params = request()->validate([
  67. 'member_id' => 'nullable',
  68. ]);
  69. $where = [];
  70. if (!empty($params['member_id'])) {
  71. $where[] = ['member_id', '=', $params['member_id']];
  72. }
  73. $list = Address::where($where)->get()->toArray();
  74. return $this->success([
  75. 'list' => $list,
  76. ]);
  77. } catch (\Exception $e) {
  78. return $this->error($e->getMessage());
  79. }
  80. }
  81. public function getChangeTypes()
  82. {
  83. return $this->success(BalanceLogService::$manualRecharge);
  84. }
  85. /**
  86. * @api {get} /admin/wallet/getPendingTasks 待处理任务
  87. * @apiDescription 后台每隔10秒,轮询请求这个接口获取待处理的任务数,如果有待处理的任务,则播放音乐
  88. * @apiGroup 充值管理
  89. * @apiUse result
  90. * @apiUse header
  91. * @apiVersion 1.0.0
  92. *
  93. * @apiSuccess (data) {Object} data
  94. * @apiSuccess (data) {int} data.recharge_task 待处理充值任务
  95. * @apiSuccess (data) {int} data.withdraw_task 待处理提现任务
  96. */
  97. function getPendingTasks()
  98. {
  99. $data = [
  100. 'recharge_task' => 0,
  101. 'withdraw_task' => 0,
  102. 'rmb_withdraw_task' => 0,
  103. ];
  104. if (MenuService::checkMenu(request()->user->id, 'withdraw/topUp')) {
  105. $data['recharge_task'] = Recharge::where('type', 2)->where('status', 0)->count();
  106. }
  107. if (MenuService::checkMenu(request()->user->id, 'listUSDT')) {
  108. $data['withdraw_task'] = Withdraw::where('status', 0)->count();
  109. }
  110. if (MenuService::checkMenu(request()->user->id, 'listRMB')) {
  111. $data['rmb_withdraw_task'] = PaymentOrder::where('type', 2)->where('status', 0)->count();
  112. }
  113. return $this->success($data);
  114. }
  115. //扣款
  116. public function debiting()
  117. {
  118. DB::beginTransaction();
  119. try {
  120. request()->validate([
  121. 'amount' => ['required', 'numeric', 'min:0.01'],
  122. 'member_id' => ['required', 'string', 'min:1'],
  123. 'remark' => ['required', 'string', 'min:1'],
  124. // 人工充值按平台倍数扣除打码量;其他类型扣除 wager_amount;不传不调整打码量
  125. 'recharge_type' => ['nullable', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
  126. 'wager_amount' => ['nullable', 'numeric', 'min:0'],
  127. ]);
  128. $memberId = request()->input('member_id');
  129. $amount = request()->input('amount');
  130. [$wagerDelta, $writeWager] = ManualAuditService::wagerDelta(true, (string)$amount, ManualAuditService::DEBIT_CHANGE_TYPE,
  131. request()->input('recharge_type'), request()->filled('wager_amount') ? (string)request()->input('wager_amount') : null);
  132. $amount = $amount * -1;
  133. $remark = request()->input('remark');
  134. $key = 'api_request_' . md5($memberId . json_encode([
  135. 'amount' => $amount,
  136. 'remark' => $remark,
  137. 'action' => "wallet/debiting"
  138. ]));
  139. if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
  140. Cache::put($key, true, 3);
  141. $wallet = WalletModel::where('member_id', $memberId)->lockForUpdate()->first();
  142. if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  143. $beforeBalance = (string)$wallet->available_balance;
  144. $availableBalance = bcadd($beforeBalance, $amount, 10);
  145. if ($availableBalance < 0) throw new Exception('可用余额不足', HttpStatus::CUSTOM_ERROR);
  146. $wallet->available_balance = $availableBalance;
  147. $wallet->save();
  148. $log = BalanceLogService::addLog($memberId, $amount, $beforeBalance, $availableBalance, "人工扣款", null, $remark);
  149. if ($writeWager) {
  150. WageringService::addRequirement($wallet, $wagerDelta, 'manual_debit', 'balance_log:' . $log->id, $remark, (int)request()->user->id);
  151. }
  152. DB::commit();
  153. } catch (ValidationException $e) {
  154. DB::rollBack();
  155. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  156. } catch (Exception $e) {
  157. DB::rollBack();
  158. return $this->error(intval($e->getCode()), $e->getMessage());
  159. }
  160. $availableBalance = floatval($availableBalance);
  161. // 去除多余0后,再用 sprintf 补足两位
  162. $availableBalance = sprintf('%.2f', $availableBalance);
  163. TopUpService::notifyTransferSuccess($memberId, "您的账户余额更新:" . ($amount > 0 ? '+' : '') . "{$amount} \n总余额为:{$availableBalance}");
  164. return $this->success();
  165. }
  166. //会员账号充值
  167. public function topUp()
  168. {
  169. DB::beginTransaction();
  170. try {
  171. request()->validate([
  172. 'amount' => ['required', 'numeric', 'min:0.01'],
  173. 'member_id' => ['required', 'string', 'min:1'],
  174. 'remark' => ['required', 'string', 'min:1'],
  175. 'change_type' => ['required', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
  176. // 人工充值按平台倍数自动计算;其他类型自行填写,留空为0
  177. 'wager_amount' => ['nullable', 'numeric', 'min:0'],
  178. ]);
  179. $memberId = request()->input('member_id');
  180. $amount = request()->input('amount');
  181. $remark = request()->input('remark');
  182. $changeType = request()->input('change_type');
  183. [$wagerDelta, $writeWager] = ManualAuditService::wagerDelta(false, (string)$amount, (string)$changeType, null,
  184. request()->filled('wager_amount') ? (string)request()->input('wager_amount') : null);
  185. $key = 'api_request_' . md5($memberId . json_encode([
  186. 'amount' => $amount,
  187. 'remark' => $remark,
  188. 'action' => "wallet/topUp"
  189. ]));
  190. if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
  191. Cache::put($key, true, 3);
  192. $wallet = WalletModel::where('member_id', $memberId)->lockForUpdate()->first();
  193. if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  194. $beforeBalance = (string)$wallet->available_balance;
  195. $availableBalance = bcadd($beforeBalance, $amount, 10);
  196. // 先保存余额再写日志:充值钩子(首充奖励、即充即送、打码量)会重新锁定并更新钱包
  197. $wallet->available_balance = $availableBalance;
  198. $wallet->save();
  199. $log = BalanceLogService::addLog($memberId, $amount, $beforeBalance, $availableBalance, $changeType, null, $remark);
  200. if ($writeWager) {
  201. $wallet = WalletModel::where('member_id', $memberId)->lockForUpdate()->first();
  202. WageringService::addRequirement($wallet, $wagerDelta, 'manual_credit', 'balance_log:' . $log->id, $remark, (int)request()->user->id);
  203. }
  204. DB::commit();
  205. } catch (ValidationException $e) {
  206. DB::rollBack();
  207. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  208. } catch (Exception $e) {
  209. DB::rollBack();
  210. return $this->error(intval($e->getCode()), $e->getMessage());
  211. }
  212. $availableBalance = floatval($availableBalance);
  213. // 去除多余0后,再用 sprintf 补足两位
  214. $availableBalance = sprintf('%.2f', $availableBalance);
  215. TopUpService::notifyTransferSuccess($memberId, "您的账户余额更新:" . ($amount > 0 ? '+' : '') . "{$amount} \n总余额为:{$availableBalance}");
  216. return $this->success();
  217. }
  218. //订单充值
  219. public function orderTopUp()
  220. {
  221. DB::beginTransaction();
  222. try {
  223. request()->validate([
  224. 'amount' => ['required', 'numeric', 'min:0.01'],
  225. 'remark' => ['required', 'string', 'min:1'],
  226. 'order_type' => ['nullable'],
  227. 'order_id' => ['nullable'],
  228. ]);
  229. $order_type = request()->input('order_type');
  230. $order_id = request()->input('order_id');
  231. $amount = request()->input('amount');
  232. $remark = request()->input('remark');
  233. // 订单补偿单独记类型:不算充值(等级、首充、返现等),报表仍计入人工加款
  234. $changeType = "订单补偿";
  235. $key = 'api_request_' . md5($order_id . json_encode([
  236. 'amount' => $amount,
  237. 'remark' => $remark,
  238. 'action' => "wallet/orderTopUp"
  239. ]));
  240. if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
  241. Cache::put($key, true, 3);
  242. if ($order_type == 'sport') {
  243. $info = Order::where('id', $order_id)->first();
  244. } elseif ($order_type == 'lhc') {
  245. $info = LhcOrder::where('id', $order_id)->first();
  246. }
  247. if (!$info) {
  248. throw new \Exception('订单不存在');
  249. } else {
  250. $info->remark = $info->remark ? $info->remark. '|' . $remark : $remark;
  251. $info->save();
  252. }
  253. $memberId = $order_type == 'sport' ? $info->user_id : $info->member_id;
  254. $wallet = WalletModel::where('member_id', $memberId)->first();
  255. if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  256. $availableBalance = bcadd($wallet->available_balance, $amount, 10);
  257. BalanceLogService::addLog($memberId, $amount, $wallet->available_balance, $availableBalance, $changeType, null, $remark);
  258. $wallet->available_balance = $availableBalance;
  259. $wallet->save();
  260. DB::commit();
  261. } catch (ValidationException $e) {
  262. DB::rollBack();
  263. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  264. } catch (Exception $e) {
  265. DB::rollBack();
  266. return $this->error(intval($e->getCode()), $e->getMessage());
  267. }
  268. return $this->success();
  269. }
  270. //订单扣款
  271. public function orderDebiting()
  272. {
  273. DB::beginTransaction();
  274. try {
  275. request()->validate([
  276. 'amount' => ['required', 'numeric', 'min:0.01'],
  277. 'remark' => ['required', 'string', 'min:1'],
  278. 'order_type' => ['required'],
  279. 'order_id' => ['required'],
  280. ]);
  281. $order_type = request()->input('order_type');
  282. $order_id = request()->input('order_id');
  283. $amount = request()->input('amount');
  284. $amount = $amount * -1;
  285. $remark = request()->input('remark');
  286. $key = 'api_request_' . md5($order_id . json_encode([
  287. 'amount' => $amount,
  288. 'remark' => $remark,
  289. 'action' => "wallet/orderDebiting"
  290. ]));
  291. if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
  292. Cache::put($key, true, 3);
  293. if ($order_type == 'sport') {
  294. $info = Order::where('id', $order_id)->first();
  295. } elseif ($order_type == 'lhc') {
  296. $info = LhcOrder::where('id', $order_id)->first();
  297. }
  298. if (!$info) {
  299. throw new \Exception('订单不存在');
  300. } else {
  301. $info->remark = $info->remark ? $info->remark. '|' . $remark : $remark;
  302. $info->save();
  303. }
  304. $memberId = $order_type == 'sport' ? $info->user_id : $info->member_id;
  305. $wallet = WalletModel::where('member_id', $memberId)->first();
  306. if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  307. $availableBalance = bcadd($wallet->available_balance, $amount, 10);
  308. if ($availableBalance < 0) throw new Exception('可用余额不足', HttpStatus::CUSTOM_ERROR);
  309. BalanceLogService::addLog($memberId, $amount, $wallet->available_balance, $availableBalance, "人工扣款", null, $remark);
  310. $wallet->available_balance = $availableBalance;
  311. $wallet->save();
  312. DB::commit();
  313. } catch (ValidationException $e) {
  314. DB::rollBack();
  315. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  316. } catch (Exception $e) {
  317. DB::rollBack();
  318. return $this->error(intval($e->getCode()), $e->getMessage());
  319. }
  320. return $this->success();
  321. }
  322. /**
  323. * @api {post} /admin/wallet/verifyRecharge 审核
  324. * @apiGroup 充值管理
  325. * @apiUse result
  326. * @apiUse header
  327. * @apiVersion 1.0.0
  328. * @apiParam {int} id 充值表的ID
  329. * @apiParam {int} status 状态
  330. * - 1 通过
  331. * - 2 拒绝
  332. * @apiParam {string} [remark] 说明
  333. * - 当status =2 时,此参数必填
  334. */
  335. public
  336. function verifyRecharge()
  337. {
  338. DB::beginTransaction();
  339. try {
  340. request()->validate([
  341. 'id' => ['required', 'integer', 'min:1'],
  342. 'status' => ['required', 'integer', 'in:1,2'],
  343. 'remark' => ['required_if:status,2', 'string', 'min:1']
  344. ]);
  345. $id = request()->input('id');
  346. $status = request()->input('status');
  347. $remark = request()->input('remark', '');
  348. $recharge = Recharge::where('id', $id)
  349. ->where('type', 2)
  350. ->where('status', 0)->lockForUpdate()->first();
  351. if (!$recharge) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  352. $amount = floatval($recharge->amount);
  353. if ($status == 2) {
  354. $recharge->status = 2;
  355. $recharge->save();
  356. $text = "充值结果通知\n";
  357. $text .= "充值数量:{$amount} USDT\n";
  358. $text .= "充值地址:{$recharge->to_address}\n";
  359. $text .= "状态:失败\n";
  360. $text .= "原因:{$remark}\n";
  361. TopUpService::notifyTransferSuccess($recharge->member_id, $text);
  362. } else {
  363. // 汇率
  364. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  365. $recharge->status = 1;
  366. $recharge->exchange_rate = $rate;
  367. $recharge->save();
  368. $wallet = WalletModel::where('member_id', $recharge->member_id)
  369. ->lockForUpdate()->first();
  370. $rate_amount = bcmul($amount, $rate, 10); // 汇率转换后分数
  371. $available_balance = bcadd($wallet->available_balance, $rate_amount, 10);
  372. $beforeBalance = (string)$wallet->available_balance;
  373. $wallet->available_balance = $available_balance;
  374. $wallet->save();
  375. $principal = BalanceLogService::addLog($recharge->member_id, $rate_amount, $beforeBalance, $available_balance, "充值", $recharge->id, "用户充值后台审核到账");
  376. \App\Services\RechargeBonusService::grant('recharge', $recharge, $principal);
  377. $text = "充值结果通知\n";
  378. $text .= "充值数量:{$amount} USDT\n";
  379. $text .= "充值地址:{$recharge->to_address}\n";
  380. $text .= "汇率:1 USDT = {$rate} RMB\n";
  381. $text .= "折合金额:" . number_format($rate_amount, 2) . " RMB\n";
  382. $text .= "状态:成功\n";
  383. TopUpService::notifyTransferSuccess($recharge->member_id, $text);
  384. }
  385. DB::commit();
  386. } catch (ValidationException $e) {
  387. DB::rollBack();
  388. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  389. } catch (Exception $e) {
  390. DB::rollBack();
  391. return $this->error(intval($e->getCode()), $e->getMessage());
  392. }
  393. return $this->success();
  394. }
  395. /**
  396. * @api {get} /admin/wallet 充值列表
  397. * @apiGroup 充值管理
  398. * @apiDescription 如果列表的type=2 并且 status = 0 <br/>那么代表这条数据是需要后台手动审核的
  399. * 需要有审核按钮,审核通过或者拒绝,其它情况则不需要审核按钮
  400. *
  401. * @apiUse result
  402. * @apiUse header
  403. * @apiVersion 1.0.0
  404. *
  405. * @apiParam {int} [page=1]
  406. * @apiParam {int} [limit=10]
  407. * @apiParam {string} [member_id] 房主 tg会员ID
  408. * @apiParam {int} [status] 状态
  409. * - 0 待确认
  410. * - 1 已确认
  411. * - 2 失败
  412. * - 3 已忽略
  413. * @apiSuccess (data) {Object} data
  414. * @apiSuccess (data) {int} data.total 数量
  415. * @apiSuccess (data) {Object[]} data.data 列表
  416. * @apiSuccess (data) {int} data.data.id
  417. * @apiSuccess (data) {int} data.data.member_id tg会员id
  418. * @apiSuccess (data) {string} data.data.net 链接类型
  419. * @apiSuccess (data) {string} data.data.coin 币种
  420. * @apiSuccess (data) {string} data.data.amount 充值数量
  421. * @apiSuccess (data) {string} data.data.to_address 充值地址(平台地址)
  422. * @apiSuccess (data) {string} data.data.from_address 转出地址(用户发起地址)
  423. * @apiSuccess (data) {string} data.data.txid 链上交易哈希
  424. * @apiSuccess (data) {string} data.data.block_time 区块时间
  425. * @apiSuccess (data) {string} data.data.block_height
  426. * @apiSuccess (data) {int} data.data.confirmations 确认数
  427. * @apiSuccess (data) {string} data.data.updated_at
  428. * @apiSuccess (data) {string} data.data.created_at
  429. * @apiSuccess (data) {int} data.data.status 状态
  430. * - 0 待确认
  431. * - 1 已确认
  432. * - 2 失败
  433. * - 3 已忽略
  434. * @apiSuccess (data) {int} data.data.type 充值类型
  435. * - 1 自动
  436. * - 2 手动
  437. * @apiSuccess (data) {string} data.data.image 充值转账凭证图片
  438. */
  439. public
  440. function index()
  441. {
  442. try {
  443. request()->validate([
  444. 'member_id' => ['nullable', 'string', 'min:1'],
  445. 'status' => ['nullable', 'string', 'min:0', 'max:3'],
  446. 'type' => ['nullable', 'integer', 'in:1,2'],
  447. 'account' => ['nullable', 'string', 'max:64'],
  448. 'first_name' => ['nullable'],
  449. ]);
  450. $search = request()->all();
  451. $result = RechargeService::paginate($search);
  452. foreach($result['data'] as &$item) {
  453. if ($item['from'] == 2) {
  454. $item['image'] = str_replace(config('app.url'), config('app.football_url').'/', $item['image']);
  455. }
  456. }
  457. } catch (ValidationException $e) {
  458. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  459. } catch (Exception $e) {
  460. return $this->error(intval($e->getCode()));
  461. }
  462. return $this->success($result);
  463. }
  464. }