Wallet.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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::beginTransaction();
  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::beginTransaction();
  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->operation_time = time();
  169. $wo->save();
  170. $user->money = $userMoney;
  171. $user->save();
  172. DB::commit();
  173. } catch (Exception $e) {
  174. DB::rollBack();
  175. return $this->error($e->getMessage(), $errors);
  176. }
  177. return $this->success();
  178. }
  179. /**
  180. * @api {post} /wallet/withdrawPass 提现通过
  181. * @apiDescription 需要手动打款后来此操作
  182. * @apigroup 财务
  183. * @apiVersion 1.0.0
  184. * @apiUse header
  185. * @apiUse lang
  186. *
  187. * @apiParam {int} id 提现订单的ID
  188. * @apiParam {String} safe_word 资金密码
  189. */
  190. function withdrawPass()
  191. {
  192. $errors = [];
  193. DB::beginTransaction();
  194. try {
  195. $params = (new WalletValidate())->post()->goCheck('withdrawPass');
  196. $safeWord = $params['safe_word'] ?? '';
  197. $admin = Admin::where('id', $this->admin_id)->find();
  198. if (!password_verify($safeWord, $admin->payment_password)) throw new Exception('资金密码错误');
  199. $wo = Withdraw::findOrFail($params['id']);
  200. if ($wo->status != 0) {
  201. $errors = ['id' => $wo->order_no];
  202. throw new Exception("该订单状态无法操作");
  203. }
  204. $wo->operation_time = date('Y-m-d H:i:s');
  205. $wo->status = 1;
  206. $wo->save();
  207. DB::commit();
  208. } catch (Exception $e) {
  209. DB::rollBack();
  210. return $this->error($e->getMessage(), $errors);
  211. }
  212. return $this->success();
  213. }
  214. /**
  215. * 提现收款地址修改
  216. */
  217. function updateAddress()
  218. {
  219. $errors = [];
  220. DB::beginTransaction();
  221. try {
  222. $params = (new WalletValidate())->post()->goCheck('updateAddress');
  223. $admin = Admin::where('id', $this->admin_id)->find();
  224. if (!password_verify($params['safe_word'], $admin->payment_password)) throw new Exception('资金密码错误');
  225. $wo = Withdraw::find($params['id']);
  226. if ($wo->status != 0) {
  227. $errors = ['id' => $wo->order_no];
  228. throw new Exception("该订单状态无法操作");
  229. }
  230. if ($wo->currency == 'bank') {
  231. $wo->bank_card_no = $params['address'];
  232. } else {
  233. $wo->channel_address = $params['address'];
  234. }
  235. $wo->save();
  236. DB::commit();
  237. } catch (Exception $e) {
  238. DB::rollBack();
  239. return $this->error($e->getMessage(), $errors);
  240. }
  241. return $this->success();
  242. }
  243. /**
  244. * 订单类型
  245. */
  246. public function rechargeAddress()
  247. {
  248. $list = DepositAddress::select(['currency', 'network_type', 'deposit_address', 'withdraw_fee','is_withdraw'])->get()->toArray();
  249. $list[] = [
  250. 'currency' => '银行卡',
  251. 'network_type' => 'BANK',
  252. 'deposit_address' => '',
  253. 'withdraw_fee' => '0.00',
  254. 'is_withdraw' => 0,
  255. ];
  256. return $this->success($list);
  257. }
  258. /**
  259. * @api {get} /wallet/withdraws 提现订单
  260. * @apiGroup 财务
  261. * @apiVersion 1.0.0
  262. * @apiUse header
  263. * @apiUse lang
  264. *
  265. * @apiParam {int} [page=1]
  266. * @apiParam {int} [limit=15]
  267. *
  268. * @apiParam {String} [order_no] 订单号(完整)
  269. * @apiParam {String} [username] 用户名、user_id
  270. * @apiParam {String} [method] 订单类型
  271. * - 通过 <a href="javascript:;" onclick="toMenu('财务','GetWalletRechargeaddress')">订单类型</a> 获取
  272. * @apiParam {String} [create_start] 创建开始日期
  273. * - 格式:yyyy-mm-dd
  274. * @apiParam {String} [create_end] 创建结束日期
  275. * - 格式:yyyy-mm-dd
  276. * @apiParam {String} [operation_start] 审核开始日期
  277. * - 格式:yyyy-mm-dd
  278. * @apiParam {String} [operation_end] 审核结束日期
  279. * - 格式:yyyy-mm-dd
  280. */
  281. public function withdraws()
  282. {
  283. try {
  284. $params = $this->request->param();
  285. $page = isset($params['page']) ? intval($params['page']) : 1;
  286. $limit = isset($params['limit']) ? intval($params['limit']) : 15;
  287. $query = Withdraw::alias('withdraw')->join('user', 'withdraw.user_id=user.user_id','left');
  288. if (isset($params['start_time'])) {
  289. $start_time = strtotime($params['start_time'].' 00:00:00');
  290. $query = $query->where('withdraw.create_time', '>=', $start_time);
  291. }
  292. if (isset($params['end_time'])) {
  293. $end_time = strtotime($params['end_time'].' 23:59:59');
  294. $query = $query->where('withdraw.create_time', '<=', $end_time);
  295. }
  296. if (isset($params['operation_start'])) {
  297. $operation_start = strtotime($params['operation_start'].' 00:00:00');
  298. $query->where('withdraw.operation_time', '>=', $operation_start);
  299. }
  300. if (isset($params['operation_end'])) {
  301. $operation_end = strtotime($params['operation_end'].' 23:59:59');
  302. $query->where('withdraw.operation_time', '<=', $operation_end);
  303. }
  304. if (isset($params['status']) && $params['status'] !== null) {
  305. $query->where('withdraw.status', $params['status']);
  306. }
  307. if (!empty($params['order_no'])) {
  308. $query->where('withdraw.order_no', $params['order_no']);
  309. }
  310. if (!empty($params['realname'])) {
  311. $query = $query->where(function ($query) use ($params) {
  312. $query->where('user.realname', 'like', "%{$params['realname']}%")
  313. ->orWhere('user.user_id', 'like', "%{$params['realname']}%");
  314. });
  315. }
  316. if (!empty($params['method'])) {
  317. if ($params['method'] == '银行卡') $params['method'] = 'bank';
  318. $query->where('withdraw.currency', $params['method']);
  319. }
  320. $count = $query->count();
  321. $list = $query->field(['withdraw.*','user.realname', 'user.user_id', 'user.remark','user.phone','user.email'])
  322. ->order('withdraw.create_time','desc')
  323. ->limit($limit)
  324. ->page($page)
  325. ->select();
  326. } catch (Exception $e) {
  327. return $this->error($e->getMessage());
  328. }
  329. return $this->success(['count' => $count, 'list' => $list]);
  330. }
  331. /**
  332. * 更改用户余额
  333. */
  334. public function changeMoney()
  335. {
  336. DB::startTrans();
  337. try {
  338. $params = $this->request->param();
  339. $admin = request()->user;
  340. if (!password_verify($params['payment_password'], $admin->payment_password)) {
  341. throw new Exception('资金密码错误');
  342. }
  343. $amount = $params['amount'];
  344. $user = User::where('user_id', $params['user_id'])->find();
  345. switch ($params['type']) {
  346. case "withdraw":
  347. if ($user->money < $amount) {
  348. throw new Exception("余额不足");
  349. }
  350. $balance_after = bcsub($user->money, $amount, 2);
  351. FundsRecord::addData([
  352. 'transaction_type' => 'withdraw',
  353. 'amount_change' => $amount * -1,
  354. 'balance_before' => $user->money,
  355. 'balance_after' => $balance_after,
  356. 'user_id' => $user->user_id
  357. ]);
  358. Withdraw::addData([
  359. 'user_id' => $user->user_id,
  360. 'currency' => 'USDT',
  361. 'network_type' => 'TRC20',
  362. 'channel_address' => '',
  363. 'amount' => $amount,
  364. 'status' => 1,
  365. 'remarks' => '',
  366. 'actual_received' => $amount,
  367. 'operation_time' => time(),
  368. 'handling_fee' => 0,
  369. 'deduction_usd' => $amount,
  370. ]);
  371. $user->money = $balance_after;
  372. $user->save();
  373. break;
  374. case 'recharge':
  375. $balance_after = bcadd($user->money, $amount, 2);
  376. FundsRecord::addData([
  377. 'transaction_type' => 'recharge',
  378. 'amount_change' => $amount,
  379. 'balance_before' => $user->money,
  380. 'balance_after' => $balance_after,
  381. 'user_id' => $user->user_id
  382. ]);
  383. Recharge::addData([
  384. 'currency' => 'USDT',
  385. 'network_type' => 'TRC20',
  386. 'user_id' => $user->user_id,
  387. 'recharge_amount' => $amount,
  388. 'actual_received' => $amount,
  389. 'payment_receipt' => '',
  390. 'status' => 1,
  391. 'operation_time' => time(),
  392. ]);
  393. $user->money = $balance_after;
  394. $user->save();
  395. break;
  396. }
  397. DB::commit();
  398. } catch (Exception $e) {
  399. DB::rollBack();
  400. return $this->error($e->getMessage());
  401. }
  402. return $this->success();
  403. }
  404. /**
  405. * 用户资金记录
  406. */
  407. public function fundsRecords()
  408. {
  409. try {
  410. $params = $this->request->param();
  411. $data = FundsRecord::getList($params, '');
  412. } catch (Exception $e) {
  413. return $this->error($e->getMessage());
  414. }
  415. return $this->success($data);
  416. }
  417. }