TradeController.php 6.0 KB

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