TradeController.php 5.1 KB

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