TradeController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\api\controller\trade;
  3. use app\models\trade\CashTradeOrder;
  4. use app\models\user\UserMoney;
  5. use app\models\user\UserMoneyOrder;
  6. use app\Request;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\services\CacheService;
  9. use crmeb\services\UtilService;
  10. use Psr\SimpleCache\InvalidArgumentException;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. class TradeController
  15. {
  16. /**
  17. * @param Request $request
  18. * @return string
  19. * @throws InvalidArgumentException
  20. */
  21. public function getNewPayCode(Request $request)
  22. {
  23. $uid = $request->uid();
  24. $code = md5(time() . rand(0, 999999));
  25. while (CacheService::redisHandler()->get("pay_code_{$code}", '')) {
  26. $code = md5($code);
  27. }
  28. CacheService::redisHandler()->set("pay_code_{$code}", $uid, 60);
  29. return app('json')->success('ok', ['code' => $code, 'exceed_time' => date('Y-m-d H:i:s', time() + 60)]);
  30. }
  31. public function checkTradePsw(Request $request)
  32. {
  33. $user = $request->user();
  34. $psw = $request->post('trade_psw', '');
  35. if (md5(md5($psw)) != $user['trade_pwd']) {
  36. return app('json')->fail();
  37. }
  38. return app('json')->success();
  39. }
  40. /**
  41. * @param Request $request
  42. * @return mixed
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws InvalidArgumentException
  46. * @throws ModelNotFoundException
  47. */
  48. public function autoPay(Request $request)
  49. {
  50. $uid = $request->uid();
  51. list($type, $num, $code) = UtilService::postMore(
  52. [
  53. ['type', 'USDT', '', '', ['not_empty_check', function ($item) {
  54. $moneys = sys_data('money_type');
  55. $new_money = [];
  56. foreach ($moneys as $v) {
  57. if ($v['can_trade'] == 1) {
  58. $new_money[] = $v['code'];
  59. }
  60. }
  61. return in_array($item, $new_money);
  62. }], ['请选择交易的币种', '请选择支持交易的币种']],
  63. ['num', 0],
  64. ['code', '']
  65. ], $request, true);
  66. if (!$num || !$code) {
  67. return app('json')->fail('参数不足');
  68. }
  69. $pay_uid = CacheService::redisHandler()->get("pay_code_{$code}", '');
  70. if (!$pay_uid) {
  71. return app('json')->fail('付款码已过期');
  72. }
  73. $res = UserMoney::tradeMoney($pay_uid, $uid, $type, $num);
  74. if ($res) {
  75. CacheService::redisHandler()->delete("pay_code_{$code}");
  76. CacheService::redisHandler()->set("pay_result_{$code}", 'SUCCESS');
  77. return app('json')->success('支付成功');
  78. } else {
  79. CacheService::redisHandler()->delete("pay_code_{$code}");
  80. CacheService::redisHandler()->set("pay_result_{$code}", UserMoney::getErrorInfo('FAIL'));
  81. return app('json')->fail('支付失败:' . UserMoney::getErrorInfo('支付错误'));
  82. }
  83. }
  84. /**
  85. * @param Request $request
  86. * @return mixed
  87. * @throws InvalidArgumentException
  88. */
  89. public function payResult(Request $request)
  90. {
  91. $code = $request->get('code', '');
  92. $result = CacheService::redisHandler()->get("pay_result_{$code}", '');
  93. if ($result) return app('json')->success($result);
  94. else return app('json')->success('NOT_USE');
  95. }
  96. /**
  97. * @param Request $request
  98. * @return mixed
  99. * @throws DataNotFoundException
  100. * @throws DbException
  101. * @throws ModelNotFoundException
  102. */
  103. public function goPay(Request $request)
  104. {
  105. $user = $request->user();
  106. $uid = $user['uid'];
  107. list($type, $num, $to_uid, , $captcha) = UtilService::postMore(
  108. [
  109. ['type', 'USDT', '', '', ['not_empty_check', function ($item) {
  110. $moneys = sys_data('money_type');
  111. $new_money = [];
  112. foreach ($moneys as $v) {
  113. if ($v['can_trade'] == 1) {
  114. $new_money[] = $v['code'];
  115. }
  116. }
  117. return in_array($item, $new_money);
  118. }], ['请选择交易的币种', '请选择支持交易的币种']],
  119. ['num', 0],
  120. ['to_uid', 0],
  121. ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
  122. return md5(md5($item)) == $user['trade_pwd'];
  123. }], ['请输入交易密码', '交易密码错误']],
  124. ['captcha', '']
  125. ], $request, true);
  126. if (!$num || !$to_uid) {
  127. return app('json')->fail('参数不足');
  128. }
  129. $price = 0;
  130. $list = sys_data('money_type');
  131. foreach ($list as $v) {
  132. if ($v['code'] == $type) {
  133. $price = $v['price'] ? $v['price'] : CashTradeOrder::averagePrice($v['code']);
  134. }
  135. }
  136. if (bcmul($num, $price, 2) >= 10000) {
  137. $verifyCode = CacheService::get('code_' . $user['account']);
  138. if (!$verifyCode)
  139. return app('json')->fail('请先获取验证码');
  140. $verifyCode = substr($verifyCode, 0, 6);
  141. if ($verifyCode != $captcha)
  142. return app('json')->fail('验证码错误');
  143. }
  144. $res = UserMoney::tradeMoney($uid, $to_uid, $type, $num);
  145. if ($res) {
  146. return app('json')->success('支付成功');
  147. } else {
  148. return app('json')->fail('支付失败:' . UserMoney::getErrorInfo('支付错误'));
  149. }
  150. }
  151. }