CashTradeController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. namespace app\api\controller\trade;
  3. use app\models\mining\MiningMachine;
  4. use app\models\trade\CashTradeOrder;
  5. use app\models\trade\CashTradeOrderPayment;
  6. use app\models\user\User;
  7. use app\models\user\UserMoney;
  8. use app\Request;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\repositories\OrderRepository;
  11. use crmeb\services\AliPayService;
  12. use crmeb\services\CacheService;
  13. use crmeb\services\UtilService;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. class CashTradeController
  18. {
  19. /**
  20. * @param $type
  21. * @return bool|string
  22. */
  23. public function merged($type)
  24. {
  25. // $key = 'mx0J52sOMse4D3bE3c';
  26. // $sec = 'ea508c4381c84527ae892fc5cbdd674d';
  27. $type = strtolower($type);
  28. // $type = 'eth';
  29. $url = "https://api.huobi.de.com/market/detail/merged?symbol={$type}usdt";
  30. // var_dump($url);
  31. $res = do_request($url, [], null, false, true);
  32. return json_decode($res, true);
  33. }
  34. /**
  35. * @param Request $request
  36. * @return mixed
  37. * @throws DataNotFoundException
  38. * @throws DbException
  39. * @throws ModelNotFoundException
  40. */
  41. /**
  42. * @param Request $request
  43. * @return mixed
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. */
  48. public function can_trade_money(Request $request)
  49. {
  50. $list = sys_data('money_type');
  51. $new = [];
  52. foreach ($list as $v) {
  53. if (explode('_', $v['code'])[0] == "USDT") {
  54. if ($v['price'] <= 0) {
  55. //计算前一天成交的平均价格
  56. $v['price'] = CashTradeOrder::averagePrice($v['code']);
  57. }
  58. $usdt_price = $v['price'];
  59. }
  60. if ($v['is_trade']) {
  61. // if ($v['price'] <= 0) {
  62. // //计算前一天成交的平均价格
  63. // $v['price'] = CashTradeOrder::averagePrice($v['code']);
  64. // }
  65. if ($request->uid())
  66. $v['wallet'] = UserMoney::initialUserMoney($request->uid(), $v['code']);
  67. unset($v['cash_commission_ratio']);
  68. unset($v['cash_commission_type']);
  69. unset($v['can_cash']);
  70. unset($v['can_trade']);
  71. unset($v['is_trade']);
  72. $new[] = $v;
  73. }
  74. }
  75. foreach ($new as $k => $v) {
  76. $new[$k]['usdt'] = (isset($usdt_price) && $usdt_price > 0) ? bcdiv($new[$k]['price'], $usdt_price, 8) : 0;
  77. }
  78. return app('json')->success('ok', $new);
  79. }
  80. public function all_money_redis()
  81. {
  82. $list = sys_data('money_type');
  83. $new = [];
  84. foreach ($list as $v) {
  85. if (explode('_', $v['code'])[0] == "USDT") {
  86. if ($v['price'] <= 0) {
  87. //计算前一天成交的平均价格
  88. $v['price'] = CashTradeOrder::averagePrice($v['code']);
  89. }
  90. $usdt_price = $v['price'];
  91. }
  92. $v['has_mining'] = (MiningMachine::valid()->where('get_money_type', $v['code'])->find()) ? 1 : 0;
  93. $new[] = $v;
  94. }
  95. foreach ($new as $k => $v) {
  96. if (!(explode('_', $v['code'])[0] == "USDT")) {
  97. $res = $this->merged($v['code']);
  98. if ($res['status'] == 'error') {
  99. $new[$k]['usdt'] = 0;
  100. $new[$k]['price'] = 0;
  101. $new[$k]['up'] = 0;
  102. } else {
  103. $new[$k]['usdt'] = $res['tick']['close'];
  104. $new[$k]['price'] = bcmul($usdt_price, $new[$k]['usdt'], 2);
  105. $new[$k]['up'] = bcdiv(bcsub($res['tick']['close'], $res['tick']['open'], 8), $res['tick']['open'], 4);
  106. }
  107. } else {
  108. $new[$k]['usdt'] = 1;
  109. $new[$k]['up'] = 0;
  110. }
  111. }
  112. CacheService::redisHandler()->set('all_money_kp', $new);
  113. }
  114. public function all_money(Request $request)
  115. {
  116. $info = CacheService::redisHandler()->get('all_money_kp');
  117. if (!$info) {
  118. $this->all_money_redis();
  119. $info = CacheService::redisHandler()->get('all_money_kp');
  120. }
  121. foreach ($info as &$v) {
  122. if ($request->uid())
  123. $v['wallet'] = UserMoney::initialUserMoney($request->uid(), $v['code']);
  124. }
  125. return app('json')->success('ok', $info);
  126. }
  127. public function can_cash_money(Request $request)
  128. {
  129. $list = sys_data('money_type');
  130. $new = [];
  131. foreach ($list as $v) {
  132. if (explode('_', $v['code'])[0] == "USDT") {
  133. if ($v['price'] <= 0) {
  134. //计算前一天成交的平均价格
  135. $v['price'] = CashTradeOrder::averagePrice($v['code']);
  136. }
  137. $usdt_price = $v['price'];
  138. }
  139. if ($v['can_cash']) {
  140. // if ($v['price'] <= 0) {
  141. // //计算前一天成交的平均价格
  142. // $v['price'] = CashTradeOrder::averagePrice($v['code']);
  143. // }
  144. if ($request->uid())
  145. $v['wallet'] = UserMoney::initialUserMoney($request->uid(), $v['code']);
  146. unset($v['cash_commission_ratio']);
  147. unset($v['cash_commission_type']);
  148. unset($v['can_cash']);
  149. unset($v['can_trade']);
  150. unset($v['is_trade']);
  151. $new[] = $v;
  152. }
  153. }
  154. foreach ($new as $k => $v) {
  155. $new[$k]['usdt'] = (isset($usdt_price) && $usdt_price > 0) ? bcdiv($new[$k]['price'], $usdt_price, 8) : 0;
  156. }
  157. return app('json')->success('ok', $new);
  158. }
  159. /**
  160. * @param Request $request
  161. * @return mixed
  162. */
  163. public function sale_list(Request $request)
  164. {
  165. $where = UtilService::getMore([
  166. ['money_type', ''],
  167. ['uid', 0],
  168. ['page', 1],
  169. ['limit', 10],
  170. ['status', 1],
  171. ], $request);
  172. $where['trade_type'] = 1;
  173. return app('json')->success('ok', CashTradeOrder::getList($where));
  174. }
  175. /**
  176. * @param Request $request
  177. * @return mixed
  178. */
  179. public function buy_list(Request $request)
  180. {
  181. $where = UtilService::getMore([
  182. ['money_type', ''],
  183. ['uid', 0],
  184. ['page', 1],
  185. ['limit', 10],
  186. ['status', 1],
  187. ], $request);
  188. $where['trade_type'] = 2;
  189. return app('json')->success('ok', CashTradeOrder::getList($where));
  190. }
  191. /**
  192. * 挂起
  193. * @param Request $request
  194. * @return mixed
  195. * @throws DataNotFoundException
  196. * @throws DbException
  197. * @throws ModelNotFoundException
  198. */
  199. public function hang(Request $request)
  200. {
  201. $user = $request->user();
  202. $uid = $user['uid'];
  203. list($money_type, $money_num, $price, $trade_type, $trade_way, $from, $hang_time, , $captcha) = UtilService::postMore([
  204. ['money_type', 'USDT_ERC20', '', '', ['not_empty_check', function ($item) {
  205. $moneys = sys_data('money_type');
  206. $new_money = [];
  207. foreach ($moneys as $v) {
  208. if ($v['is_trade'] == 1) {
  209. $new_money[] = $v['code'];
  210. }
  211. }
  212. return in_array($item, $new_money);
  213. }], ['请选择交易的币种', '请选择支持交易的币种']],
  214. ['money_num', 0],
  215. ['price', 0],
  216. ['trade_type', 0, '', '', ['not_empty_check', function ($item) {
  217. return in_array($item, [1, 2]);
  218. }], '请选择挂买或挂卖'],
  219. ['trade_way', 'weixin'],
  220. ['from', 'weixin'],
  221. ['hang_time', 0, '', '', ['not_empty_check', function ($item) {
  222. return $item > 0 && $item < 1000;
  223. }], '请输入正确的挂出时常'],
  224. ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
  225. return md5(md5($item)) == $user['trade_pwd'];
  226. }], ['请输入交易密码', '交易密码错误']],
  227. ['captcha', '']
  228. ], $request, true);
  229. if ($money_num <= 0) {
  230. return app('json')->fail('请输入币数');
  231. }
  232. if ($price <= 0) {
  233. return app('json')->fail('请输入单价');
  234. }
  235. $sum = bcmul($money_num, $price, 2);
  236. if ($sum >= 10000) {
  237. $verifyCode = CacheService::get('code_' . $user['account']);
  238. if (!$verifyCode)
  239. return app('json')->fail('请先获取验证码');
  240. $verifyCode = substr($verifyCode, 0, 6);
  241. if ($verifyCode != $captcha)
  242. return app('json')->fail('验证码错误');
  243. }
  244. switch ($trade_type) {
  245. case 1:
  246. $res = CashTradeOrder::hangSale($uid, $money_type, $money_num, $price, $hang_time);
  247. break;
  248. case 2:
  249. $res = CashTradeOrder::hangBuy2($uid, $money_type, $money_num, $price, $hang_time);//改方式
  250. // $res = CashTradeOrder::hangBuy($uid, $money_type, $money_num, $price, $hang_time);
  251. // if ($res) {
  252. // $orderId = $res['order_id'];
  253. // switch ($trade_way) {
  254. // case "weixin":
  255. // $orderInfo = CashTradeOrderPayment::where('order_id', $orderId)->find();
  256. // if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  257. // $orderInfo = $orderInfo->toArray();
  258. // if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  259. // //支付金额为0
  260. // if (bcsub((float)$orderInfo['pay_money'], 0, 2) <= 0) {
  261. // //创建订单jspay支付
  262. // $payPriceStatus = CashTradeOrderPayment::jsPayPrice($orderId, $uid);
  263. // if ($payPriceStatus)//0元支付成功
  264. // return app('json')->status('success', '微信支付成功');
  265. // else
  266. // return app('json')->status('pay_error', CashTradeOrderPayment::getErrorInfo());
  267. // } else {
  268. // try {
  269. // if ($from == 'routine') {
  270. // $jsConfig = OrderRepository::tradeJsPay($orderId); //创建订单jspay
  271. // } else if ($from == 'weixinh5') {
  272. // $jsConfig = OrderRepository::tradeH5Pay($orderId);
  273. // } else {
  274. // $jsConfig = OrderRepository::tradeWxPay($orderId);
  275. // }
  276. // } catch (\Exception $e) {
  277. // return app('json')->status('pay_error', $e->getMessage());
  278. // }
  279. // $res['jsConfig'] = $jsConfig;
  280. // if ($from == 'weixinh5') {
  281. // return app('json')->status('wechat_h5_pay', '订单创建成功', $res);
  282. // } else {
  283. // return app('json')->status('wechat_pay', '订单创建成功', $res);
  284. // }
  285. // }
  286. // case 'yue':
  287. // if (CashTradeOrderPayment::yuePay($orderId, $request->uid()))
  288. // return app('json')->status('success', '余额支付成功');
  289. // else {
  290. // $errorinfo = CashTradeOrderPayment::getErrorInfo();
  291. // if (is_array($errorinfo))
  292. // return app('json')->status($errorinfo['status'], $errorinfo['msg']);
  293. // else
  294. // return app('json')->status('pay_error', $errorinfo);
  295. // }
  296. // case 'ali':
  297. // try {
  298. // $jsConfig = AliPayService::createTrade($orderId); //创建订单jspay
  299. // } catch (\Exception $e) {
  300. // return app('json')->status('pay_error', $e->getMessage());
  301. // }
  302. // $res['jsConfig'] = $jsConfig;
  303. // return app('json')->status('ali_pay', '订单创建成功', $res);
  304. // default:
  305. // return app('json')->fail('支付方式错误');
  306. // }
  307. // } else return app('json')->fail(CashTradeOrder::getErrorInfo('订单生成失败!'));
  308. break;
  309. default:
  310. $res = false;
  311. break;
  312. }
  313. if ($res) {
  314. return app('json')->success('挂出成功');
  315. } else {
  316. return app('json')->fail('挂出失败:' . CashTradeOrder::getErrorInfo('挂出错误'));
  317. }
  318. }
  319. public function trade(Request $request)
  320. {
  321. $user = $request->user();
  322. $uid = $user['uid'];
  323. list($id, $trade_way, $from, , $captcha) = UtilService::postMore([
  324. ['id', 'USDT_ERC20', '', '', ['not_empty_check'], ['请选择要进行的交易']],
  325. ['trade_way', 'weixin'],
  326. ['from', 'weixin'],
  327. ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
  328. return md5(md5($item)) == $user['trade_pwd'];
  329. }], ['请输入交易密码', '交易密码错误']],
  330. ['captcha', '']
  331. ], $request, true);
  332. $order = CashTradeOrder::setStatus(1)->where('id', $id)->find();
  333. // var_dump(CashTradeOrder::getLastSql());
  334. if (!$order) {
  335. return app('json')->fail('订单不存在或已结束挂出');
  336. }
  337. $sum = bcmul($order['money_num'], $order['price'], 2);
  338. if ($sum >= 10000) {
  339. $verifyCode = CacheService::get('code_' . $user['account']);
  340. if (!$verifyCode)
  341. return app('json')->fail('请先获取验证码');
  342. $verifyCode = substr($verifyCode, 0, 6);
  343. if ($verifyCode != $captcha)
  344. return app('json')->fail('验证码错误');
  345. }
  346. switch ($order['trade_type']) {
  347. case 2:
  348. $res = CashTradeOrder::sale($uid, $id);
  349. break;
  350. case 1:
  351. $res = CashTradeOrder::buy2($uid, $id);
  352. // if ($res) {
  353. // $orderId = $res['order_id'];
  354. // switch ($trade_way) {
  355. // case "weixin":
  356. // $orderInfo = CashTradeOrderPayment::where('order_id', $orderId)->find();
  357. // if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  358. // $orderInfo = $orderInfo->toArray();
  359. // if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  360. // //支付金额为0
  361. // if (bcsub((float)$orderInfo['pay_money'], 0, 2) <= 0) {
  362. // //创建订单jspay支付
  363. // $payPriceStatus = CashTradeOrderPayment::jsPayPrice($orderId, $uid);
  364. // if ($payPriceStatus)//0元支付成功
  365. // return app('json')->status('success', '微信支付成功');
  366. // else
  367. // return app('json')->status('pay_error', CashTradeOrderPayment::getErrorInfo());
  368. // } else {
  369. // try {
  370. // if ($from == 'routine') {
  371. // $jsConfig = OrderRepository::tradeJsPay($orderId); //创建订单jspay
  372. // } else if ($from == 'weixinh5') {
  373. // $jsConfig = OrderRepository::tradeH5Pay($orderId);
  374. // } else {
  375. // $jsConfig = OrderRepository::tradeWxPay($orderId);
  376. // }
  377. // } catch (\Exception $e) {
  378. // return app('json')->status('pay_error', $e->getMessage());
  379. // }
  380. // $res['jsConfig'] = $jsConfig;
  381. // if ($from == 'weixinh5') {
  382. // return app('json')->status('wechat_h5_pay', '订单创建成功', $res);
  383. // } else {
  384. // return app('json')->status('wechat_pay', '订单创建成功', $res);
  385. // }
  386. // }
  387. // case 'yue':
  388. // if (CashTradeOrderPayment::yuePay($orderId, $request->uid()))
  389. // return app('json')->status('success', '余额支付成功');
  390. // else {
  391. // $errorinfo = CashTradeOrderPayment::getErrorInfo();
  392. // if (is_array($errorinfo))
  393. // return app('json')->status($errorinfo['status'], $errorinfo['msg']);
  394. // else
  395. // return app('json')->status('pay_error', $errorinfo);
  396. // }
  397. // case 'ali':
  398. // try {
  399. // $jsConfig = AliPayService::createTrade($orderId); //创建订单jspay
  400. // } catch (\Exception $e) {
  401. // return app('json')->status('pay_error', $e->getMessage());
  402. // }
  403. // $res['jsConfig'] = $jsConfig;
  404. // return app('json')->status('ali_pay', '订单创建成功', $res);
  405. // default:
  406. // return app('json')->fail('支付方式错误');
  407. // }
  408. // } else return app('json')->fail(CashTradeOrder::getErrorInfo('订单生成失败!'));
  409. break;
  410. default:
  411. $res = false;
  412. break;
  413. }
  414. if ($res) {
  415. return app('json')->success('交易开始进行,' . $order['trade_type'] == 1 ? '请通过用户的转账方式交易并上传交易凭证' : '请等待买家转账并上传凭证');
  416. } else {
  417. return app('json')->fail('交易失败:' . CashTradeOrder::getErrorInfo('挂出错误'));
  418. }
  419. }
  420. public function myOrder(Request $request)
  421. {
  422. $where = UtilService::getMore([
  423. ['money_type', ''],
  424. ['page', 1],
  425. ['limit', 10],
  426. ['status', ''],
  427. ['trade_type', '']
  428. ], $request);
  429. // $where['trade_type'] = 2;
  430. $where['uid'] = $request->uid();
  431. return app('json')->success('ok', CashTradeOrder::getList($where));
  432. }
  433. public function uploadProof($id, Request $request)
  434. {
  435. $order = CashTradeOrder::get($id);
  436. $proof = $request->post('proof', '');
  437. if (!not_empty_check($proof)) return app('json')->fail('请上传凭证');
  438. if ($order['close_time'] < time()) return app('json')->fail('订单已关闭');
  439. switch ($order['trade_type']) {
  440. case 1:
  441. if ($order['trade_uid'] != $request->uid()) return app('json')->fail('订单交易方错误');
  442. if ($order['status'] != -1) return app('json')->fail('订单状态错误');
  443. break;
  444. case 2:
  445. if ($order['uid'] != $request->uid()) return app('json')->fail('订单交易方错误');
  446. if ($order['status'] != 0) return app('json')->fail('订单状态错误');
  447. break;
  448. default:
  449. break;
  450. }
  451. $res = CashTradeOrder::where('id', $id)->update(['proof' => $proof, 'status' => 3]);
  452. if ($res) {
  453. return app('json')->fail('上传成功');
  454. } else {
  455. return app('json')->fail('上传失败');
  456. }
  457. }
  458. public function cancel($id, Request $request)
  459. {
  460. $order = CashTradeOrder::get($id);
  461. if ($order['close_time'] < time()) return app('json')->fail('订单已关闭');
  462. if ($order['status'] > 0) return app('json')->fail('订单不可撤销');
  463. BaseModel::beginTrans();
  464. $res = true;
  465. switch ($order['trade_type']) {
  466. case 1:
  467. //退回币
  468. $res = $res && UserMoney::incomeMoney($order['uid'], $order['money_type'], $order['money_num'], 'hang_sale_cancel', '挂卖取消', '挂卖取消,退回钱包');
  469. $res = $res && CashTradeOrder::where('id', $order['id'])->update(['status' => 2]);
  470. break;
  471. case 2:
  472. //
  473. if ($order['trade_uid'])
  474. $res = $res && UserMoney::incomeMoney($order['trade_uid'], $order['money_type'], $order['money_num'], 'hang_buy_cancel', '挂买取消', '挂买取消,退回钱包');
  475. $res = $res && CashTradeOrder::where('id', $order['id'])->update(['status' => 2]);
  476. break;
  477. default:
  478. break;
  479. }
  480. BaseModel::checkTrans($res);
  481. if ($res) {
  482. return app('json')->fail('取消成功');
  483. } else {
  484. return app('json')->fail('取消失败');
  485. }
  486. }
  487. public function check($id, $ok, Request $request)
  488. {
  489. $order = CashTradeOrder::where('id', $id)->where('status', 3)->find();
  490. if (!in_array($ok, ['y', 'n'])) return app('json')->fail('参数错误');
  491. $res = true;
  492. BaseModel::beginTrans();
  493. if ($ok == 'y') {
  494. switch ($order['trade_type']) {
  495. case 1:
  496. if ($order['uid'] != $request->uid()) return app('json')->fail('订单审核方错误');
  497. $res = $res && UserMoney::incomeMoney($order['trade_uid'], $order['money_type'], $order['money_num'], '购币成功', 'buy_success', '购币成功,出售方' . $order['uid']);
  498. break;
  499. case 2:
  500. if ($order['trade_uid'] != $request->uid()) return app('json')->fail('订单审核方错误');
  501. $res = $res && UserMoney::incomeMoney($order['uid'], $order['money_type'], $order['money_num'], '购币成功', 'buy_success', '购币成功,出售方' . $order['trade_uid']);
  502. break;
  503. default:
  504. break;
  505. }
  506. $res = $res && CashTradeOrder::where('id', $id)->update(['status' => 1, 'trade_time' => time()]);
  507. BaseModel::checkTrans($res);
  508. if ($res) {
  509. return app('json')->fail('审核成功');
  510. } else {
  511. return app('json')->fail('审核失败');
  512. }
  513. } else {
  514. return app('json')->fail('请联系后台审核');
  515. }
  516. }
  517. }