Wallet.php 27 KB

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