Wallet.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\Recharge;
  5. use app\admin\model\Withdraw;
  6. use app\admin\model\Admin;
  7. use app\admin\model\User;
  8. use app\admin\model\FundsRecord;
  9. use app\admin\model\DepositAddress;
  10. use app\admin\validate\WalletValidate;
  11. use Exception;
  12. use think\facade\Db;
  13. /**
  14. * 用户资金
  15. */
  16. class Wallet extends BaseController
  17. {
  18. /**
  19. * 充值通过
  20. * @apiParam {int} id 充值订单ID
  21. * @apiParam {String} safe_word 资金密码
  22. */
  23. function rechargePass()
  24. {
  25. $errors = [];
  26. Db::startTrans();
  27. try {
  28. $params = (new WalletValidate())->post()->goCheck('rechargePass');
  29. $admin = Admin::where('id', $this->admin_id)->find();
  30. if (!password_verify($params['safe_word'], $admin->payment_password)) throw new Exception('资金密码错误');
  31. $ro = Recharge::find($params['id']);
  32. if ($ro->status != 0) {
  33. $errors = ['id' => $ro->order_no];
  34. throw new Exception("该订单状态无法操作");
  35. }
  36. //获取汇率
  37. $currency_rate = getBlockChainFee($ro->currency);
  38. $ro->currency_rate = $currency_rate;//审核通过时候的汇率
  39. $ro->actual_received = bcmul($ro->recharge_amount, $currency_rate, 2);//实际到账的usdt
  40. $ro->status = 1;
  41. $ro->operation_time = time();
  42. $ro->save();
  43. $user = User::where('user_id', $ro->user_id)->find();
  44. $userMoney = bcadd($user->money, $ro->actual_received, 2);
  45. FundsRecord::addData([
  46. 'transaction_type' => 'recharge',
  47. 'amount_change' => $ro->actual_received,
  48. 'balance_before' => $user->money,
  49. 'balance_after' => $userMoney,
  50. 'user_id' => $user->user_id
  51. ]);
  52. $user->money = $userMoney;
  53. $user->save();
  54. DB::commit();
  55. } catch (Exception $e) {
  56. DB::rollBack();
  57. return $this->error($e->getMessage(), $errors);
  58. }
  59. return $this->success();
  60. }
  61. /**
  62. * 充值驳回
  63. * @apiParam {int} id 充值订单ID
  64. * @apiParam {String{0..200}} operation_remark 驳回原因
  65. *
  66. */
  67. function rechargeRefuse()
  68. {
  69. $errors = [];
  70. Db::startTrans();
  71. try {
  72. $params = (new WalletValidate())->post()->goCheck('rechargeRefuse');
  73. $operationRemark = $params['operation_remark'] ?? '';
  74. $ro = Recharge::findOrFail($params['id']);
  75. if ($ro->status != 0) {
  76. $errors = ['id' => $ro->order_no];
  77. throw new Exception("该订单状态无法操作");
  78. }
  79. $ro->status = 2;
  80. $ro->operation_remark = $operationRemark;
  81. $ro->operation_time = time();
  82. $ro->save();
  83. Db::commit();
  84. } catch (Exception $e) {
  85. Db::rollBack();
  86. return $this->error($e->getMessage(), $errors);
  87. }
  88. return $this->success();
  89. }
  90. /**
  91. * 充值订单
  92. */
  93. function recharges()
  94. {
  95. try {
  96. $params = $this->request->param();
  97. $page = isset($params['page']) ? intval($params['page']) : 1;
  98. $limit = isset($params['limit']) ? intval($params['limit']) : 15;
  99. $query = Recharge::alias('recharge')->join('user', 'recharge.user_id=user.user_id','left');
  100. if (isset($params['start_time'])) {
  101. $start_time = strtotime($params['start_time'].' 00:00:00');
  102. $query = $query->where('recharge.create_time', '>=', $start_time);
  103. }
  104. if (isset($params['end_time'])) {
  105. $end_time = strtotime($params['end_time'].' 23:59:59');
  106. $query = $query->where('recharge.create_time', '<=', $end_time);
  107. }
  108. if (isset($params['operation_start'])) {
  109. $operation_start = strtotime($params['operation_start'].' 00:00:00');
  110. $query->where('recharge.operation_time', '>=', $operation_start);
  111. }
  112. if (isset($params['operation_end'])) {
  113. $operation_end = strtotime($params['operation_end'].' 23:59:59');
  114. $query->where('recharge.operation_time', '<=', $operation_end);
  115. }
  116. if (isset($params['status']) && $params['status'] !== null) {
  117. $query->where('recharge.status', $params['status']);
  118. }
  119. if (!empty($params['order_no'])) {
  120. $query->where('recharge.order_no', $params['order_no']);
  121. }
  122. if (!empty($params['currency'])) {
  123. $query->where('recharge.currency', $params['currency']);
  124. }
  125. if (!empty($params['realname'])) {
  126. $query = $query->where(function ($query) use ($params) {
  127. $query->where('user.realname', 'like', "%{$params['realname']}%")
  128. ->orWhere('user.user_id', 'like', "%{$params['realname']}%");
  129. });
  130. }
  131. $count = $query->count();
  132. $list = $query->field(['recharge.*','user.realname', 'user.user_id', 'user.remark','user.phone','user.email'])
  133. ->limit($limit)
  134. ->page($page)
  135. ->order('recharge.create_time','desc')
  136. ->select()->toArray();
  137. } catch (Exception $e) {
  138. return $this->error($e->getMessage());
  139. }
  140. return $this->success(['count' => $count, 'list' => $list]);
  141. }
  142. /**
  143. * 提现驳回
  144. */
  145. function withdrawRefuse()
  146. {
  147. $errors = [];
  148. Db::startTrans();
  149. try {
  150. $params = (new WalletValidate())->post()->goCheck('withdrawRefuse');
  151. $operationRemark = $params['operation_remark'] ?? '';
  152. $wo = Withdraw::findOrFail($params['id']);
  153. if ($wo->status != 0) {
  154. $errors = ['id' => $wo->order_no];
  155. throw new Exception("该订单状态无法操作");
  156. }
  157. $user = User::where('user_id', $wo->user_id)->find();
  158. $userMoney = bcadd($user->money, $wo->deduction_usd, 2);
  159. FundsRecord::addData([
  160. 'transaction_type' => 'withdraw',
  161. 'amount_change' => $wo->deduction_usd,
  162. 'balance_before' => $user->money,
  163. 'balance_after' => $userMoney,
  164. 'user_id' => $user->user_id
  165. ]);
  166. $wo->status = 2;
  167. $wo->operation_remark = $operationRemark;
  168. $wo->operator_id = $this->admin_id;
  169. $wo->operation_time = time();
  170. $wo->save();
  171. $user->money = $userMoney;
  172. $user->save();
  173. Db::commit();
  174. } catch (Exception $e) {
  175. Db::rollBack();
  176. return $this->error($e->getMessage(), $errors);
  177. }
  178. return $this->success();
  179. }
  180. /**
  181. * @api {post} /wallet/withdrawPass 提现通过
  182. * @apiDescription 需要手动打款后来此操作
  183. * @apigroup 财务
  184. * @apiVersion 1.0.0
  185. * @apiUse header
  186. * @apiUse lang
  187. *
  188. * @apiParam {int} id 提现订单的ID
  189. * @apiParam {String} safe_word 资金密码
  190. */
  191. function withdrawPass()
  192. {
  193. $errors = [];
  194. Db::startTrans();
  195. try {
  196. $params = (new WalletValidate())->post()->goCheck('withdrawPass');
  197. $safeWord = $params['safe_word'] ?? '';
  198. $admin = Admin::where('id', $this->admin_id)->find();
  199. if (!password_verify($safeWord, $admin->payment_password)) throw new Exception('资金密码错误');
  200. $wo = Withdraw::findOrFail($params['id']);
  201. if ($wo->status != 0) {
  202. $errors = ['id' => $wo->order_no];
  203. throw new Exception("该订单状态无法操作");
  204. }
  205. $wo->operation_time = date('Y-m-d H:i:s');
  206. $wo->operator_id = $this->admin_id;
  207. $wo->status = 1;
  208. $wo->save();
  209. Db::commit();
  210. } catch (Exception $e) {
  211. Db::rollBack();
  212. return $this->error($e->getMessage(), $errors);
  213. }
  214. return $this->success();
  215. }
  216. /**
  217. * 提现收款地址修改
  218. */
  219. function updateAddress()
  220. {
  221. $errors = [];
  222. Db::startTrans();
  223. try {
  224. $params = (new WalletValidate())->post()->goCheck('updateAddress');
  225. $admin = Admin::where('id', $this->admin_id)->find();
  226. if (!password_verify($params['safe_word'], $admin->payment_password)) throw new Exception('资金密码错误');
  227. $wo = Withdraw::find($params['id']);
  228. if ($wo->status != 0) {
  229. $errors = ['id' => $wo->order_no];
  230. throw new Exception("该订单状态无法操作");
  231. }
  232. if ($wo->currency == 'bank') {
  233. $wo->bank_card_no = $params['address'];
  234. } else {
  235. $wo->channel_address = $params['address'];
  236. }
  237. $wo->save();
  238. Db::commit();
  239. } catch (Exception $e) {
  240. Db::rollBack();
  241. return $this->error($e->getMessage(), $errors);
  242. }
  243. return $this->success();
  244. }
  245. /**
  246. * 订单类型
  247. */
  248. public function rechargeAddress()
  249. {
  250. $list = DepositAddress::field(['currency', 'network_type', 'address', 'withdraw_fee','is_withdraw'])->select()->toArray();
  251. $list[] = [
  252. 'currency' => '银行卡',
  253. 'network_type' => 'BANK',
  254. 'address' => '',
  255. 'withdraw_fee' => '0.00',
  256. 'is_withdraw' => 0,
  257. ];
  258. return $this->success($list);
  259. }
  260. /**
  261. * 提现订单
  262. */
  263. public function withdraws()
  264. {
  265. try {
  266. $params = $this->request->param();
  267. $page = isset($params['page']) ? intval($params['page']) : 1;
  268. $limit = isset($params['limit']) ? intval($params['limit']) : 15;
  269. $query = Withdraw::alias('withdraw')->join('user', 'withdraw.user_id=user.user_id','left');
  270. if (isset($params['start_time'])) {
  271. $start_time = strtotime($params['start_time'].' 00:00:00');
  272. $query = $query->where('withdraw.create_time', '>=', $start_time);
  273. }
  274. if (isset($params['end_time'])) {
  275. $end_time = strtotime($params['end_time'].' 23:59:59');
  276. $query = $query->where('withdraw.create_time', '<=', $end_time);
  277. }
  278. if (isset($params['operation_start'])) {
  279. $operation_start = strtotime($params['operation_start'].' 00:00:00');
  280. $query->where('withdraw.operation_time', '>=', $operation_start);
  281. }
  282. if (isset($params['operation_end'])) {
  283. $operation_end = strtotime($params['operation_end'].' 23:59:59');
  284. $query->where('withdraw.operation_time', '<=', $operation_end);
  285. }
  286. if (isset($params['status']) && $params['status'] !== null) {
  287. $query->where('withdraw.status', $params['status']);
  288. }
  289. if (!empty($params['order_no'])) {
  290. $query->where('withdraw.order_no', $params['order_no']);
  291. }
  292. if (!empty($params['currency'])) {
  293. $query->where('withdraw.currency', $params['currency']);
  294. }
  295. if (!empty($params['realname'])) {
  296. $query = $query->where(function ($query) use ($params) {
  297. $query->where('user.realname', 'like', "%{$params['realname']}%")
  298. ->orWhere('user.user_id', 'like', "%{$params['realname']}%");
  299. });
  300. }
  301. if (!empty($params['method'])) {
  302. if ($params['method'] == '银行卡') $params['method'] = 'bank';
  303. $query->where('withdraw.currency', $params['method']);
  304. }
  305. $count = $query->count();
  306. $list = $query->field(['withdraw.*','user.realname', 'user.user_id', 'user.remark','user.phone','user.email'])
  307. ->order('withdraw.create_time','desc')
  308. ->limit($limit)
  309. ->page($page)
  310. ->select();
  311. } catch (Exception $e) {
  312. return $this->error($e->getMessage());
  313. }
  314. return $this->success(['count' => $count, 'list' => $list]);
  315. }
  316. /**
  317. * 更改用户余额
  318. */
  319. public function changeMoney()
  320. {
  321. Db::startTrans();
  322. try {
  323. $params = $this->request->param();
  324. $admin = request()->user;
  325. if (!password_verify($params['payment_password'], $admin->payment_password)) {
  326. throw new Exception('资金密码错误');
  327. }
  328. $amount = $params['amount'];
  329. $user = User::where('user_id', $params['user_id'])->find();
  330. switch ($params['type']) {
  331. case "withdraw":
  332. if ($user->money < $amount) {
  333. throw new Exception("余额不足");
  334. }
  335. $balance_after = bcsub($user->money, $amount, 2);
  336. FundsRecord::addData([
  337. 'transaction_type' => 'withdraw',
  338. 'amount_change' => $amount * -1,
  339. 'balance_before' => $user->money,
  340. 'balance_after' => $balance_after,
  341. 'user_id' => $user->user_id
  342. ]);
  343. Withdraw::addData([
  344. 'user_id' => $user->user_id,
  345. 'currency' => 'USDT',
  346. 'network_type' => 'TRC20',
  347. 'channel_address' => '',
  348. 'amount' => $amount,
  349. 'status' => 1,
  350. 'remarks' => '',
  351. 'actual_received' => $amount,
  352. 'operation_time' => time(),
  353. 'handling_fee' => 0,
  354. 'deduction_usd' => $amount,
  355. ]);
  356. $user->money = $balance_after;
  357. $user->save();
  358. break;
  359. case 'recharge':
  360. $balance_after = bcadd($user->money, $amount, 2);
  361. FundsRecord::addData([
  362. 'transaction_type' => 'recharge',
  363. 'amount_change' => $amount,
  364. 'balance_before' => $user->money,
  365. 'balance_after' => $balance_after,
  366. 'user_id' => $user->user_id
  367. ]);
  368. Recharge::addData([
  369. 'currency' => 'USDT',
  370. 'network_type' => 'TRC20',
  371. 'user_id' => $user->user_id,
  372. 'recharge_amount' => $amount,
  373. 'actual_received' => $amount,
  374. 'payment_receipt' => '',
  375. 'status' => 1,
  376. 'operation_time' => time(),
  377. ]);
  378. $user->money = $balance_after;
  379. $user->save();
  380. break;
  381. }
  382. Db::commit();
  383. } catch (Exception $e) {
  384. Db::rollBack();
  385. return $this->error($e->getMessage());
  386. }
  387. return $this->success();
  388. }
  389. /**
  390. * 用户资金记录
  391. */
  392. public function fundsRecords()
  393. {
  394. try {
  395. $params = $this->request->param();
  396. $data = FundsRecord::getList($params, '');
  397. } catch (Exception $e) {
  398. return $this->error($e->getMessage());
  399. }
  400. return $this->success($data);
  401. }
  402. }