OrderController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace addons\ValetOrder\api\modules\v1\controllers;
  3. use addons\CloudStock\common\logic\order\RestrictPurchases;
  4. use addons\ValetOrder\common\enums\ValetOrderEnum;
  5. use addons\ValetOrder\common\models\ValetOrder;
  6. use addons\ValetOrder\common\models\ValetOrderGoods;
  7. use addons\ValetOrder\common\service\OrderService;
  8. use addons\ValetOrder\common\service\SettingService;
  9. use api\controllers\OnAuthController;
  10. use common\enums\AddonsEnum;
  11. use common\enums\ApiStatusEnum;
  12. use common\enums\AppEnum;
  13. use common\enums\ShippingTypeEnum;
  14. use common\enums\StatusEnum;
  15. use common\globals\GlobalInvoke;
  16. use common\helpers\sms\Sms;
  17. use common\models\order\Order;
  18. use common\logic\order\OrderFormLogic;
  19. use common\logic\order\OrderInitDataLogic;
  20. use common\logic\order\OrderLimitLogic;
  21. use common\logic\order\OrderValidateLogic;
  22. use common\models\goods\GoodsAttr;
  23. use common\models\mall\MallAddons;
  24. use common\models\user\User;
  25. use common\models\user\UserAddress;
  26. use common\models\user\UserPartitionRelationship;
  27. use InvalidArgumentException;
  28. use Yii;
  29. class OrderController extends OnAuthController
  30. {
  31. public $modelClass = '';
  32. /**
  33. * 不用进行登录验证的方法
  34. *
  35. * 例如: ['index', 'update', 'create', 'view', 'delete']
  36. * 默认全部需要验证
  37. *
  38. * @var array
  39. */
  40. protected $authOptional = ['index'];
  41. /**
  42. * 订单列表
  43. * @return mixed|null
  44. */
  45. public function actionIndex()
  46. {
  47. $data = \Yii::$app->request->get();
  48. $valid = Yii::$app->services->validate->validate($data, [
  49. [['page', 'limit', 'order_status'], 'integer'],
  50. [['keyword'], 'string'],
  51. ]);
  52. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  53. $ret = (new OrderService())->frontPage($this->mall_id, $this->user_id, $data, $this->mall,
  54. empty($this->platform) ? 'h5' : $this->platform);
  55. return $this->success('操作成功', $ret);
  56. }
  57. /**
  58. * @return mixed|null
  59. * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
  60. * @throws InvalidArgumentException
  61. */
  62. public function actionSms()
  63. {
  64. $params = Yii::$app->request->post();
  65. $res = Yii::$app->services->validate->validate($params, [
  66. [['mobile'], 'required', 'message' => '请输入手机号码'],
  67. [['mobile','area_code'], 'integer'],
  68. ]);
  69. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  70. // 判断是否需要强制发送短信
  71. $is_enable_sms_code = \Yii::$app->services->setting->addons->get($this->mall_id, ValetOrderEnum::ADDONS_NAME, 'basic.is_enable_sms_code');
  72. if (empty($is_enable_sms_code)) return $this->error('不需要短信验证码');
  73. $area_code = '';// 初始化区号
  74. $gateways = 'domestic';// 默认国内短信
  75. // 检测是登录并且区号不为空则使用国际短信
  76. if (!empty($this->user) && !empty($this->user->area_code) && $this->user->area_code!=86) {
  77. $gateways = 'international';
  78. $area_code = $this->user->area_code;
  79. }
  80. // 判断是否有区号
  81. if (!empty($params['area_code']) && $params['area_code'] != 86) {
  82. $gateways = 'international';
  83. $area_code = $params['area_code'];
  84. }
  85. $sms = new Sms($this->mall_id, $gateways);
  86. try {
  87. if ($sms->sendCaptcha($params['mobile'], $area_code, ValetOrderEnum::SMS_TYPE) === false) return $this->error('发送失败');
  88. }catch (\Exception $e){
  89. return $this->error($e->getMessage());
  90. }
  91. return $this->success('发送成功');
  92. }
  93. /**
  94. * 代客下单去支付
  95. *
  96. * @return mixed|null
  97. */
  98. public function actionToPay()
  99. {
  100. $params = \Yii::$app->request->get();
  101. $res = \Yii::$app->services->validate->validate($params, [
  102. [['id'], 'required'],
  103. ]);
  104. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  105. // 查询出订单所属的userid
  106. $orderId = json_decode($params['id'], true);
  107. $orderUserId = ValetOrder::find()->select('user_id')->where(['order_id' => reset($orderId)])->scalar();
  108. if (!$orderUserId) {
  109. return $this->error('查无订单');
  110. }
  111. $params['mall_id'] = $this->mall_id;
  112. $params['user_id'] = $this->user_id;
  113. $params['order_user_id'] = $orderUserId;
  114. $res = Order::toPay($params);
  115. if ($res === false) return $this->error(Order::getStaticError());
  116. return $this->success('操作成功', $res);
  117. }
  118. /**
  119. * 代客下单订单预览
  120. * @return mixed|null
  121. */
  122. public function actionPreview()
  123. {
  124. try {
  125. $orderFormLogic = new OrderFormLogic();
  126. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  127. $form->attributes = Yii::$app->request->post();
  128. $form['action'] = AppEnum::ACTION_PREVIEW;
  129. !empty($form->shipping_type) && $form->shipping_type = ShippingTypeEnum::LOGISTICS;
  130. // 初始化订单数据
  131. $orderInitDataLogic = new OrderInitDataLogic();
  132. $form = $orderInitDataLogic->execute($form, $form->type);
  133. if ($form === false) return $this->error($orderInitDataLogic->getError());
  134. $is_bind_popups = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id);
  135. if($is_bind_popups == 1) return $this->error('请进入商品详情页,绑定邀请码!');
  136. // 购买限制验证
  137. $orderLimitLogic = new OrderLimitLogic();
  138. $form = $orderLimitLogic->run($form);
  139. if ($form === false) return $this->error($orderLimitLogic->getError());
  140. // 获取收货地址
  141. $address_info = UserAddress::getUserAddress($this->user_id, $form->address_id);
  142. $form->address_id = $address_info['id']??0;
  143. // 营销减免活动
  144. $orderService = new OrderService();
  145. $form = $orderService->orderMarketing($form);
  146. $form = $this->jsTanCheck($form);
  147. $group_order_goods = $form['group_order_goods'];
  148. $group_order_goods_list = [];
  149. $gIds = [];
  150. foreach ($group_order_goods as $val){
  151. $group_order_goods_list[]=$val;
  152. foreach ($val['goods'] as $gId => $item) {
  153. $gIds[] = $gId;
  154. }
  155. }
  156. $marketing = $form['marketing']??[];
  157. $pay_type_list = \Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform);
  158. if($form['total_pay_money']<=0){
  159. $tmp = [];
  160. foreach ($pay_type_list as $item){
  161. if($item['trade_type']=='default'){
  162. $tmp[]=$item;
  163. break;
  164. }
  165. }
  166. $pay_type_list = $tmp;
  167. }
  168. $shipping_type_list = ShippingTypeEnum::getMap();
  169. $option_list = [];
  170. $group_order_goods = $group_order_goods_list;
  171. $extends_fields = [];
  172. $info = compact('group_order_goods', 'marketing', 'address_info', 'pay_type_list', 'shipping_type_list', 'option_list', 'extends_fields');
  173. if($form['total_pay_money'] < 0) $form['total_pay_money'] = 0;
  174. $info['total_pay_money'] = '¥' . $form['total_pay_money'];
  175. $info['total_pay_money_not_coupon'] = $form['total_pay_money_not_coupon'];
  176. $info['is_address'] = $form['is_address'] ?? 0;
  177. $info['is_virtual'] = $form['is_virtual'] ?? 0;
  178. $info['is_sign_agreement'] = $form['is_sign_agreement'] ?? 0;
  179. $info['shipping_type'] = $form->data['shipping_type'] ?? null;
  180. // 农业补贴 - 是否需要显示推荐人输入框
  181. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES)) {
  182. $aConfig = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES, 'basic');
  183. if($aConfig && isset($aConfig['goods'])){
  184. $tGoods = json_decode($aConfig['goods'], true);
  185. $tGids = array_column($tGoods, 'goods_id');
  186. $info['show_invite_form'] = StatusEnum::DISABLED;
  187. if ($aConfig['enable_bind_master'] == StatusEnum::ENABLED && !empty(array_intersect($tGids, $gIds))) {
  188. $info['show_invite_form'] = StatusEnum::ENABLED;
  189. }
  190. }
  191. // 判断是否包含当前商品
  192. // $info['show_invite_form'] = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES, 'basic.enable_bind_master');
  193. }
  194. $form->data['type'] = $form->type;
  195. $invoke_res = GlobalInvoke::exec(GlobalInvoke::ORDER_PREVIEW_APPEND_FILEDS, $this->mall_id, ['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'data' => $info, 'params' => $form->data]);
  196. $info = !empty($invoke_res) ? $invoke_res : $info;
  197. return $this->success('操作成功', $info);
  198. }catch (\Exception $e){
  199. return $this->error($e->getMessage());
  200. }
  201. }
  202. /**
  203. * 创建订单
  204. * @return mixed|null
  205. */
  206. public function actionCreate()
  207. {
  208. try {
  209. $orderFormLogic = new OrderFormLogic();
  210. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  211. $form->attributes = Yii::$app->request->post();
  212. $form['action'] = AppEnum::ACTION_CREATE;
  213. // 初始化订单数据
  214. $orderInitDataLogic = new OrderInitDataLogic();
  215. $form = $orderInitDataLogic->execute($form, $form->type);
  216. if ($form === false) return $this->error($orderInitDataLogic->getError());
  217. $is_bind_popups = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id);
  218. if($is_bind_popups == 1) return $this->error('请进入商品详情页,绑定邀请码!');
  219. // 购买限制验证
  220. $orderLimitLogic = new OrderLimitLogic();
  221. $form = $orderLimitLogic->run($form);
  222. if ($form === false) return $this->error($orderLimitLogic->getError());
  223. // 必须安装了插件,必须开启了插件,下单必须传了参数
  224. if (!MallAddons::isInstall($form->mall_id, AddonsEnum::ADDONS_NAME_VALET_ORDER)) {
  225. return $this->error('非法请求');
  226. }
  227. $config = \Yii::$app->services->setting->addons->get($form->mall_id, AddonsEnum::ADDONS_NAME_VALET_ORDER, 'basic');
  228. if (empty($config['is_enable']) || empty($form->attributes['data']['extra_info']['valet_order']['mobile'])) {
  229. return $this->error('非法请求');
  230. }
  231. // 判断商品是否开启代客下单
  232. $goodsIdFind = GoodsAttr::find()->select('goods_id');
  233. GoodsAttr::paramPack(['where' => [['<>', 'status', StatusEnum::DELETE], ['id' => $form->data['attr_id']]]], $goodsIdFind);
  234. if (!ValetOrderGoods::find()->where(['goods_id' => $goodsIdFind->scalar(), 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall])->count()) {
  235. return $this->error('非法请求');
  236. }
  237. // 数据校验
  238. $flag = OrderValidateLogic::run($form);
  239. if (is_string($flag)) return $this->error($flag);
  240. // 营销减免活动
  241. $orderService = new OrderService();
  242. $form = $orderService->orderMarketing($form);
  243. $form = $this->jsTanCheck($form);
  244. $goods_ids = array_column($form['order_goods'],'goods_id');
  245. if(MallAddons::isInstall($this->mall_id,'CloudStock')){
  246. (new RestrictPurchases())->orderCreate(['user_id' => $this->user_id,'mall_id' => $this->mall_id,'type' => 'order','goods_ids'=>$goods_ids]);
  247. }
  248. $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH, $this->mall_id, ['user_id' => $this->user_id,'mall_id' => $this->mall_id,'type' => 'order','goods_ids'=>$goods_ids]);
  249. if($need_auth === true){
  250. //需要先进行实名认证
  251. return $this->error('请先进行实名认证',[],ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
  252. }
  253. // print_r($form);exit;
  254. $res = Order::createOrder($form);
  255. if ($res === false) return $this->error(Order::getStaticError());
  256. return $this->success('操作成功', $res);
  257. }catch (\Exception $e){
  258. return $this->error($e->getMessage());
  259. }
  260. }
  261. /**
  262. * 聚水潭处理
  263. *
  264. * @param $form
  265. */
  266. protected function jsTanCheck($form)
  267. {
  268. if (
  269. MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_JSTAN_ERP) &&
  270. !empty($form->data['marketing_type']) &&
  271. $form->data['marketing_type'] === AddonsEnum::ADDONS_NAME_JSTAN_ERP
  272. ) {
  273. $service = new \addons\JsTanErp\common\service\OrderService(['mall_id' => $this->mall_id]);
  274. $form = $service->createBeforeCheck($form);
  275. }
  276. return $form;
  277. }
  278. }