Wallet.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Constants\Util;
  4. use App\Constants\HttpStatus;
  5. use App\Services\WalletService;
  6. use App\Services\UsdtRechargeConfigService;
  7. use App\Services\ConfigService;
  8. use App\Models\Config;
  9. use App\Models\Recharge;
  10. use App\Models\Wallet as WalletModel;
  11. use App\Models\Withdraw;
  12. use App\Models\User;
  13. use App\Models\Bank;
  14. use App\Models\Address;
  15. use App\Models\RechargeChannel;
  16. use App\Models\PaymentOrder;
  17. use App\Services\BalanceLogService;
  18. use App\Services\WithdrawFundsService;
  19. use App\Services\WithdrawalName;
  20. use App\Services\WithdrawalDestination;
  21. use App\Services\PaymentOrderService;
  22. use App\Services\Payment\JdPayService;
  23. use App\Services\Payment\NoPayService;
  24. use App\Services\Payment\ZimuPayService;
  25. use App\Services\QianBaoWithdrawService;
  26. use App\Services\WithdrawService;
  27. use App\Services\LogService;
  28. use Illuminate\Support\Facades\DB;
  29. use Illuminate\Validation\ValidationException;
  30. use Exception;
  31. /**
  32. * 充值提现接口
  33. */
  34. class Wallet extends BaseController
  35. {
  36. //获取三斤充值通道(微信、支付宝、扫码充值)
  37. public function getChannel()
  38. {
  39. try {
  40. $params = request()->validate(['currency_type' => ['nullable', 'string', 'in:crypto,fiat']],
  41. ['currency_type.in' => '充值渠道分类仅支持crypto或fiat']);
  42. } catch (ValidationException $e) {
  43. return $this->error($e->validator->errors()->first(), [], HttpStatus::CUSTOM_ERROR);
  44. }
  45. $member_id = request()->user->member_id;
  46. if (empty(request()->user->recharge_channel_group_id)) {
  47. $recharge_channel_group_id = User::where('member_id', $member_id)->value('recharge_channel_group_id');
  48. } else {
  49. $recharge_channel_group_id = request()->user->recharge_channel_group_id;
  50. }
  51. $list = RechargeChannel::getFormatChannel(1, $recharge_channel_group_id);
  52. if (!JdPayService::canUserRecharge(request()->user->id)) {
  53. $list = array_values(array_filter($list, function ($item) {
  54. return !JdPayService::isChannel($item['value'] ?? '');
  55. }));
  56. }
  57. if (!NoPayService::canUserRecharge(request()->user->id)) {
  58. $list = array_values(array_filter($list, function ($item) {
  59. return !NoPayService::isRechargeChannel($item['value'] ?? '');
  60. }));
  61. }
  62. if (!ZimuPayService::canUserRecharge(request()->user->id)) {
  63. $list = array_values(array_filter($list, function ($item) {
  64. return !ZimuPayService::isRechargeChannel($item['value'] ?? '');
  65. }));
  66. }
  67. $currencyType = $params['currency_type'] ?? null;
  68. if ($currencyType !== null && $currencyType !== '') {
  69. $list = array_values(array_filter($list, static function ($item) use ($currencyType) {
  70. return (($item['value'] ?? '') === 'usdt') === ($currencyType === 'crypto');
  71. }));
  72. }
  73. $list = \App\Services\RechargeBonusService::decorate($list, $this->lang, (int)request()->user->from === 1);
  74. return $this->success([
  75. 'list' => $list,
  76. ]);
  77. }
  78. /**
  79. * 创建代收订单(自动扫码充值)
  80. */
  81. public function createPay()
  82. {
  83. try {
  84. $params = request()->validate([
  85. 'amount' => ['required', 'numeric', 'min:0.01'],
  86. 'payment_type' => ['required_without:channel_id', 'nullable', 'string'],
  87. 'channel_id' => ['nullable', 'integer', 'min:1'],
  88. ]);
  89. $channelId = !empty($params['channel_id']) ? (int)$params['channel_id'] : null;
  90. if ($channelId) {
  91. $channelType = RechargeChannel::query()->where('data_type', 1)->whereKey($channelId)->value('type');
  92. if ($channelType === null) {
  93. throw new Exception(lang("充值通道不存在"));
  94. }
  95. if (!empty($params['payment_type']) && $params['payment_type'] !== $channelType) {
  96. throw new Exception(lang("充值通道与充值方式不匹配"));
  97. }
  98. $params['payment_type'] = $channelType;
  99. }
  100. if ($params['payment_type'] == 'rgcz') {
  101. return $this->error('参数有误');
  102. }
  103. $member_id = request()->user->member_id;
  104. $user = User::where('member_id', $member_id)->first();
  105. if (JdPayService::isChannel($params['payment_type']) && !JdPayService::canUserRecharge($user->id)) {
  106. throw new Exception(lang("不支持此充值方式"));
  107. }
  108. if (NoPayService::isRechargeChannel($params['payment_type']) && !NoPayService::canUserRecharge($user->id)) {
  109. throw new Exception(lang("不支持此充值方式"));
  110. }
  111. if (ZimuPayService::isRechargeChannel($params['payment_type']) && !ZimuPayService::canUserRecharge($user->id)) {
  112. throw new Exception(lang("不支持此充值方式"));
  113. }
  114. //校验是否支持此充值方式
  115. $check = RechargeChannel::checkRechargeChannel($params['payment_type'], $user->recharge_channel_group_id);
  116. if ($check === false) {
  117. throw new Exception(lang("不支持此充值方式").$user->recharge_channel_group_id);
  118. }
  119. $res = PaymentOrderService::createPay($member_id, $params['amount'], $params['payment_type'], $channelId);
  120. if ($res['code'] == 0) {
  121. return $this->success($res);
  122. }
  123. return $this->error($res['text']);
  124. } catch (ValidationException $e) {
  125. return $this->error($e->validator->errors()->first());
  126. } catch (\Exception $e) {
  127. return $this->error($e->getMessage());
  128. }
  129. }
  130. /**
  131. * 获取充值二维码(USDT充值)
  132. */
  133. public function scan()
  134. {
  135. try {
  136. $member_id = request()->user->member_id;
  137. $params = request()->validate([
  138. 'type' => ['required', 'string'],
  139. ]);
  140. if (strtolower($params['type']) === "trc20") {
  141. $address = Config::where('field', 'receiving_address')->first()->val;
  142. } elseif (strtolower($params['type']) === "erc20") {
  143. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  144. } else {
  145. return $this->error(lang('充值类型错误'));
  146. }
  147. $receivingType = ConfigService::getVal("receiving_type");
  148. //自动充值
  149. if ($receivingType == 1) {
  150. $res = WalletService::getRechargeImageAddress($member_id);
  151. $address = $res['address'];
  152. $qrCode = $res['full_path'];
  153. } else {
  154. //手动充值
  155. $res = WalletService::getPlatformImageAddress($address);
  156. $res['net'] = $params['type'];
  157. $qrCode = $res['full_path'];
  158. }
  159. return $this->success([
  160. 'qrcode' => $qrCode,
  161. 'address' => $address,
  162. ] + (new UsdtRechargeConfigService())->get());
  163. } catch (ValidationException $e) {
  164. return $this->error($e->validator->errors()->first());
  165. } catch (\Exception $e) {
  166. return $this->error($e->getMessage());
  167. }
  168. }
  169. /**
  170. * 提交充值凭证(USDT手动充值)
  171. */
  172. public function recharge()
  173. {
  174. try {
  175. $params = request()->validate([
  176. 'net' => ['required', 'string'],
  177. 'amount' => ['required', 'numeric', 'min:0.01'],
  178. 'toAddress' => ['required', 'string'],
  179. 'image' => ['required', 'url'],
  180. ]);
  181. $member_id = request()->user->member_id;
  182. $user = User::where('member_id', $member_id)->first();
  183. //校验是否支持此充值方式
  184. $check = RechargeChannel::checkRechargeChannel('usdt', $user->recharge_channel_group_id);
  185. if ($check === false) {
  186. throw new Exception(lang("不支持此充值方式"));
  187. }
  188. $recharge = new Recharge();
  189. $recharge->member_id = $member_id;
  190. $recharge->net = $params['net'];
  191. $recharge->coin = "USDT";
  192. $recharge->amount = $params['amount'];
  193. $recharge->to_address = $params['toAddress'];
  194. $recharge->status = 0;
  195. $recharge->type = 2;
  196. $recharge->image = Util::replacePartInUrl($params['image']);
  197. $recharge->save();
  198. return $this->success($recharge,'提交成功');
  199. } catch (ValidationException $e) {
  200. return $this->error($e->validator->errors()->first());
  201. } catch (Exception $e) {
  202. return $this->error($e->getMessage());
  203. }
  204. }
  205. //支付宝、微信、银行卡手动充值
  206. public function paymentOrder()
  207. {
  208. try {
  209. $member_id = request()->user->member_id;
  210. $params = request()->validate([
  211. 'amount' => ['required', 'numeric', 'min:0.01'],
  212. 'payment_type' => ['required', 'integer'],
  213. ]);
  214. $member_id = request()->user->member_id;
  215. $user = User::where('member_id', $member_id)->first();
  216. //校验是否支持此充值方式
  217. $check = RechargeChannel::checkRechargeChannel('rgcz', $user->recharge_channel_group_id);
  218. if ($check === false) {
  219. throw new Exception(lang("不支持此充值方式"));
  220. }
  221. $data = [];
  222. $data['type'] = PaymentOrderService::TYPE_SELF_PAY;
  223. $data['order_no'] = PaymentOrderService::createOrderNo('rgcz_', $member_id);
  224. $data['member_id'] = $member_id;
  225. $data += PaymentOrderService::feeFields($params['amount'], 0);
  226. $data['amount'] = number_format($params['amount'], 2, '.', '');
  227. $data['payment_type'] = $params['payment_type'];
  228. $data['channel'] = 'rgcz';
  229. $data['status'] = PaymentOrderService::STATUS_STAY;
  230. $data['remark'] = '';
  231. // 创建待处理状态的提现记录
  232. $info = PaymentOrder::create($data);
  233. return $this->success($info,'提交成功,请等待人工回复');
  234. } catch (ValidationException $e) {
  235. return $this->error($e->validator->errors()->first());
  236. } catch (Exception $e) {
  237. return $this->error($e->getMessage());
  238. }
  239. }
  240. //支付宝、微信、银行卡手动充值提交图片
  241. public function submitImage()
  242. {
  243. try {
  244. $member_id = request()->user->member_id;
  245. $params = request()->validate([
  246. 'id' => ['required', 'integer'],
  247. 'image' => ['required', 'url'],
  248. ]);
  249. // 创建待处理状态的提现记录
  250. $info = PaymentOrder::where('id', $params['id'])->where('member_id', $member_id)->first();
  251. if (!$info) {
  252. return $this->error(lang('找不到此记录'));
  253. }
  254. if ($info->status != PaymentOrderService::STATUS_USER) {
  255. return $this->error(lang('待处理中,请稍后'));
  256. }
  257. $info->image = $params['image'];
  258. $info->status = PaymentOrderService::STATUS_AUDIT;
  259. $info->save();
  260. return $this->success($info,'提交成功,请等待人工审核');
  261. } catch (ValidationException $e) {
  262. return $this->error($e->validator->errors()->first());
  263. } catch (Exception $e) {
  264. return $this->error($e->getMessage());
  265. }
  266. }
  267. /**
  268. * 获取提现通道
  269. */
  270. public function withdrawChannel()
  271. {
  272. $member_id = request()->user->member_id;
  273. if (empty(request()->user->recharge_channel_group_id)) {
  274. $recharge_channel_group_id = User::where('member_id', $member_id)->value('recharge_channel_group_id');
  275. } else {
  276. $recharge_channel_group_id = request()->user->recharge_channel_group_id;
  277. }
  278. $list = WithdrawalDestination::describeChannels(RechargeChannel::getFormatChannel(2, $recharge_channel_group_id));
  279. return $this->success($list);
  280. }
  281. //USDT提现
  282. public function withdraw()
  283. {
  284. $error_code = -1;
  285. try {
  286. $member_id = request()->user->member_id;
  287. $params = request()->validate([
  288. 'amount' => ['required', 'numeric', 'min:0.01'],
  289. 'address' => ['required', 'string'],
  290. 'safe_word' => ['required'],
  291. ]);
  292. $user = User::where('member_id', $member_id)->first();
  293. if ($user->from == 2) {
  294. throw new Exception(lang("游客不支持提现"));
  295. }
  296. if (empty($user->payment_password)) {
  297. $error_code = 10011;
  298. throw new Exception(lang("请先设置资金密码"));
  299. }
  300. //校验资金密码
  301. if (!password_verify($params['safe_word'], $user->payment_password)) {
  302. throw new Exception(lang('资金密码错误'));
  303. }
  304. WithdrawalName::required($user);
  305. //校验是否支持此提现方式
  306. $check = RechargeChannel::checkWithdrawChannel('usdt', $user->recharge_channel_group_id);
  307. if ($check === false) {
  308. throw new Exception(lang("不支持此提现方式"));
  309. }
  310. $amount = $params['amount'];
  311. $address = $params['address'];
  312. //校验提现金额的限制
  313. if ($amount < $check->min || $amount > $check->max) {
  314. throw new Exception(lang("提现金额超出限制"));
  315. }
  316. $serviceCharge = (new WithdrawService())->serviceCharge;
  317. $real = bcsub($amount, $serviceCharge, 10);
  318. $real = floatval($real);
  319. if ($amount <= $serviceCharge) {
  320. throw new Exception(lang("提现不能少于") . "{$serviceCharge} USDT");
  321. }
  322. $wallet = WalletModel::where('member_id', $member_id)->first();
  323. $temp = floatval($wallet->available_balance);
  324. // 汇率
  325. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  326. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  327. $rate = bcadd($rate, $exchange_rate_difference, 2);
  328. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  329. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  330. if ($amount > $rate_usdt_amount) {
  331. throw new Exception(lang("余额不足") . "{$serviceCharge} USDT");
  332. }
  333. // 校验打码量 → 可用余额转入冻结余额 → 创建提现单 → 重置打码周期
  334. [$withdraw] = WithdrawFundsService::hold($member_id, $rate_rmb_amount, '提现', '', WithdrawFundsService::WITHDRAWS,
  335. fn ($before, $after) => Withdraw::create([
  336. 'member_id' => $member_id,
  337. 'amount' => $amount,
  338. 'service_charge' => $serviceCharge,
  339. 'to_account' => $real,
  340. 'address' => $address,
  341. 'exchange_rate' => $rate,
  342. 'status' => 0,
  343. 'after_balance' => $after,
  344. ]));
  345. return $this->success($withdraw,'提交成功');
  346. } catch (ValidationException $e) {
  347. return $this->error($e->validator->errors()->first());
  348. } catch (\Exception $e) {
  349. return $this->error($e->getMessage(), [], $error_code);
  350. }
  351. }
  352. /**
  353. * 提现(手动到账): DF001 支付宝转卡; DF002 支付宝转支付宝; DF005数字人民币; JDpay JD钱包; NOwithdraw NO钱包; ZIMUwithdraw 808币钱包地址提现;ZIMUcash 808人民币代付; rgtx(人工提现,手动打款)
  354. */
  355. public function payout() {
  356. $error_code = -1;
  357. try {
  358. $params = request()->validate([
  359. 'amount' => ['required', 'numeric', 'min:0.01'],
  360. 'channel' => ['required', 'string', 'in:' . WithdrawalName::CHANNELS],
  361. 'bank_name' => ['nullable', 'string', 'max:255'],
  362. 'bank_branch' => ['nullable', 'string', 'max:255'],
  363. 'card_no' => ['nullable', 'string', 'max:255'],
  364. 'payout_method' => ['nullable', 'string', 'in:' . implode(',', array_keys(WithdrawalDestination::MANUAL_METHODS))],
  365. 'qr_image' => ['nullable', 'string', 'max:255'],
  366. 'safe_word' => ['required'],
  367. ]);
  368. $member_id = request()->user->member_id;
  369. $user = User::where('member_id', $member_id)->first();
  370. if ($user->from == 2) {
  371. throw new Exception(lang("游客不支持提现"));
  372. }
  373. if (empty($user->payment_password)) {
  374. $error_code = 10011;
  375. throw new Exception(lang("请先设置资金密码"));
  376. }
  377. //校验资金密码
  378. if (!password_verify($params['safe_word'], $user->payment_password)) {
  379. throw new Exception(lang('资金密码错误'));
  380. }
  381. //校验是否支持此提现方式
  382. $check = RechargeChannel::checkWithdrawChannel($params['channel'], $user->recharge_channel_group_id);
  383. if ($check === false) {
  384. throw new Exception(lang("不支持此提现方式"));
  385. }
  386. $channel = $params['channel'];
  387. $destination = WithdrawalDestination::resolve($channel, $params);
  388. $amount = $params['amount'];
  389. //校验提现金额的限制
  390. if ($amount < $check->min || $amount > $check->max) {
  391. throw new Exception(lang("提现金额超出限制"));
  392. }
  393. if ($channel === 'rgtx') {
  394. try {
  395. $data = [];
  396. $data['type'] = PaymentOrderService::TYPE_SELF_PAYOUT;
  397. $data['order_no'] = PaymentOrderService::createOrderNo('rgtx' . $data['type'] . '_', $member_id);
  398. $data['member_id'] = $member_id;
  399. // 人工提现暂不记三方手续费(全额出款,不扣客户)。
  400. $data += PaymentOrderService::feeFields($amount, 0);
  401. $data['amount'] = number_format($amount, 2, '.', '');
  402. $data['channel'] = $params['channel'];
  403. $data['bank_name'] = $destination['bank_name'];
  404. $data['bank_branch'] = $destination['bank_branch'] ?? null;
  405. $data['account'] = WithdrawalName::forPayout($user, $channel);
  406. $data['card_no'] = $destination['card_no'];
  407. $data['qr_image'] = $destination['qr_image'] ?? null;
  408. $data['status'] = PaymentOrderService::STATUS_STAY;
  409. // 校验打码量 → 冻结资金 → 创建待处理的提现记录 → 重置打码周期
  410. [$info] = WithdrawFundsService::hold($member_id, $amount, '人工提现', '',
  411. WithdrawFundsService::PAYMENT_ORDERS, function () use ($data, $member_id) {
  412. $pending = PaymentOrder::where('type', PaymentOrderService::TYPE_SELF_PAYOUT)
  413. ->where('member_id', $member_id)->whereIn('status', [0, 1, 4, 5])
  414. ->lockForUpdate()->first(['id']);
  415. if ($pending) {
  416. throw new Exception(lang('您已有1笔提现订单正常处理中!'), HttpStatus::CUSTOM_ERROR);
  417. }
  418. return PaymentOrder::create($data);
  419. });
  420. } catch (Exception $e) {
  421. if ($e->getCode() === HttpStatus::CUSTOM_ERROR) {
  422. return $this->error($e->getMessage());
  423. }
  424. LogService::error($e);
  425. return $this->error('系统繁忙,请稍后重试!');
  426. }
  427. return $this->success(array_replace($info->toArray(), WithdrawalDestination::manualResponse(
  428. $destination['bank_name'], $destination['card_no'], $destination['qr_image'] ?? null
  429. )), '提交成功');
  430. } else {
  431. $res = QianBaoWithdrawService::createOrder(
  432. $member_id, $params['amount'], $channel, $destination['bank_name'], $destination['card_no'],
  433. $destination['bank_branch']
  434. );
  435. if ($res['code'] == 0) {
  436. return $this->success($res,'提交成功');
  437. }
  438. return $this->error($res['text']);
  439. }
  440. } catch (ValidationException $e) {
  441. return $this->error($e->validator->errors()->first());
  442. } catch (\Exception $e) {
  443. return $this->error($e->getMessage(), [], $error_code);
  444. }
  445. }
  446. /**
  447. * 提现(自动到账): DF001 支付宝转卡/DF002 支付宝转支付宝
  448. */
  449. public function autoPayout()
  450. {
  451. try {
  452. $params = request()->validate([
  453. 'amount' => ['required', 'numeric', 'min:0.01'],
  454. 'channel' => ['required', 'string'],
  455. 'bank_name' => ['nullable', 'string', 'max:255'],
  456. 'bank_branch' => ['nullable', 'string', 'max:255'],
  457. 'card_no' => ['required', 'string', 'max:255'],
  458. ]);
  459. $member_id = request()->user->member_id;
  460. $destination = WithdrawalDestination::resolve($params['channel'], $params);
  461. $res = PaymentOrderService::autoCreatePayout(
  462. $member_id, $params['amount'], $params['channel'], $destination['bank_name'], $destination['card_no'],
  463. $destination['bank_branch'] ?? null
  464. );
  465. if (empty($res['text'])) {
  466. return $this->success($res,'提交成功');
  467. }
  468. return $this->error($res['text']);
  469. } catch (ValidationException $e) {
  470. return $this->error($e->validator->errors()->first());
  471. } catch (Exception $e) {
  472. return $this->error($e->getMessage());
  473. }
  474. }
  475. public function addBank()
  476. {
  477. try {
  478. $params = request()->validate([
  479. 'id' => 'nullable|integer',
  480. 'channel' => 'required|string|in:' . WithdrawalName::CHANNELS,
  481. 'account' => 'nullable|string',
  482. 'card_no' => 'nullable|string|max:255',
  483. 'bank_name' => 'nullable|string|max:255',
  484. 'bank_branch' => 'nullable|string|max:255',
  485. 'payout_method' => 'nullable|string|in:' . implode(',', array_keys(WithdrawalDestination::MANUAL_METHODS)),
  486. 'qr_image' => 'nullable|string|max:255',
  487. ]);
  488. $member_id = request()->user->member_id;
  489. $destination = WithdrawalDestination::resolve($params['channel'], $params);
  490. $realName = DB::transaction(function () use ($member_id, $params, $destination) {
  491. $user = User::where('member_id', $member_id)->lockForUpdate()->firstOrFail();
  492. $accounts = Bank::where('member_id', $member_id);
  493. if (!empty($params['id'])) {
  494. $info = $accounts->where('id', $params['id'])->lockForUpdate()->first();
  495. if (!$info) throw new Exception(lang('找不到此记录'));
  496. } else {
  497. if ($accounts->where('channel', $params['channel'])->count() >= 5) {
  498. throw new Exception(lang('已达添加上限'));
  499. }
  500. $info = new Bank();
  501. }
  502. $name = WithdrawalName::bind($user, $params['account'] ?? null);
  503. if (!$info->fill([
  504. 'member_id' => $member_id,
  505. 'channel' => $params['channel'],
  506. 'account' => WithdrawalName::usesOrderName($params['channel']) ? $name : '',
  507. 'card_no' => $destination['card_no'],
  508. 'bank_name' => $destination['bank_name'],
  509. 'bank_branch' => $destination['bank_branch'] ?? null,
  510. 'qr_image' => $destination['qr_image'] ?? null,
  511. ])->save()) {
  512. throw new Exception(lang('系统繁忙,请稍后重试!'));
  513. }
  514. return $user->real_name;
  515. });
  516. return $this->success(['real_name' => $realName], '提交成功');
  517. } catch (ValidationException $e) {
  518. return $this->error($e->validator->errors()->first());
  519. } catch (\Exception $e) {
  520. return $this->error($e->getMessage());
  521. }
  522. }
  523. public function delBank()
  524. {
  525. try {
  526. $params = request()->validate([
  527. 'id' => 'required|integer',
  528. ]);
  529. $member_id = request()->user->member_id;
  530. $info = Bank::where('id', $params['id'])->where('member_id', $member_id)->first();
  531. if (empty($info)) throw new Exception(lang('找不到此记录'));
  532. $info->delete();
  533. return $this->success([],'删除成功');
  534. } catch (ValidationException $e) {
  535. return $this->error($e->validator->errors()->first());
  536. } catch (\Exception $e) {
  537. return $this->error($e->getMessage());
  538. }
  539. }
  540. public function bankList()
  541. {
  542. try {
  543. $params = request()->validate([
  544. 'channel' => 'nullable',
  545. ]);
  546. $member_id = request()->user->member_id;
  547. $realName = request()->user->real_name;
  548. $where = !empty($params['channel']) ? ['channel' => $params['channel']] : [];
  549. $list = Bank::where('member_id', $member_id)->where($where)->get()->toArray();
  550. foreach ($list as &$account) {
  551. if (WithdrawalName::usesOrderName($account['channel'])) {
  552. $account['account'] = $realName ?? '';
  553. }
  554. if ($account['channel'] === 'rgtx') {
  555. $account = array_replace($account, WithdrawalDestination::manualResponse(
  556. $account['bank_name'], $account['card_no'], $account['qr_image'] ?? null
  557. ));
  558. }
  559. }
  560. return $this->success([
  561. 'list' => $list,
  562. 'real_name' => $realName,
  563. ]);
  564. } catch (\Exception $e) {
  565. return $this->error($e->getMessage());
  566. }
  567. }
  568. public function addAddress()
  569. {
  570. try {
  571. $params = request()->validate([
  572. 'id' => 'nullable|integer',
  573. 'address' => 'required|string',
  574. 'account' => 'nullable|string',
  575. ]);
  576. $member_id = request()->user->member_id;
  577. $name = DB::transaction(function () use ($member_id, $params) {
  578. $user = User::where('member_id', $member_id)->lockForUpdate()->firstOrFail();
  579. $addresses = Address::where('member_id', $member_id);
  580. if (!empty($params['id'])) {
  581. $info = $addresses->where('id', $params['id'])->lockForUpdate()->first();
  582. if (!$info) throw new Exception(lang('找不到此记录'));
  583. } else {
  584. if ($addresses->where('address', $params['address'])->count() >= 5) {
  585. throw new Exception(lang('已达添加上限'));
  586. }
  587. $info = new Address();
  588. }
  589. $name = WithdrawalName::bind($user, $params['account'] ?? null);
  590. if (!$info->fill([
  591. 'member_id' => $member_id,
  592. 'address' => $params['address'],
  593. ])->save()) {
  594. throw new Exception(lang('系统繁忙,请稍后重试!'));
  595. }
  596. return $name;
  597. });
  598. return $this->success(['real_name' => $name], '提交成功');
  599. } catch (ValidationException $e) {
  600. return $this->error($e->validator->errors()->first());
  601. } catch (\Exception $e) {
  602. return $this->error($e->getMessage());
  603. }
  604. }
  605. public function delAddress()
  606. {
  607. try {
  608. $params = request()->validate([
  609. 'id' => 'required|integer',
  610. ]);
  611. $member_id = request()->user->member_id;
  612. $info = Address::where('id', $params['id'])->where('member_id', $member_id)->first();
  613. if (empty($info)) throw new Exception(lang('找不到此记录'));
  614. $info->delete();
  615. return $this->success([],'删除成功');
  616. } catch (ValidationException $e) {
  617. return $this->error($e->validator->errors()->first());
  618. } catch (\Exception $e) {
  619. return $this->error($e->getMessage());
  620. }
  621. }
  622. public function address()
  623. {
  624. try {
  625. $member_id = request()->user->member_id;
  626. $list = Address::where('member_id', $member_id)->get()->toArray();
  627. return $this->success([
  628. 'list' => $list,
  629. 'real_name' => request()->user->real_name,
  630. ]);
  631. } catch (\Exception $e) {
  632. return $this->error($e->getMessage());
  633. }
  634. }
  635. }