CashTradeController.php 22 KB

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