Wallet.php 30 KB

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