Wallet.php 27 KB

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