OrderController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\member;
  3. use Yii;
  4. use yii\web\NotFoundHttpException;
  5. use yii\data\ActiveDataProvider;
  6. use api\controllers\UserAuthController;
  7. use common\helpers\BcHelper;
  8. use common\enums\PayTypeEnum;
  9. use common\enums\StatusEnum;
  10. use common\helpers\ResultHelper;
  11. use addons\TinyShop\common\forms\OrderSearchForm;
  12. use addons\TinyShop\common\models\order\Order;
  13. use addons\TinyShop\common\enums\OrderStatusEnum;
  14. use addons\TinyShop\common\enums\ShippingTypeEnum;
  15. /**
  16. * Class OrderController
  17. * @package addons\TinyShop\api\modules\v1\controllers\member
  18. * @author jianyan74 <751393839@qq.com>
  19. */
  20. class OrderController extends UserAuthController
  21. {
  22. /**
  23. * @var Order
  24. */
  25. public $modelClass = Order::class;
  26. /**
  27. * 首页
  28. *
  29. * @return array|ActiveDataProvider|\yii\db\ActiveRecord[]
  30. */
  31. public function actionIndex()
  32. {
  33. $model = new OrderSearchForm();
  34. $model->attributes = Yii::$app->request->get();
  35. $model->member_id = Yii::$app->user->identity->member_id;
  36. return Yii::$app->tinyShopService->order->query($model);
  37. }
  38. /**
  39. * 单个显示
  40. *
  41. * @param $id
  42. * @return \yii\db\ActiveRecord
  43. * @throws NotFoundHttpException
  44. */
  45. public function actionView($id)
  46. {
  47. $with = ['product', 'coupon', 'baseMerchant', 'marketingDetail'];
  48. // 简单的查询订单基本信息
  49. if ($simplify = Yii::$app->request->get('simplify')) {
  50. $with = [];
  51. }
  52. $model = $this->modelClass::find()->where([
  53. 'id' => $id,
  54. 'status' => StatusEnum::ENABLED,
  55. 'buyer_id' => Yii::$app->user->identity->member_id,
  56. ])
  57. ->with($with)
  58. ->asArray()
  59. ->one();
  60. if (!$model) {
  61. throw new NotFoundHttpException('找不到订单信息');
  62. }
  63. // 自提地点
  64. $model['store'] = $model['shipping_type'] == ShippingTypeEnum::PICKUP ? Yii::$app->tinyShopService->orderStore->findById($id) : [];
  65. // 合并营销显示
  66. $model['marketingDetail'] = Yii::$app->tinyShopService->marketing->mergeIdenticalMarketing($model['marketingDetail'] ?? []);
  67. // 支付类型、配送方式
  68. $model['pay_explain'] = PayTypeEnum::getValue($model['pay_type']);
  69. $model['shipping_explain'] = ShippingTypeEnum::getValue($model['shipping_type']);
  70. // 好友代付(未支付的情况)
  71. $model['peer_pay'] = [];
  72. // 调价
  73. $model['adjust_money'] = 0;
  74. if (!empty($model['product'])) {
  75. foreach ($model['product'] as $value) {
  76. // 调整金额
  77. $model['adjust_money'] = BcHelper::add($model['adjust_money'], $value['adjust_money']);
  78. }
  79. }
  80. $setting = Yii::$app->tinyShopService->config->setting();
  81. $model['order_invoice_status'] = $setting->order_invoice_status;
  82. return $model;
  83. }
  84. /**
  85. * 关闭订单
  86. *
  87. * @param $id
  88. * @throws NotFoundHttpException
  89. * @throws \yii\web\UnprocessableEntityHttpException
  90. */
  91. public function actionClose($id)
  92. {
  93. $member_id = Yii::$app->user->identity->member_id;
  94. // 记录操作
  95. Yii::$app->services->actionLog->create('order', '用户关闭订单', $id);
  96. return Yii::$app->tinyShopService->order->close($id, $member_id);
  97. }
  98. /**
  99. * 删除订单
  100. *
  101. * @param $id
  102. * @throws NotFoundHttpException
  103. * @throws \yii\web\UnprocessableEntityHttpException
  104. */
  105. public function actionDelete($id)
  106. {
  107. $model = $this->findModel($id);
  108. // 非关闭订单不可删除
  109. if ($model->order_status != OrderStatusEnum::REPEAL) {
  110. return ResultHelper::json(422, "删除失败");
  111. }
  112. $model->status = StatusEnum::DELETE;
  113. if ($model->save()) {
  114. // 记录操作
  115. Yii::$app->services->actionLog->create('order', '删除订单', $model->id);
  116. return true;
  117. }
  118. return ResultHelper::json(422, "删除失败");
  119. }
  120. /**
  121. * 确认收货
  122. *
  123. * @param $id
  124. * @throws NotFoundHttpException
  125. * @throws \yii\web\UnprocessableEntityHttpException
  126. */
  127. public function actionTakeDelivery($id)
  128. {
  129. $member_id = Yii::$app->user->identity->member_id;
  130. $data = Yii::$app->tinyShopService->order->takeDelivery($id, $member_id);
  131. return $data;
  132. }
  133. /**
  134. * @param $id
  135. * @return \yii\db\ActiveRecord
  136. * @throws NotFoundHttpException
  137. */
  138. protected function findModel($id)
  139. {
  140. /* @var $model \yii\db\ActiveRecord */
  141. if (empty($id) || !($model = $this->modelClass::find()->where([
  142. 'id' => $id,
  143. 'status' => StatusEnum::ENABLED,
  144. 'buyer_id' => Yii::$app->user->identity->member_id
  145. ])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
  146. throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
  147. }
  148. return $model;
  149. }
  150. /**
  151. * 权限验证
  152. *
  153. * @param string $action 当前的方法
  154. * @param null $model 当前的模型类
  155. * @param array $params $_GET变量
  156. * @throws \yii\web\BadRequestHttpException
  157. */
  158. public function checkAccess($action, $model = null, $params = [])
  159. {
  160. // 方法名称
  161. if (in_array($action, ['update', 'create'])) {
  162. throw new \yii\web\BadRequestHttpException('权限不足');
  163. }
  164. }
  165. }
粤ICP备19079148号