OrderController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wniu
  5. * Date: 2022/3/16
  6. * Time: 3:19 下午
  7. */
  8. namespace addons\Course\api\modules\v1\controllers;
  9. use addons\Course\common\enums\CourseEnum;
  10. use addons\Course\common\logic\Notify;
  11. use addons\Course\common\models\AddonsCourse;
  12. use addons\Course\common\models\AddonsCourseBought;
  13. use addons\Course\common\models\AddonsCourseOrders;
  14. use api\controllers\OnAuthController;
  15. use common\enums\AddonsEnum;
  16. use common\enums\PaymentTradeOrderEnum;
  17. use common\enums\PayTypeEnum;
  18. use common\enums\StatusEnum;
  19. use common\models\order\Order;
  20. use common\models\order\OrderDetail;
  21. class OrderController extends OnAuthController
  22. {
  23. public $modelClass = '';
  24. /**
  25. * 不用进行登录验证的方法
  26. *
  27. * 例如: ['index', 'update', 'create', 'view', 'delete']
  28. * 默认全部需要验证
  29. *
  30. * @var array
  31. */
  32. protected $authOptional = [];
  33. /**
  34. * 预览订单
  35. * requestData 数据结构 array ['list' =>[{"id":"课程ID","num":"购买的课程数量"}],'其他key'=>"其他Value" ]
  36. * User: wniu
  37. * Date: 2022/3/18
  38. * Time: 3:21 下午
  39. *
  40. */
  41. public function actionToSubmitOrder()
  42. {
  43. $post = \Yii::$app->request->post();
  44. $res = \Yii::$app->services->validate->validate($post, [
  45. [['list'], 'required'],
  46. ]);
  47. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  48. //todo 测试数据
  49. // $post['list'] = [[
  50. // 'id' => 2,
  51. // 'num' => 1
  52. // ]];
  53. $data = AddonsCourseOrders::preview($this->user, $this->mall_id, $post);
  54. return $this->success('success', $data);
  55. }
  56. /**
  57. * 提交订单
  58. * requestData 数据结构 array ['list' =>[{"id":"课程ID","num":"购买的课程数量"}],'其他key'=>"其他Value" ]
  59. * User: wniu
  60. * Date: 2022/3/18
  61. * Time: 3:31 下午
  62. *
  63. */
  64. public function actionDoSubmitOrder()
  65. {
  66. $post = \Yii::$app->request->post();
  67. $res = \Yii::$app->services->validate->validate($post, [
  68. [['list'], 'required'],
  69. ]);
  70. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  71. // $post['list'] = [[
  72. // 'id' => 2,
  73. // 'num' => 1
  74. // ]];
  75. $res = AddonsCourseOrders::createOrder($this->user, $this->mall_id, $post);
  76. if ($res === false) return $this->error(AddonsCourseOrders::getStaticError());
  77. return $this->success('操作成功', $res);
  78. }
  79. public function actionSuccess()
  80. {
  81. $get = \Yii::$app->request->get();
  82. $res = \Yii::$app->services->validate->validate($get, [
  83. [['orderno'], 'required'],
  84. ]);
  85. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  86. $order = AddonsCourseOrders::getOne([['order_no' => $get['orderno']]]);
  87. if (empty($order)) {
  88. throw new \Exception("订单不存在");
  89. }
  90. $data['orderno'] = $order->order_no;
  91. $data['status'] = $order->order_status;
  92. $data['pay_type'] = $order->pay_type;
  93. $config = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, 'basic');
  94. unset($config['index_link']);
  95. $data['pay_sucess_conf'] = $config;
  96. return $this->success('success', $data);
  97. }
  98. /**
  99. * 查看课程订单详情
  100. * @Author guyu
  101. * @DateTime 2021-5-21
  102. * @return string
  103. * @copyright Copyright
  104. */
  105. public function actionDetail()
  106. {
  107. $get = \Yii::$app->request->get();
  108. $res = \Yii::$app->services->validate->validate($get, [
  109. [['bought_id'], 'required'],
  110. ]);
  111. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  112. $course_bought = AddonsCourseBought::getOne([['id' => $get['bought_id']]]);
  113. if (!empty($course_bought)) {
  114. $main_order_detail = [];
  115. if (1 == $course_bought->buy_type) {
  116. // 单独购买课程
  117. $course_order = AddonsCourseOrders::getOne([['id' => $course_bought->order_id, 'mall_id' => $course_bought->mall_id]]);
  118. } elseif (2 == $course_bought->buy_type) {
  119. // 购买商品附赠课程
  120. $order = Order::getOne([['id' => $course_bought->order_id, 'mall_id' => $course_bought->mall_id]]);
  121. $course_order = AddonsCourseOrders::getOne([['course_id' => $course_bought->course_id, 'mall_id' => $course_bought->mall_id, 'order_no' => $order->order_no]]);
  122. //课程购买类型为商品赠送时,获取主订单详情
  123. $main_order_detail = OrderDetail::find()
  124. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'order_id' => $course_bought->order_id])
  125. ->asArray()
  126. ->all();
  127. }
  128. $course_info = AddonsCourse::getOne([['id' => $course_bought->course_id, 'mall_id' => $course_bought->mall_id]]);
  129. $payment_type_list = PayTypeEnum::getMap();
  130. $order_detail = [
  131. 'id' => $course_bought->id,
  132. 'course_id' => $course_bought->course_id,
  133. 'orderno' => $course_order->order_no,
  134. 'real_pay' => $course_order->pay_money,
  135. 'pay_type' => $payment_type_list[$course_order->pay_type],
  136. 'created_at' => $course_order->created_at,
  137. 'pay_time' => $course_order->pay_time,
  138. 'course_name' => $course_info->name,
  139. 'introduce' => $course_info->introduce,
  140. 'logo' => $course_info->logo,
  141. 'buy_type' => $course_bought->buy_type,
  142. 'main_order_detail' => $main_order_detail,
  143. ];
  144. } else {
  145. $order_detail = [];
  146. }
  147. return $this->success('success', $order_detail);
  148. }
  149. }