Wallet.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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')
  100. ->join(env('database.DATABASE').'.bot_users user', 'recharge.user_id = user.user_id', 'left');
  101. // ->join('user', 'recharge.user_id=user.user_id','left');
  102. if (isset($params['start_time'])) {
  103. $start_time = strtotime($params['start_time'].' 00:00:00');
  104. $query = $query->where('recharge.create_time', '>=', $start_time);
  105. }
  106. if (isset($params['end_time'])) {
  107. $end_time = strtotime($params['end_time'].' 23:59:59');
  108. $query = $query->where('recharge.create_time', '<=', $end_time);
  109. }
  110. if (isset($params['operation_start'])) {
  111. $operation_start = strtotime($params['operation_start'].' 00:00:00');
  112. $query->where('recharge.operation_time', '>=', $operation_start);
  113. }
  114. if (isset($params['operation_end'])) {
  115. $operation_end = strtotime($params['operation_end'].' 23:59:59');
  116. $query->where('recharge.operation_time', '<=', $operation_end);
  117. }
  118. if (isset($params['status']) && $params['status'] !== null) {
  119. $query->where('recharge.status', $params['status']);
  120. }
  121. if (!empty($params['order_no'])) {
  122. $query->where('recharge.order_no', $params['order_no']);
  123. }
  124. if (!empty($params['currency'])) {
  125. $query->where('recharge.currency', $params['currency']);
  126. }
  127. if (!empty($params['realname'])) {
  128. $query = $query->where(function ($query) use ($params) {
  129. $query->where('user.realname', 'like', "%{$params['realname']}%")
  130. ->orWhere('user.user_id', 'like', "%{$params['realname']}%");
  131. });
  132. }
  133. $count = $query->count();
  134. $list = $query->field(['recharge.*','user.realname', 'user.user_id', 'user.remark','user.phone','user.email'])
  135. ->limit($limit)
  136. ->page($page)
  137. ->order('recharge.create_time','desc')
  138. ->select()->toArray();
  139. } catch (Exception $e) {
  140. return $this->error($e->getMessage());
  141. }
  142. return $this->success(['count' => $count, 'list' => $list]);
  143. }
  144. /**
  145. * 提现驳回
  146. */
  147. function withdrawRefuse()
  148. {
  149. $errors = [];
  150. Db::startTrans();
  151. try {
  152. $params = (new WalletValidate())->post()->goCheck('withdrawRefuse');
  153. $operationRemark = $params['operation_remark'] ?? '';
  154. $wo = Withdraw::findOrFail($params['id']);
  155. if ($wo->status != 0) {
  156. $errors = ['id' => $wo->order_no];
  157. throw new Exception("该订单状态无法操作");
  158. }
  159. $user = User::where('user_id', $wo->user_id)->find();
  160. $userMoney = bcadd($user->money, $wo->deduction_usd, 2);
  161. FundsRecord::addData([
  162. 'transaction_type' => 'withdraw',
  163. 'amount_change' => $wo->deduction_usd,
  164. 'balance_before' => $user->money,
  165. 'balance_after' => $userMoney,
  166. 'user_id' => $user->user_id
  167. ]);
  168. $wo->status = 2;
  169. $wo->operation_remark = $operationRemark;
  170. $wo->operator_id = $this->admin_id;
  171. $wo->operation_time = time();
  172. $wo->save();
  173. $user->money = $userMoney;
  174. $user->save();
  175. Db::commit();
  176. } catch (Exception $e) {
  177. Db::rollBack();
  178. return $this->error($e->getMessage(), $errors);
  179. }
  180. return $this->success();
  181. }
  182. /**
  183. * @api {post} /wallet/withdrawPass 提现通过
  184. * @apiDescription 需要手动打款后来此操作
  185. * @apigroup 财务
  186. * @apiVersion 1.0.0
  187. * @apiUse header
  188. * @apiUse lang
  189. *
  190. * @apiParam {int} id 提现订单的ID
  191. * @apiParam {String} safe_word 资金密码
  192. */
  193. function withdrawPass()
  194. {
  195. $errors = [];
  196. Db::startTrans();
  197. try {
  198. $params = (new WalletValidate())->post()->goCheck('withdrawPass');
  199. $safeWord = $params['safe_word'] ?? '';
  200. $admin = Admin::where('id', $this->admin_id)->find();
  201. if (!password_verify($safeWord, $admin->payment_password)) throw new Exception('资金密码错误');
  202. $wo = Withdraw::findOrFail($params['id']);
  203. if ($wo->status != 0) {
  204. $errors = ['id' => $wo->order_no];
  205. throw new Exception("该订单状态无法操作");
  206. }
  207. $wo->operation_time = date('Y-m-d H:i:s');
  208. $wo->operator_id = $this->admin_id;
  209. $wo->status = 1;
  210. $wo->save();
  211. Db::commit();
  212. } catch (Exception $e) {
  213. Db::rollBack();
  214. return $this->error($e->getMessage(), $errors);
  215. }
  216. return $this->success();
  217. }
  218. /**
  219. * 提现收款地址修改
  220. */
  221. function updateAddress()
  222. {
  223. $errors = [];
  224. Db::startTrans();
  225. try {
  226. $params = (new WalletValidate())->post()->goCheck('updateAddress');
  227. $admin = Admin::where('id', $this->admin_id)->find();
  228. if (!password_verify($params['safe_word'], $admin->payment_password)) throw new Exception('资金密码错误');
  229. $wo = Withdraw::find($params['id']);
  230. if ($wo->status != 0) {
  231. $errors = ['id' => $wo->order_no];
  232. throw new Exception("该订单状态无法操作");
  233. }
  234. if ($wo->currency == 'bank') {
  235. $wo->bank_card_no = $params['address'];
  236. } else {
  237. $wo->channel_address = $params['address'];
  238. }
  239. $wo->save();
  240. Db::commit();
  241. } catch (Exception $e) {
  242. Db::rollBack();
  243. return $this->error($e->getMessage(), $errors);
  244. }
  245. return $this->success();
  246. }
  247. /**
  248. * 订单类型
  249. */
  250. public function rechargeAddress()
  251. {
  252. $list = DepositAddress::field(['currency', 'network_type', 'address', 'withdraw_fee','is_withdraw'])->select()->toArray();
  253. $list[] = [
  254. 'currency' => '银行卡',
  255. 'network_type' => 'BANK',
  256. 'address' => '',
  257. 'withdraw_fee' => '0.00',
  258. 'is_withdraw' => 0,
  259. ];
  260. return $this->success($list);
  261. }
  262. /**
  263. * 提现订单
  264. */
  265. public function withdraws()
  266. {
  267. try {
  268. $params = $this->request->param();
  269. $page = isset($params['page']) ? intval($params['page']) : 1;
  270. $limit = isset($params['limit']) ? intval($params['limit']) : 15;
  271. $query = Withdraw::alias('withdraw')
  272. ->join(env('database.DATABASE').'.bot_users user', 'withdraw.user_id = user.user_id', 'left');
  273. // ->join('user', 'withdraw.user_id=user.user_id','left');
  274. if (isset($params['start_time'])) {
  275. $start_time = strtotime($params['start_time'].' 00:00:00');
  276. $query = $query->where('withdraw.create_time', '>=', $start_time);
  277. }
  278. if (isset($params['end_time'])) {
  279. $end_time = strtotime($params['end_time'].' 23:59:59');
  280. $query = $query->where('withdraw.create_time', '<=', $end_time);
  281. }
  282. if (isset($params['operation_start'])) {
  283. $operation_start = strtotime($params['operation_start'].' 00:00:00');
  284. $query->where('withdraw.operation_time', '>=', $operation_start);
  285. }
  286. if (isset($params['operation_end'])) {
  287. $operation_end = strtotime($params['operation_end'].' 23:59:59');
  288. $query->where('withdraw.operation_time', '<=', $operation_end);
  289. }
  290. if (isset($params['status']) && $params['status'] !== null) {
  291. $query->where('withdraw.status', $params['status']);
  292. }
  293. if (!empty($params['order_no'])) {
  294. $query->where('withdraw.order_no', $params['order_no']);
  295. }
  296. if (!empty($params['currency'])) {
  297. $query->where('withdraw.currency', $params['currency']);
  298. }
  299. if (!empty($params['realname'])) {
  300. $query = $query->where(function ($query) use ($params) {
  301. $query->where('user.realname', 'like', "%{$params['realname']}%")
  302. ->orWhere('user.user_id', 'like', "%{$params['realname']}%");
  303. });
  304. }
  305. if (!empty($params['method'])) {
  306. if ($params['method'] == '银行卡') $params['method'] = 'bank';
  307. $query->where('withdraw.currency', $params['method']);
  308. }
  309. $count = $query->count();
  310. $list = $query->field(['withdraw.*','user.realname', 'user.user_id', 'user.remark','user.phone','user.email'])
  311. ->order('withdraw.create_time','desc')
  312. ->limit($limit)
  313. ->page($page)
  314. ->select();
  315. } catch (Exception $e) {
  316. return $this->error($e->getMessage());
  317. }
  318. return $this->success(['count' => $count, 'list' => $list]);
  319. }
  320. /**
  321. * 更改用户余额
  322. */
  323. public function changeMoney()
  324. {
  325. Db::startTrans();
  326. try {
  327. $params = $this->request->param();
  328. $admin = request()->user;
  329. if (!password_verify($params['payment_password'], $admin->payment_password)) {
  330. throw new Exception('资金密码错误');
  331. }
  332. $amount = $params['amount'];
  333. $user = User::where('user_id', $params['user_id'])->find();
  334. switch ($params['type']) {
  335. case "withdraw":
  336. if ($user->money < $amount) {
  337. throw new Exception("余额不足");
  338. }
  339. $balance_after = bcsub($user->money, $amount, 2);
  340. FundsRecord::addData([
  341. 'transaction_type' => 'withdraw',
  342. 'amount_change' => $amount * -1,
  343. 'balance_before' => $user->money,
  344. 'balance_after' => $balance_after,
  345. 'user_id' => $user->user_id
  346. ]);
  347. Withdraw::addData([
  348. 'user_id' => $user->user_id,
  349. 'currency' => 'USDT',
  350. 'network_type' => 'TRC20',
  351. 'channel_address' => '',
  352. 'amount' => $amount,
  353. 'status' => 1,
  354. 'remarks' => '',
  355. 'actual_received' => $amount,
  356. 'operation_time' => time(),
  357. 'handling_fee' => 0,
  358. 'deduction_usd' => $amount,
  359. ]);
  360. $user->money = $balance_after;
  361. $user->save();
  362. break;
  363. case 'recharge':
  364. $balance_after = bcadd($user->money, $amount, 2);
  365. FundsRecord::addData([
  366. 'transaction_type' => 'recharge',
  367. 'amount_change' => $amount,
  368. 'balance_before' => $user->money,
  369. 'balance_after' => $balance_after,
  370. 'user_id' => $user->user_id
  371. ]);
  372. Recharge::addData([
  373. 'currency' => 'USDT',
  374. 'network_type' => 'TRC20',
  375. 'user_id' => $user->user_id,
  376. 'recharge_amount' => $amount,
  377. 'actual_received' => $amount,
  378. 'payment_receipt' => '',
  379. 'status' => 1,
  380. 'operation_time' => time(),
  381. ]);
  382. $user->money = $balance_after;
  383. $user->save();
  384. break;
  385. }
  386. Db::commit();
  387. } catch (Exception $e) {
  388. Db::rollBack();
  389. return $this->error($e->getMessage());
  390. }
  391. return $this->success();
  392. }
  393. /**
  394. * 用户资金记录
  395. */
  396. public function fundsRecords()
  397. {
  398. try {
  399. $params = $this->request->param();
  400. $data = FundsRecord::getList($params, '');
  401. } catch (Exception $e) {
  402. return $this->error($e->getMessage());
  403. }
  404. return $this->success($data);
  405. }
  406. }