OrderController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace addons\Happiness\api\modules\v1\controllers;
  3. use addons\Community\common\models\CommunityGoods;
  4. use Yii;
  5. use Exception;
  6. use api\controllers\OnAuthController;
  7. use common\enums\AddonsEnum;
  8. use common\enums\AppEnum;
  9. use common\enums\ShippingTypeEnum;
  10. use common\enums\StatusEnum;
  11. use addons\Happiness\common\logic\order\OrderFormLogic;
  12. use addons\Happiness\common\service\OrderService;
  13. use common\models\mall\MallAddons;
  14. use services\common\SettingService;
  15. /**
  16. * 幸福小店门店
  17. *
  18. * Class DefaultController
  19. * @package addons\Store\api\modules\v1\controllers
  20. */
  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 = ['index', 'detail', 'cate-goods-list', 'list', 'recommend', 'comments-list', 'get-platform-goods'];
  33. /**
  34. * 订单预览
  35. * @Author bing
  36. * @DateTime 2021-09-24 19:46:46
  37. * @return void
  38. * @copyright: Copyright (c) 2020 广东七件事集团
  39. */
  40. public function actionPreview()
  41. {
  42. try {
  43. $orderFormLogic = new OrderFormLogic();
  44. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  45. $form->attributes = Yii::$app->request->post();
  46. $form['action'] = AppEnum::ACTION_PREVIEW;
  47. $info = (new OrderService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->preview($form);
  48. return $this->success('操作成功', $info);
  49. }catch (\Exception $e){
  50. return $this->error($e->getMessage());
  51. }
  52. }
  53. /**
  54. * 是否强制免运费
  55. * @param $form
  56. * @return mixed
  57. */
  58. protected function isForceFreeShipping($form)
  59. {
  60. // 社区团购 是否强制免运费
  61. if (!$form->is_force_free_shipping && MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COMMUNITY) &&
  62. (new SettingService())->isForceFreeShipping($this->mall_id)) { // 需要处理
  63. // 判断是否为社区团购订单
  64. // 这种判断仅限于定制创建的时候
  65. if ($form->action == AppEnum::ACTION_CREATE && in_array($form->shipping_type,
  66. [ShippingTypeEnum::PICKUP, ShippingTypeEnum::MERCHANT_DISTRIBUTION]) && !empty($form->extra_info)) {
  67. $ext = json_decode($form->extra_info, true);
  68. if (empty($ext['head_id'])) {
  69. return $form;
  70. }
  71. }
  72. // 如果有安装社区团购 并且
  73. $community_res = (new CommunityGoods())->orderPreviewAppendFields(['mall_id' => $this->mall_id, 'user_id' => $this->user_id,
  74. 'data' => [], 'params' => array_merge($form->data, ['type' => $form->type])]);
  75. $shipping_type = null;
  76. if (!empty($community_res['option_list']) && is_array($community_res['option_list'])) {
  77. $option_list = array_values(array_filter($community_res['option_list'], function ($val) {
  78. return $val['enable'] == StatusEnum::ENABLED;
  79. }));
  80. $shipping_type = $option_list[0]['key'] ?? null;
  81. }
  82. $shipping_type = $form->data['shipping_type'] ?? $shipping_type; // 如果前端没有传,那么就取默认第一个
  83. $form->is_force_free_shipping = in_array($shipping_type, [ShippingTypeEnum::PICKUP]);
  84. }
  85. return $form;
  86. }
  87. /**
  88. * 创建订单
  89. * @Author bing
  90. * @DateTime 2021-09-22 09:31:22
  91. * @return void
  92. * @copyright: Copyright (c) 2020 广东七件事集团
  93. */
  94. public function actionCreate()
  95. {
  96. try {
  97. $orderFormLogic = new OrderFormLogic();
  98. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  99. $form->attributes = Yii::$app->request->post();
  100. $form['action'] = AppEnum::ACTION_CREATE;
  101. // 初始化订单数据
  102. $res = (new OrderService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->created($form);
  103. return $this->success('操作成功', $res);
  104. }catch (\Exception $e){
  105. return $this->error($e->getMessage());
  106. }
  107. }
  108. }