Wallet.php 28 KB

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