Wallet.php 19 KB

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