Modules_Api_Controllers_WithdrawController.proxy.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace Modules\Api\Controllers;
  3. use App\Constants\ChangeLogConstant;
  4. use App\Jobs\SendTgWithdrawMegJob;
  5. use App\Model\CoinsLogModel;
  6. use App\Model\WithdrawalLogModel;
  7. use App\Model\WithdrawalMethodModel;
  8. use App\Repositories\ConfigRepoistory;
  9. use App\Repositories\Service\QueueService;
  10. use App\Repositories\UserRepository;
  11. use Carbon\Carbon;
  12. use Hyperf\DbConnection\Db;
  13. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  14. use App\Constants\CommonConstant;
  15. /**
  16. * 提现相关
  17. */
  18. class WithdrawController extends BaseController
  19. {
  20. use \Hyperf\Di\Aop\ProxyTrait;
  21. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  22. function __construct()
  23. {
  24. if (method_exists(parent::class, '__construct')) {
  25. parent::__construct(...func_get_args());
  26. }
  27. $this->__handlePropertyHandler(__CLASS__);
  28. }
  29. /**
  30. * 提现方式列表
  31. */
  32. public function userWithdrawalMethodList()
  33. {
  34. return json_success(WithdrawalMethodModel::query()->where('user_uid', $this->getUserId())->get(['id', 'type', 'username', 'account', 'bank']));
  35. }
  36. /**
  37. * 提币网络
  38. * @return mixed
  39. */
  40. public function usdtNetwork()
  41. {
  42. $configRepo = make(ConfigRepoistory::class);
  43. $usdtNet = $configRepo->get('usdt_net', 'sys', '');
  44. $arrUsdtNet = !empty($usdtNet) ? explode(',', $usdtNet) : [];
  45. return json_success($arrUsdtNet);
  46. }
  47. /**
  48. * 添加提现方式
  49. * @param ValidatorFactoryInterface $validatorFactory
  50. * @return mixed
  51. */
  52. public function createWithdrawalMethod(ValidatorFactoryInterface $validatorFactory)
  53. {
  54. $inputData = $this->getJsonData();
  55. $rules = [
  56. 'type' => 'required|numeric|between:1,3',
  57. // 收款类型 (1USDT,2银行卡,3支付宝)
  58. 'username' => 'nullable|min:1|max:10',
  59. // 姓名
  60. 'account' => 'required|min:4|max:40',
  61. // 账号,卡号,usdt地址
  62. 'bank' => 'nullable|min:4|max:20',
  63. ];
  64. $validator = $validatorFactory->make($inputData, $rules);
  65. if ($validator->fails()) {
  66. return json_fail($validator->errors()->first());
  67. }
  68. if ($inputData['type'] === 1) {
  69. // USDT
  70. if (!$inputData['bank']) {
  71. return json_fail('请检查usdt提币方式!');
  72. }
  73. } elseif ($inputData['type'] === 2) {
  74. // 银行卡
  75. if (!$inputData['username'] || !$inputData['bank']) {
  76. return json_fail('请检查姓名或所属银行!');
  77. }
  78. } elseif ($inputData['type'] === 3) {
  79. // 支付宝
  80. if (!$inputData['username']) {
  81. return json_fail('请检查姓名!');
  82. }
  83. }
  84. $userId = $this->getUserId();
  85. $withdrawalMethod = WithdrawalMethodModel::query();
  86. $withdrawalMethodClone = clone $withdrawalMethod;
  87. if ($withdrawalMethod->where(['type' => $inputData['type'], 'user_uid' => $userId])->count()) {
  88. return json_fail('该提现方式已存在!');
  89. }
  90. $array = ['user_uid' => $userId, 'type' => $inputData['type'], 'username' => $inputData['username'], 'account' => $inputData['account'], 'bank' => $inputData['bank']];
  91. if (!$withdrawalMethodClone->create($array)) {
  92. return json_fail('添加失败请联系客服!');
  93. }
  94. return json_success('添加成功!');
  95. }
  96. /**
  97. * 删除提现方式
  98. */
  99. public function deleteWithdrawalMethod(ValidatorFactoryInterface $validatorFactory)
  100. {
  101. $inputData = $this->getJsonData();
  102. $rules = ['type' => 'required|numeric|between:1,3'];
  103. $validator = $validatorFactory->make($inputData, $rules);
  104. if ($validator->fails()) {
  105. return json_fail($validator->errors()->first());
  106. }
  107. // 查询是否有这个方法
  108. $userInfo = make(UserRepository::class)->findWhere(['code' => $this->getUserId()])->first();
  109. if (!$userInfo) {
  110. return json_fail('操作有误!');
  111. }
  112. $resData = WithdrawalMethodModel::query()->where(['user_uid' => $userInfo->code, 'type' => $inputData['type']])->first();
  113. if (!$resData) {
  114. return json_fail('提现方式不存在!');
  115. }
  116. if (!$resData->delete()) {
  117. return json_fail('删除失败!');
  118. }
  119. return json_success($inputData);
  120. }
  121. /**
  122. * 提现记录
  123. * @param ValidatorFactoryInterface $validatorFactory
  124. * @return mixed
  125. */
  126. public function withdrawalLogList(ValidatorFactoryInterface $validatorFactory)
  127. {
  128. $inputData = $this->getJsonData();
  129. $rules = ['page' => 'required|int', 'page_size' => 'required|int'];
  130. $validator = $validatorFactory->make($inputData, $rules);
  131. if ($validator->fails()) {
  132. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  133. return json_fail($validator->errors()->first());
  134. }
  135. $page = $inputData['page'] ?? 1;
  136. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  137. if ($pageSize > 100) {
  138. $pageSize = CommonConstant::PAGE_SIZE;
  139. }
  140. $page = format_page($page);
  141. $pageSize = intval($pageSize);
  142. if ($pageSize <= 0) {
  143. $pageSize = CommonConstant::PAGE_SIZE;
  144. }
  145. 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));
  146. }
  147. /**
  148. * 申请提现列表
  149. */
  150. public function applyDorWithdrawalList()
  151. {
  152. // 提现列表
  153. $dataList = WithdrawalMethodModel::query()->where('user_uid', $this->getUserId())->get(['id', 'type', 'username', 'account', 'bank']);
  154. // 查询用户
  155. $userInfo = make(UserRepository::class)->findWhere(['code' => $this->getUserId()])->first();
  156. if (!$userInfo) {
  157. return json_fail('操作有误!');
  158. }
  159. if ($dataList) {
  160. $configRepo = make(ConfigRepoistory::class);
  161. $usdtNet = $configRepo->gets(['withdraw_money', 'usdt_conf', 'withdraw_usdt'], 'sys', '');
  162. $withdrawMoney = $usdtNet['withdraw_money'];
  163. // 最低提现
  164. $usdt_conf = $usdtNet['usdt_conf'];
  165. // utdt汇率
  166. $withdraw_usdt = $usdtNet['withdraw_usdt'];
  167. // utdt最低提现
  168. foreach ($dataList as &$item) {
  169. if ($item->type === 1) {
  170. // usdt提现
  171. $item->low_withdraw = (int) $withdraw_usdt;
  172. //最低提现
  173. $item->withdraw = floor($userInfo->balance / $usdt_conf);
  174. //可提现
  175. $item->exchange = (float) $usdt_conf;
  176. //汇率
  177. } else {
  178. $item->low_withdraw = (int) $withdrawMoney;
  179. $item->withdraw = (float) $userInfo->balance;
  180. $item->exchange = 0;
  181. }
  182. }
  183. }
  184. $userWithdraw = ['周*雨提现9000元', '白*惠提现8000元', '李*提现10000元', '黄*提现50000元'];
  185. $resData = [
  186. 'user_withdraw_list' => $userWithdraw,
  187. 'dataList' => $dataList,
  188. 'balance' => $userInfo->balance,
  189. //我的金额
  190. 'coins' => $userInfo->balance * 10,
  191. // 可提现金币
  192. 'coins_proportion' => 10,
  193. // 金币比例
  194. 'usdt_proportion' => $usdt_conf,
  195. // usdt 比例
  196. 'coins_low_withdraw' => 20,
  197. // 最低提现金币
  198. 'usdt_low_withdraw' => $withdraw_usdt,
  199. // usdt 最低提现
  200. 'money_low_proportion' => $withdrawMoney,
  201. ];
  202. return json_success($resData);
  203. }
  204. /**
  205. * 申请提现
  206. * @param ValidatorFactoryInterface $validatorFactory
  207. * @return mixed
  208. */
  209. public function applyDorWithdrawal(ValidatorFactoryInterface $validatorFactory)
  210. {
  211. $inputData = $this->getJsonData();
  212. $rules = ['amount' => 'required|numeric', 'withdrawal_method_id' => 'required|numeric'];
  213. $validator = $validatorFactory->make($inputData, $rules);
  214. if ($validator->fails()) {
  215. return json_fail($validator->errors()->first());
  216. }
  217. $userId = $this->getUserId();
  218. // 查询提现方式
  219. $withdrawalMethodInfo = WithdrawalMethodModel::query()->where(['id' => $inputData['withdrawal_method_id'], 'user_uid' => $userId])->first(['id', 'type', 'username', 'account', 'bank']);
  220. if (!$withdrawalMethodInfo) {
  221. return json_fail('没有该提现方式!');
  222. }
  223. $amount = $inputData['amount'];
  224. // 查询用户
  225. $userInfo = make(UserRepository::class)->findWhere(['code' => $userId])->first();
  226. $configRepo = make(ConfigRepoistory::class);
  227. $usdtNet = $configRepo->gets(['usdt_conf', 'withdraw_usdt', 'withdraw_money'], 'sys', '');
  228. $usdt_conf = floatval($usdtNet['usdt_conf']);
  229. //utdt汇率
  230. $withdrawUsdtConf = intval($usdtNet['withdraw_usdt']);
  231. // ustd最低提现
  232. $withdrawConf = intval($usdtNet['withdraw_money']);
  233. // ustd最低提现
  234. //判断提现的类型
  235. if ($withdrawalMethodInfo->type === 1) {
  236. // utdt最低提现
  237. if ($amount < $withdrawUsdtConf) {
  238. return json_fail('最低提现要求' . $withdrawUsdtConf . 'U');
  239. }
  240. // 可提现U
  241. $withdrawalUsdt = floor($userInfo->balance / $usdt_conf);
  242. if ($amount > $withdrawalUsdt) {
  243. return json_fail('余额不足!');
  244. }
  245. } else {
  246. if ($amount < $withdrawConf) {
  247. return json_fail('最低提现要求' . $withdrawConf . '元');
  248. }
  249. if ($amount > $userInfo->balance) {
  250. return json_fail('余额不足!');
  251. }
  252. }
  253. // 金额变动
  254. $dataArray = [
  255. 'order_number' => date('YmdHis') . random_int(10000, 99999),
  256. 'amount' => $amount,
  257. //提现金额
  258. 'withdrawal_method_id' => $inputData['withdrawal_method_id'],
  259. //提现方式id
  260. 'balance_forward' => $userInfo->balance,
  261. //提现前金额
  262. 'freeze_forward' => $userInfo->freeze,
  263. //提现前冻结金额
  264. 'user_uid' => $userInfo->code,
  265. 'type' => $withdrawalMethodInfo->type,
  266. 'username' => $withdrawalMethodInfo->username,
  267. 'account' => $withdrawalMethodInfo->account,
  268. 'bank' => $withdrawalMethodInfo->bank,
  269. 'status' => 1,
  270. //1打款中,2已到账,3失败,4冻结
  271. 'created_at' => Carbon::now()->toDateTimeString(),
  272. // 申请时间
  273. 'updated_at' => Carbon::now()->toDateTimeString(),
  274. ];
  275. if ($withdrawalMethodInfo->type === 1) {
  276. // usdt计算
  277. $amount = $amount * $usdt_conf;
  278. // usdt转现金
  279. $dataArray['usdt_to_amount'] = $amount;
  280. //usdt转现金
  281. $dataArray['usdt_proportion'] = $usdt_conf;
  282. }
  283. $dataArray['balance_rear'] = $userInfo->balance - $amount;
  284. //提现后金额
  285. $dataArray['freeze_rear'] = $userInfo->freeze + $amount;
  286. //提现后冻结金额
  287. $withdrawalLog = WithdrawalLogModel::query();
  288. // 事物执行
  289. Db::beginTransaction();
  290. try {
  291. $userInfo->balance = $userInfo->balance - $amount;
  292. // 余额
  293. $userInfo->freeze = $userInfo->freeze + $amount;
  294. // 冻结余额
  295. if (!$userInfo->save()) {
  296. Db::rollback();
  297. return json_fail('提现失败!请重试 -001');
  298. }
  299. $res = $withdrawalLog->create($dataArray);
  300. if (!$res) {
  301. Db::rollback();
  302. return json_fail('提现失败!请重试 -002');
  303. }
  304. Db::commit();
  305. } catch (\Exception $exception) {
  306. Db::rollback();
  307. return json_fail('提现失败!请重试 -003');
  308. }
  309. dispatch_job(new SendTgWithdrawMegJob('雏姬', $userInfo->code, $userInfo->nick, $withdrawalMethodInfo->type, $inputData['amount'], Carbon::now()->toDateTimeString()));
  310. // 帐变记录
  311. $changeData = ['user_uid' => $userId, 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT, 'amount' => $amount, 'way' => ChangeLogConstant::WAY_2, 'type' => ChangeLogConstant::TYPE_20];
  312. make(QueueService::class)->saveChangeLogJob($changeData);
  313. return json_success('申请提现成功!');
  314. }
  315. /**
  316. * 余额转金币列表
  317. * @param ValidatorFactoryInterface $validatorFactory
  318. */
  319. public function BalanceChangeCoinList(ValidatorFactoryInterface $validatorFactory)
  320. {
  321. $inputData = $this->getJsonData();
  322. $rules = ['page' => 'required|int', 'page_size' => 'required|int'];
  323. $validator = $validatorFactory->make($inputData, $rules);
  324. if ($validator->fails()) {
  325. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  326. return json_fail($validator->errors()->first());
  327. }
  328. $page = $inputData['page'] ?? 1;
  329. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  330. if ($pageSize > 100) {
  331. $pageSize = CommonConstant::PAGE_SIZE;
  332. }
  333. $page = format_page($page);
  334. $pageSize = intval($pageSize);
  335. if ($pageSize <= 0) {
  336. $pageSize = CommonConstant::PAGE_SIZE;
  337. }
  338. return json_success(WithdrawalLogModel::query()->where([['user_uid', '=', $this->getUserId()], ['type', '=', 4]])->select('amount', 'coins', 'updated_at')->paginate($pageSize, ['*'], 'page', $page));
  339. }
  340. /**
  341. * 余额转金币
  342. * @param ValidatorFactoryInterface $validatorFactory
  343. * @return mixed
  344. * @throws \Exception
  345. */
  346. public function BalanceChangeCoin(ValidatorFactoryInterface $validatorFactory)
  347. {
  348. $inputData = $this->getJsonData();
  349. $rules = ['amount' => 'required|numeric|min:10'];
  350. $validator = $validatorFactory->make($inputData, $rules);
  351. if ($validator->fails()) {
  352. return json_fail($validator->errors()->first());
  353. }
  354. $amount = $inputData['amount'];
  355. $userId = $this->getUserId();
  356. // 查询用户
  357. $userInfo = make(UserRepository::class)->findWhere(['code' => $userId])->first();
  358. if ($userInfo->balance < $amount) {
  359. return json_fail('余额不足!');
  360. }
  361. $coinConf = 10;
  362. $coin = $amount * $coinConf;
  363. $dataArray = [
  364. 'order_number' => date('YmdHis') . random_int(10000, 99999),
  365. 'amount' => $amount,
  366. // 兑换的金额
  367. 'balance_forward' => $userInfo->balance,
  368. //提现前金额
  369. 'balance_rear' => $userInfo->balance - $amount,
  370. //提现后金额
  371. 'withdrawal_method_id' => 0,
  372. 'coins' => $coin,
  373. // 兑换的金币
  374. 'forward_coins' => $userInfo->coins,
  375. // 兑换前
  376. 'rear_coins' => $userInfo->coins + $coin,
  377. // 兑换后
  378. 'user_uid' => $userInfo->code,
  379. 'type' => 4,
  380. // 4兑换金币
  381. 'status' => 2,
  382. //1打款中,2已到账,3失败,4冻结
  383. 'coin_proportion' => $coinConf,
  384. 'created_at' => Carbon::now()->toDateTimeString(),
  385. // 申请时间
  386. 'updated_at' => Carbon::now()->toDateTimeString(),
  387. ];
  388. $withdrawalLog = WithdrawalLogModel::query();
  389. //--记录帐变使用日志
  390. $changeData = ['user_uid' => $userInfo->code, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'amount' => $coin, 'way' => ChangeLogConstant::WAY_1, 'type' => ChangeLogConstant::TYPE_5];
  391. $changeBalanceData = ['user_uid' => $userInfo->code, 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT, 'amount' => $amount, 'way' => ChangeLogConstant::WAY_2, 'type' => ChangeLogConstant::TYPE_5];
  392. // 事物执行
  393. Db::beginTransaction();
  394. try {
  395. $userInfo->balance = $userInfo->balance - $amount;
  396. // 余额
  397. $userInfo->coins = $userInfo->coins + $coin;
  398. make(UserRepository::class)->delUserCache($userInfo->code);
  399. if (!$userInfo->save()) {
  400. Db::rollback();
  401. return json_fail('兑换失败!请重试 -001');
  402. }
  403. $res = $withdrawalLog->create($dataArray);
  404. if (!$res) {
  405. Db::rollback();
  406. return json_fail('兑换失败!请重试 -002');
  407. }
  408. Db::commit();
  409. // 帐变记录
  410. $queue = make(QueueService::class);
  411. $queue->saveChangeLogJob($changeData);
  412. // 金币
  413. $queue->saveChangeLogJob($changeBalanceData);
  414. // 余额
  415. } catch (\Exception $exception) {
  416. Db::rollback();
  417. return json_fail('兑换失败!请重试 -003');
  418. }
  419. return json_success('兑换成功!');
  420. }
  421. }