TradeController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Http\Controllers\Oapi;
  3. use App\Http\Validator\TradeValidator;
  4. use App\Services\ConfigService;
  5. use App\Services\Oapi\TradeOrderService;
  6. /**
  7. * 交易
  8. * Class TradeController
  9. * @package App\Http\Controllers\Oapi
  10. */
  11. class TradeController extends webApp
  12. {
  13. /**
  14. * 构造函数
  15. * @author laravel开发员
  16. * @since 2020/11/11
  17. * NoticeController constructor.
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->service = new TradeOrderService();
  23. }
  24. /**
  25. * 买入
  26. * @param TradeValidator $validate
  27. * @return array
  28. */
  29. public function buy(TradeValidator $validate)
  30. {
  31. $params = request()->post();
  32. $params = $validate->check($params,'trade_buy');
  33. if(!is_array($params)){
  34. return message($params, false);
  35. }
  36. // 额外验证没注册需要前往下载APP注册
  37. $tradeType = isset($params['trade_type']) ? intval($params['trade_type']) : 0;
  38. if($tradeType == 3 && (empty($this->userId) || $this->userInfo['idcard_check'] != 1)){
  39. $appUrl = ConfigService::make()->getConfigByCode('app_download_url');
  40. return message(2029, true, [
  41. 'app_url' => $appUrl,
  42. ]);
  43. }
  44. $params['api_id'] = $this->apiId;
  45. if($info = TradeOrderService::make()->buy($this->userId, $params)){
  46. return message(3006, true, $info);
  47. }else{
  48. return message(TradeOrderService::make()->getError(), false);
  49. }
  50. }
  51. /**
  52. * 出售
  53. * @param TradeValidator $validate
  54. * @return array
  55. */
  56. public function sell(TradeValidator $validate)
  57. {
  58. $params = request()->post();
  59. $params = $validate->check($params,'trade_sell');
  60. if(!is_array($params)){
  61. return message($params, false);
  62. }
  63. // 需要注册认证
  64. $tradeType = isset($params['trade_type']) ? intval($params['trade_type']) : 0;
  65. if($tradeType == 3 && (empty($this->userId) || $this->userInfo['idcard_check'] != 1)){
  66. $appUrl = ConfigService::make()->getConfigByCode('app_download_url');
  67. return message(2029, true, [
  68. 'app_url' => $appUrl,
  69. ]);
  70. }
  71. $params['api_id'] = $this->apiId;
  72. if($info = TradeOrderService::make()->sell($this->userId, $params)){
  73. return message(3007, true, $info);
  74. }else{
  75. return message(TradeOrderService::make()->getError(), false);
  76. }
  77. }
  78. /**
  79. * 确认打款
  80. * @param TradeValidator $validate
  81. * @return array
  82. */
  83. public function pay(TradeValidator $validate)
  84. {
  85. $params = request()->post();
  86. $params = $validate->check($params,'pay');
  87. if(!is_array($params)){
  88. return message($params, false);
  89. }
  90. if($info = TradeOrderService::make()->pay($this->userId, $params)){
  91. return message(3017, true, $info);
  92. }else{
  93. return message(TradeOrderService::make()->getError(), false);
  94. }
  95. }
  96. /**
  97. * 确认收款
  98. * @param TradeValidator $validate
  99. * @return array
  100. */
  101. public function collection(TradeValidator $validate)
  102. {
  103. $params = request()->post();
  104. $params = $validate->check($params,'info');
  105. if(!is_array($params)){
  106. return message($params, false);
  107. }
  108. if($info = TradeOrderService::make()->collection($this->userId, $params)){
  109. return message(3021, true, $info);
  110. }else{
  111. return message(TradeOrderService::make()->getError(), false);
  112. }
  113. }
  114. /**
  115. * 取消订单
  116. * @param TradeValidator $validate
  117. * @return array
  118. */
  119. public function cancel(TradeValidator $validate)
  120. {
  121. $params = request()->post();
  122. $params = $validate->check($params,'info');
  123. if(!is_array($params)){
  124. return message($params, false);
  125. }
  126. if($info = TradeOrderService::make()->cancel($this->userId, $params)){
  127. return message(3034, true, $info);
  128. }else{
  129. return message(TradeOrderService::make()->getError(), false);
  130. }
  131. }
  132. /**
  133. * 订单信息
  134. * @return array
  135. */
  136. public function info()
  137. {
  138. $orderNo = request()->post('order_no','');
  139. if(empty($orderNo)){
  140. return message(1013,false);
  141. }
  142. $info = $this->service->getInfoByNo($orderNo, $this->userId);
  143. $tradeType = isset($info['trade_type'])? $info['trade_type'] : 0;
  144. $userId = isset($info['user_id'])? $info['user_id'] : 0;
  145. if($tradeType == 3 && ($this->userId<=0 || $userId != $this->userId)){
  146. return message(6012, false);
  147. }
  148. if($info){
  149. return message(1002, true, $info);
  150. }else{
  151. return message(1002, false);
  152. }
  153. }
  154. }