Order.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\OrderService;
  4. use app\api\validate\CreateOrderValidate;
  5. use app\exception\BaseException;
  6. use think\App;
  7. class Order extends \app\BaseController
  8. {
  9. private $service;
  10. public function __construct(App $app)
  11. {
  12. $this->service = new OrderService();
  13. parent::__construct($app);
  14. }
  15. /**
  16. * @return \think\response\Json
  17. * @throws BaseException
  18. */
  19. public function create() {
  20. $params = (new CreateOrderValidate())->message([
  21. 'store_id.require' => lang("Stores do not exist"),
  22. 'customer_id.require' => lang("Customer do not exist"),
  23. 'products.require' => lang("Products do not exist"),
  24. ])->requestBodyCheck($this->request);
  25. $products = json_decode($params['products']);
  26. predicate($products != null, 'products 解析错误');
  27. for ($i = 0; $i < count($products); $i++) {
  28. $product = $products[$i];
  29. predicate(isset($product->store_product_id) && $product->store_product_id > 0, '商品ID错误!');
  30. predicate($product->quantity > 0, '商品数量错误!');
  31. predicate($product->adviser_1_id > 0, '销售顾问1必须存在!');
  32. if(isset($product->adviser_2_id)) predicate($product->adviser_2_id > 0, '销售顾问2不符合规则!');
  33. if(isset($product->teacher_id)) predicate($product->teacher_id > 0, '老师不符合规则!');
  34. }
  35. $res = $this->service->create($params, $products);
  36. predicate($res->bool,$res->message);
  37. return $this->ok($res->data);
  38. }
  39. /**
  40. * @return \think\response\Json
  41. * @throws BaseException
  42. */
  43. public function payment() {
  44. $params = $this->request->param();
  45. if(!isset($params['order_id'])) {
  46. return $this->fail("订单ID不存在!");
  47. }
  48. if(!isset($params['channels'])) {
  49. return $this->fail("支付记录不存在!");
  50. }
  51. $channels = json_decode($params['channels']);
  52. predicate($channels != null, 'channels 解析错误');
  53. for ($i = 0; $i < count($channels); $i++) {
  54. $channel = $channels[$i];
  55. predicate(isset($channel->channel_id) && $channel->channel_id > 0, '支付渠道错误!');
  56. if (isset($channel->fee)) {
  57. predicate($channel->fee > 0, '支付费用错误!');
  58. }
  59. if (isset($channel->credit_card)) {
  60. predicate(is_array($channel->credit_card), 'credit_card 不符合规则!');
  61. for ($j = 0; $j < count($channel->credit_card); $j++) {
  62. $credit_card = $channel->credit_card[$j];
  63. predicate(isset($credit_card->credit_card_id) && $credit_card->credit_card_id > 0, '信用卡配置id错误!');
  64. predicate($credit_card->fee > 0, '支付费用错误!');
  65. if (isset($credit_card->stage_num)) predicate(in_array($credit_card->stage_num,[6, 9, 12, 24, 36]), '分期数不符合规则!');
  66. }
  67. }
  68. }
  69. $res = $this->service->payment($params, $channels);
  70. predicate($res->bool,$res->message);
  71. return $this->ok($res->data);
  72. }
  73. }