| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <?php
- namespace Modules\Api\Controllers;
- use App\Constants\ChangeLogConstant;
- use App\Jobs\SendTgWithdrawMegJob;
- use App\Model\CoinsLogModel;
- use App\Model\WithdrawalLogModel;
- use App\Model\WithdrawalMethodModel;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\Service\QueueService;
- use App\Repositories\UserRepository;
- use Carbon\Carbon;
- use Hyperf\DbConnection\Db;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use App\Constants\CommonConstant;
- /**
- * 提现相关
- */
- class WithdrawController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 提现方式列表
- */
- public function userWithdrawalMethodList()
- {
- return json_success(WithdrawalMethodModel::query()->where('user_uid', $this->getUserId())->get(['id', 'type', 'username', 'account', 'bank']));
- }
- /**
- * 提币网络
- * @return mixed
- */
- public function usdtNetwork()
- {
- $configRepo = make(ConfigRepoistory::class);
- $usdtNet = $configRepo->get('usdt_net', 'sys', '');
- $arrUsdtNet = !empty($usdtNet) ? explode(',', $usdtNet) : [];
- return json_success($arrUsdtNet);
- }
- /**
- * 添加提现方式
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function createWithdrawalMethod(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'type' => 'required|numeric|between:1,3',
- // 收款类型 (1USDT,2银行卡,3支付宝)
- 'username' => 'nullable|min:1|max:10',
- // 姓名
- 'account' => 'required|min:4|max:40',
- // 账号,卡号,usdt地址
- 'bank' => 'nullable|min:4|max:20',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- if ($inputData['type'] === 1) {
- // USDT
- if (!$inputData['bank']) {
- return json_fail('请检查usdt提币方式!');
- }
- } elseif ($inputData['type'] === 2) {
- // 银行卡
- if (!$inputData['username'] || !$inputData['bank']) {
- return json_fail('请检查姓名或所属银行!');
- }
- } elseif ($inputData['type'] === 3) {
- // 支付宝
- if (!$inputData['username']) {
- return json_fail('请检查姓名!');
- }
- }
- $userId = $this->getUserId();
- $withdrawalMethod = WithdrawalMethodModel::query();
- $withdrawalMethodClone = clone $withdrawalMethod;
- if ($withdrawalMethod->where(['type' => $inputData['type'], 'user_uid' => $userId])->count()) {
- return json_fail('该提现方式已存在!');
- }
- $array = ['user_uid' => $userId, 'type' => $inputData['type'], 'username' => $inputData['username'], 'account' => $inputData['account'], 'bank' => $inputData['bank']];
- if (!$withdrawalMethodClone->create($array)) {
- return json_fail('添加失败请联系客服!');
- }
- return json_success('添加成功!');
- }
- /**
- * 删除提现方式
- */
- public function deleteWithdrawalMethod(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['type' => 'required|numeric|between:1,3'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- // 查询是否有这个方法
- $userInfo = make(UserRepository::class)->findWhere(['code' => $this->getUserId()])->first();
- if (!$userInfo) {
- return json_fail('操作有误!');
- }
- $resData = WithdrawalMethodModel::query()->where(['user_uid' => $userInfo->code, 'type' => $inputData['type']])->first();
- if (!$resData) {
- return json_fail('提现方式不存在!');
- }
- if (!$resData->delete()) {
- return json_fail('删除失败!');
- }
- return json_success($inputData);
- }
- /**
- * 提现记录
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function withdrawalLogList(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|int', 'page_size' => 'required|int'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $page = $inputData['page'] ?? 1;
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 100) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($page);
- $pageSize = intval($pageSize);
- if ($pageSize <= 0) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- return json_success(WithdrawalLogModel::query()->where([['user_uid', '=', $this->getUserId()], ['type', '!=', 4]])->select('order_number', 'amount', 'type', 'username', 'account', 'bank', 'status', 'remark', 'created_at', 'updated_at')->orderBy('created_at', 'desc')->paginate($pageSize, ['*'], 'page', $page));
- }
- /**
- * 申请提现列表
- */
- public function applyDorWithdrawalList()
- {
- // 提现列表
- $dataList = WithdrawalMethodModel::query()->where('user_uid', $this->getUserId())->get(['id', 'type', 'username', 'account', 'bank']);
- // 查询用户
- $userInfo = make(UserRepository::class)->findWhere(['code' => $this->getUserId()])->first();
- if (!$userInfo) {
- return json_fail('操作有误!');
- }
- if ($dataList) {
- $configRepo = make(ConfigRepoistory::class);
- $usdtNet = $configRepo->gets(['withdraw_money', 'usdt_conf', 'withdraw_usdt'], 'sys', '');
- $withdrawMoney = $usdtNet['withdraw_money'];
- // 最低提现
- $usdt_conf = $usdtNet['usdt_conf'];
- // utdt汇率
- $withdraw_usdt = $usdtNet['withdraw_usdt'];
- // utdt最低提现
- foreach ($dataList as &$item) {
- if ($item->type === 1) {
- // usdt提现
- $item->low_withdraw = (int) $withdraw_usdt;
- //最低提现
- $item->withdraw = floor($userInfo->balance / $usdt_conf);
- //可提现
- $item->exchange = (float) $usdt_conf;
- //汇率
- } else {
- $item->low_withdraw = (int) $withdrawMoney;
- $item->withdraw = (float) $userInfo->balance;
- $item->exchange = 0;
- }
- }
- }
- $userWithdraw = ['周*雨提现9000元', '白*惠提现8000元', '李*提现10000元', '黄*提现50000元'];
- $resData = [
- 'user_withdraw_list' => $userWithdraw,
- 'dataList' => $dataList,
- 'balance' => $userInfo->balance,
- //我的金额
- 'coins' => $userInfo->balance * 10,
- // 可提现金币
- 'coins_proportion' => 10,
- // 金币比例
- 'usdt_proportion' => $usdt_conf,
- // usdt 比例
- 'coins_low_withdraw' => 20,
- // 最低提现金币
- 'usdt_low_withdraw' => $withdraw_usdt,
- // usdt 最低提现
- 'money_low_proportion' => $withdrawMoney,
- ];
- return json_success($resData);
- }
- /**
- * 申请提现
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function applyDorWithdrawal(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['amount' => 'required|numeric', 'withdrawal_method_id' => 'required|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $userId = $this->getUserId();
- // 查询提现方式
- $withdrawalMethodInfo = WithdrawalMethodModel::query()->where(['id' => $inputData['withdrawal_method_id'], 'user_uid' => $userId])->first(['id', 'type', 'username', 'account', 'bank']);
- if (!$withdrawalMethodInfo) {
- return json_fail('没有该提现方式!');
- }
- $amount = $inputData['amount'];
- // 查询用户
- $userInfo = make(UserRepository::class)->findWhere(['code' => $userId])->first();
- $configRepo = make(ConfigRepoistory::class);
- $usdtNet = $configRepo->gets(['usdt_conf', 'withdraw_usdt', 'withdraw_money'], 'sys', '');
- $usdt_conf = floatval($usdtNet['usdt_conf']);
- //utdt汇率
- $withdrawUsdtConf = intval($usdtNet['withdraw_usdt']);
- // ustd最低提现
- $withdrawConf = intval($usdtNet['withdraw_money']);
- // ustd最低提现
- //判断提现的类型
- if ($withdrawalMethodInfo->type === 1) {
- // utdt最低提现
- if ($amount < $withdrawUsdtConf) {
- return json_fail('最低提现要求' . $withdrawUsdtConf . 'U');
- }
- // 可提现U
- $withdrawalUsdt = floor($userInfo->balance / $usdt_conf);
- if ($amount > $withdrawalUsdt) {
- return json_fail('余额不足!');
- }
- } else {
- if ($amount < $withdrawConf) {
- return json_fail('最低提现要求' . $withdrawConf . '元');
- }
- if ($amount > $userInfo->balance) {
- return json_fail('余额不足!');
- }
- }
- // 金额变动
- $dataArray = [
- 'order_number' => date('YmdHis') . random_int(10000, 99999),
- 'amount' => $amount,
- //提现金额
- 'withdrawal_method_id' => $inputData['withdrawal_method_id'],
- //提现方式id
- 'balance_forward' => $userInfo->balance,
- //提现前金额
- 'freeze_forward' => $userInfo->freeze,
- //提现前冻结金额
- 'user_uid' => $userInfo->code,
- 'type' => $withdrawalMethodInfo->type,
- 'username' => $withdrawalMethodInfo->username,
- 'account' => $withdrawalMethodInfo->account,
- 'bank' => $withdrawalMethodInfo->bank,
- 'status' => 1,
- //1打款中,2已到账,3失败,4冻结
- 'created_at' => Carbon::now()->toDateTimeString(),
- // 申请时间
- 'updated_at' => Carbon::now()->toDateTimeString(),
- ];
- if ($withdrawalMethodInfo->type === 1) {
- // usdt计算
- $amount = $amount * $usdt_conf;
- // usdt转现金
- $dataArray['usdt_to_amount'] = $amount;
- //usdt转现金
- $dataArray['usdt_proportion'] = $usdt_conf;
- }
- $dataArray['balance_rear'] = $userInfo->balance - $amount;
- //提现后金额
- $dataArray['freeze_rear'] = $userInfo->freeze + $amount;
- //提现后冻结金额
- $withdrawalLog = WithdrawalLogModel::query();
- // 事物执行
- Db::beginTransaction();
- try {
- $userInfo->balance = $userInfo->balance - $amount;
- // 余额
- $userInfo->freeze = $userInfo->freeze + $amount;
- // 冻结余额
- if (!$userInfo->save()) {
- Db::rollback();
- return json_fail('提现失败!请重试 -001');
- }
- $res = $withdrawalLog->create($dataArray);
- if (!$res) {
- Db::rollback();
- return json_fail('提现失败!请重试 -002');
- }
- Db::commit();
- } catch (\Exception $exception) {
- Db::rollback();
- return json_fail('提现失败!请重试 -003');
- }
- dispatch_job(new SendTgWithdrawMegJob('雏姬', $userInfo->code, $userInfo->nick, $withdrawalMethodInfo->type, $inputData['amount'], Carbon::now()->toDateTimeString()));
- // 帐变记录
- $changeData = ['user_uid' => $userId, 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT, 'amount' => $amount, 'way' => ChangeLogConstant::WAY_2, 'type' => ChangeLogConstant::TYPE_20];
- make(QueueService::class)->saveChangeLogJob($changeData);
- return json_success('申请提现成功!');
- }
- /**
- * 余额转金币列表
- * @param ValidatorFactoryInterface $validatorFactory
- */
- public function BalanceChangeCoinList(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|int', 'page_size' => 'required|int'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $page = $inputData['page'] ?? 1;
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 100) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($page);
- $pageSize = intval($pageSize);
- if ($pageSize <= 0) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- return json_success(WithdrawalLogModel::query()->where([['user_uid', '=', $this->getUserId()], ['type', '=', 4]])->select('amount', 'coins', 'updated_at')->paginate($pageSize, ['*'], 'page', $page));
- }
- /**
- * 余额转金币
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @throws \Exception
- */
- public function BalanceChangeCoin(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['amount' => 'required|numeric|min:10'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $amount = $inputData['amount'];
- $userId = $this->getUserId();
- // 查询用户
- $userInfo = make(UserRepository::class)->findWhere(['code' => $userId])->first();
- if ($userInfo->balance < $amount) {
- return json_fail('余额不足!');
- }
- $coinConf = 10;
- $coin = $amount * $coinConf;
- $dataArray = [
- 'order_number' => date('YmdHis') . random_int(10000, 99999),
- 'amount' => $amount,
- // 兑换的金额
- 'balance_forward' => $userInfo->balance,
- //提现前金额
- 'balance_rear' => $userInfo->balance - $amount,
- //提现后金额
- 'withdrawal_method_id' => 0,
- 'coins' => $coin,
- // 兑换的金币
- 'forward_coins' => $userInfo->coins,
- // 兑换前
- 'rear_coins' => $userInfo->coins + $coin,
- // 兑换后
- 'user_uid' => $userInfo->code,
- 'type' => 4,
- // 4兑换金币
- 'status' => 2,
- //1打款中,2已到账,3失败,4冻结
- 'coin_proportion' => $coinConf,
- 'created_at' => Carbon::now()->toDateTimeString(),
- // 申请时间
- 'updated_at' => Carbon::now()->toDateTimeString(),
- ];
- $withdrawalLog = WithdrawalLogModel::query();
- //--记录帐变使用日志
- $changeData = ['user_uid' => $userInfo->code, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'amount' => $coin, 'way' => ChangeLogConstant::WAY_1, 'type' => ChangeLogConstant::TYPE_5];
- $changeBalanceData = ['user_uid' => $userInfo->code, 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT, 'amount' => $amount, 'way' => ChangeLogConstant::WAY_2, 'type' => ChangeLogConstant::TYPE_5];
- // 事物执行
- Db::beginTransaction();
- try {
- $userInfo->balance = $userInfo->balance - $amount;
- // 余额
- $userInfo->coins = $userInfo->coins + $coin;
- make(UserRepository::class)->delUserCache($userInfo->code);
- if (!$userInfo->save()) {
- Db::rollback();
- return json_fail('兑换失败!请重试 -001');
- }
- $res = $withdrawalLog->create($dataArray);
- if (!$res) {
- Db::rollback();
- return json_fail('兑换失败!请重试 -002');
- }
- Db::commit();
- // 帐变记录
- $queue = make(QueueService::class);
- $queue->saveChangeLogJob($changeData);
- // 金币
- $queue->saveChangeLogJob($changeBalanceData);
- // 余额
- } catch (\Exception $exception) {
- Db::rollback();
- return json_fail('兑换失败!请重试 -003');
- }
- return json_success('兑换成功!');
- }
- }
|