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\ConfigService;
  7. use App\Models\Config;
  8. use App\Models\Recharge;
  9. use App\Models\Wallet as WalletModel;
  10. use App\Models\Withdraw;
  11. use App\Models\User;
  12. use App\Models\Bank;
  13. use App\Models\Address;
  14. use App\Models\RechargeChannel;
  15. use App\Models\PaymentOrder;
  16. use App\Services\BalanceLogService;
  17. use App\Services\PaymentOrderService;
  18. use App\Services\Payment\JdPayService;
  19. use App\Services\Payment\NoPayService;
  20. use App\Services\Payment\ZimuPayService;
  21. use App\Services\QianBaoWithdrawService;
  22. use App\Services\WithdrawService;
  23. use Illuminate\Support\Facades\DB;
  24. use Illuminate\Validation\ValidationException;
  25. use Exception;
  26. /**
  27. * 充值提现接口
  28. */
  29. class Wallet extends BaseController
  30. {
  31. //获取三斤充值通道(微信、支付宝、扫码充值)
  32. public function getChannel()
  33. {
  34. try {
  35. $params = request()->validate(['currency_type' => ['nullable', 'string', 'in:crypto,fiat']],
  36. ['currency_type.in' => '充值渠道分类仅支持crypto或fiat']);
  37. } catch (ValidationException $e) {
  38. return $this->error($e->validator->errors()->first(), [], HttpStatus::CUSTOM_ERROR);
  39. }
  40. $member_id = request()->user->member_id;
  41. if (empty(request()->user->recharge_channel_group_id)) {
  42. $recharge_channel_group_id = User::where('member_id', $member_id)->value('recharge_channel_group_id');
  43. } else {
  44. $recharge_channel_group_id = request()->user->recharge_channel_group_id;
  45. }
  46. $list = RechargeChannel::getFormatChannel(1, $recharge_channel_group_id);
  47. if (!JdPayService::canUserRecharge(request()->user->id)) {
  48. $list = array_values(array_filter($list, function ($item) {
  49. return !JdPayService::isChannel($item['value'] ?? '');
  50. }));
  51. }
  52. if (!NoPayService::canUserRecharge(request()->user->id)) {
  53. $list = array_values(array_filter($list, function ($item) {
  54. return !NoPayService::isRechargeChannel($item['value'] ?? '');
  55. }));
  56. }
  57. if (!ZimuPayService::canUserRecharge(request()->user->id)) {
  58. $list = array_values(array_filter($list, function ($item) {
  59. return !ZimuPayService::isRechargeChannel($item['value'] ?? '');
  60. }));
  61. }
  62. $currencyType = $params['currency_type'] ?? null;
  63. if ($currencyType !== null && $currencyType !== '') {
  64. $list = array_values(array_filter($list, static function ($item) use ($currencyType) {
  65. return (($item['value'] ?? '') === 'usdt') === ($currencyType === 'crypto');
  66. }));
  67. }
  68. $list = \App\Services\RechargeBonusService::decorate($list, $this->lang, (int)request()->user->from === 1);
  69. return $this->success([
  70. 'list' => $list,
  71. ]);
  72. }
  73. /**
  74. * 创建代收订单(自动扫码充值)
  75. */
  76. public function createPay()
  77. {
  78. try {
  79. $params = request()->validate([
  80. 'amount' => ['required', 'numeric', 'min:0.01'],
  81. 'payment_type' => ['required', 'string'],
  82. ]);
  83. if ($params['payment_type'] == 'rgcz') {
  84. return $this->error('参数有误');
  85. }
  86. $member_id = request()->user->member_id;
  87. $user = User::where('member_id', $member_id)->first();
  88. if (JdPayService::isChannel($params['payment_type']) && !JdPayService::canUserRecharge($user->id)) {
  89. throw new Exception(lang("不支持此充值方式"));
  90. }
  91. if (NoPayService::isRechargeChannel($params['payment_type']) && !NoPayService::canUserRecharge($user->id)) {
  92. throw new Exception(lang("不支持此充值方式"));
  93. }
  94. if (ZimuPayService::isRechargeChannel($params['payment_type']) && !ZimuPayService::canUserRecharge($user->id)) {
  95. throw new Exception(lang("不支持此充值方式"));
  96. }
  97. //校验是否支持此充值方式
  98. $check = RechargeChannel::checkRechargeChannel($params['payment_type'], $user->recharge_channel_group_id);
  99. if ($check === false) {
  100. throw new Exception(lang("不支持此充值方式").$user->recharge_channel_group_id);
  101. }
  102. $res = PaymentOrderService::createPay($member_id, $params['amount'], $params['payment_type']);
  103. if ($res['code'] == 0) {
  104. return $this->success($res);
  105. }
  106. return $this->error($res['text']);
  107. } catch (ValidationException $e) {
  108. return $this->error($e->validator->errors()->first());
  109. } catch (\Exception $e) {
  110. return $this->error($e->getMessage());
  111. }
  112. }
  113. /**
  114. * 获取充值二维码(USDT充值)
  115. */
  116. public function scan()
  117. {
  118. try {
  119. $member_id = request()->user->member_id;
  120. $params = request()->validate([
  121. 'type' => ['required', 'string'],
  122. ]);
  123. if (strtolower($params['type']) === "trc20") {
  124. $address = Config::where('field', 'receiving_address')->first()->val;
  125. } elseif (strtolower($params['type']) === "erc20") {
  126. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  127. } else {
  128. return $this->error(lang('充值类型错误'));
  129. }
  130. $receivingType = ConfigService::getVal("receiving_type");
  131. //自动充值
  132. if ($receivingType == 1) {
  133. $res = WalletService::getRechargeImageAddress($member_id);
  134. $address = $res['address'];
  135. $qrCode = $res['full_path'];
  136. } else {
  137. //手动充值
  138. $res = WalletService::getPlatformImageAddress($address);
  139. $res['net'] = $params['type'];
  140. $qrCode = $res['full_path'];
  141. }
  142. return $this->success([
  143. 'qrcode' => $qrCode,
  144. 'address' => $address,
  145. 'public_chain' => (string)(Config::where('field', 'public_chain')->value('val') ?? ''),
  146. ]);
  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. }