| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- namespace addons\ValetOrder\api\modules\v1\controllers;
- use addons\CloudStock\common\logic\order\RestrictPurchases;
- use addons\ValetOrder\common\enums\ValetOrderEnum;
- use addons\ValetOrder\common\models\ValetOrder;
- use addons\ValetOrder\common\models\ValetOrderGoods;
- use addons\ValetOrder\common\service\OrderService;
- use addons\ValetOrder\common\service\SettingService;
- use api\controllers\OnAuthController;
- use common\enums\AddonsEnum;
- use common\enums\ApiStatusEnum;
- use common\enums\AppEnum;
- use common\enums\ShippingTypeEnum;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use common\helpers\sms\Sms;
- use common\models\order\Order;
- use common\logic\order\OrderFormLogic;
- use common\logic\order\OrderInitDataLogic;
- use common\logic\order\OrderLimitLogic;
- use common\logic\order\OrderValidateLogic;
- use common\models\goods\GoodsAttr;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserAddress;
- use common\models\user\UserPartitionRelationship;
- use InvalidArgumentException;
- use Yii;
- class OrderController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 订单列表
- * @return mixed|null
- */
- public function actionIndex()
- {
- $data = \Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['page', 'limit', 'order_status'], 'integer'],
- [['keyword'], 'string'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $ret = (new OrderService())->frontPage($this->mall_id, $this->user_id, $data, $this->mall,
- empty($this->platform) ? 'h5' : $this->platform);
- return $this->success('操作成功', $ret);
- }
- /**
- * @return mixed|null
- * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
- * @throws InvalidArgumentException
- */
- public function actionSms()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile'], 'required', 'message' => '请输入手机号码'],
- [['mobile','area_code'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 判断是否需要强制发送短信
- $is_enable_sms_code = \Yii::$app->services->setting->addons->get($this->mall_id, ValetOrderEnum::ADDONS_NAME, 'basic.is_enable_sms_code');
- if (empty($is_enable_sms_code)) return $this->error('不需要短信验证码');
- $area_code = '';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($this->user) && !empty($this->user->area_code) && $this->user->area_code!=86) {
- $gateways = 'international';
- $area_code = $this->user->area_code;
- }
- // 判断是否有区号
- if (!empty($params['area_code']) && $params['area_code'] != 86) {
- $gateways = 'international';
- $area_code = $params['area_code'];
- }
- $sms = new Sms($this->mall_id, $gateways);
- try {
- if ($sms->sendCaptcha($params['mobile'], $area_code, ValetOrderEnum::SMS_TYPE) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
- /**
- * 代客下单去支付
- *
- * @return mixed|null
- */
- public function actionToPay()
- {
- $params = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- // 查询出订单所属的userid
- $orderId = json_decode($params['id'], true);
- $orderUserId = ValetOrder::find()->select('user_id')->where(['order_id' => reset($orderId)])->scalar();
- if (!$orderUserId) {
- return $this->error('查无订单');
- }
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $params['order_user_id'] = $orderUserId;
- $res = Order::toPay($params);
- if ($res === false) return $this->error(Order::getStaticError());
- return $this->success('操作成功', $res);
- }
- /**
- * 代客下单订单预览
- * @return mixed|null
- */
- public function actionPreview()
- {
- try {
- $orderFormLogic = new OrderFormLogic();
- $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
- $form->attributes = Yii::$app->request->post();
- $form['action'] = AppEnum::ACTION_PREVIEW;
- !empty($form->shipping_type) && $form->shipping_type = ShippingTypeEnum::LOGISTICS;
- // 初始化订单数据
- $orderInitDataLogic = new OrderInitDataLogic();
- $form = $orderInitDataLogic->execute($form, $form->type);
- if ($form === false) return $this->error($orderInitDataLogic->getError());
- $is_bind_popups = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id);
- if($is_bind_popups == 1) return $this->error('请进入商品详情页,绑定邀请码!');
- // 购买限制验证
- $orderLimitLogic = new OrderLimitLogic();
- $form = $orderLimitLogic->run($form);
- if ($form === false) return $this->error($orderLimitLogic->getError());
- // 获取收货地址
- $address_info = UserAddress::getUserAddress($this->user_id, $form->address_id);
- $form->address_id = $address_info['id']??0;
- // 营销减免活动
- $orderService = new OrderService();
- $form = $orderService->orderMarketing($form);
- $form = $this->jsTanCheck($form);
- $group_order_goods = $form['group_order_goods'];
- $group_order_goods_list = [];
- $gIds = [];
- foreach ($group_order_goods as $val){
- $group_order_goods_list[]=$val;
- foreach ($val['goods'] as $gId => $item) {
- $gIds[] = $gId;
- }
- }
- $marketing = $form['marketing']??[];
- $pay_type_list = \Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform);
- if($form['total_pay_money']<=0){
- $tmp = [];
- foreach ($pay_type_list as $item){
- if($item['trade_type']=='default'){
- $tmp[]=$item;
- break;
- }
- }
- $pay_type_list = $tmp;
- }
- $shipping_type_list = ShippingTypeEnum::getMap();
- $option_list = [];
- $group_order_goods = $group_order_goods_list;
- $extends_fields = [];
- $info = compact('group_order_goods', 'marketing', 'address_info', 'pay_type_list', 'shipping_type_list', 'option_list', 'extends_fields');
- if($form['total_pay_money'] < 0) $form['total_pay_money'] = 0;
- $info['total_pay_money'] = '¥' . $form['total_pay_money'];
- $info['total_pay_money_not_coupon'] = $form['total_pay_money_not_coupon'];
- $info['is_address'] = $form['is_address'] ?? 0;
- $info['is_virtual'] = $form['is_virtual'] ?? 0;
- $info['is_sign_agreement'] = $form['is_sign_agreement'] ?? 0;
- $info['shipping_type'] = $form->data['shipping_type'] ?? null;
- // 农业补贴 - 是否需要显示推荐人输入框
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES)) {
- $aConfig = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES, 'basic');
- if($aConfig && isset($aConfig['goods'])){
- $tGoods = json_decode($aConfig['goods'], true);
- $tGids = array_column($tGoods, 'goods_id');
- $info['show_invite_form'] = StatusEnum::DISABLED;
- if ($aConfig['enable_bind_master'] == StatusEnum::ENABLED && !empty(array_intersect($tGids, $gIds))) {
- $info['show_invite_form'] = StatusEnum::ENABLED;
- }
- }
- // 判断是否包含当前商品
- // $info['show_invite_form'] = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AGRICULTURAL_SUBSIDIES, 'basic.enable_bind_master');
- }
- $form->data['type'] = $form->type;
- $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]);
- $info = !empty($invoke_res) ? $invoke_res : $info;
- return $this->success('操作成功', $info);
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 创建订单
- * @return mixed|null
- */
- public function actionCreate()
- {
- try {
- $orderFormLogic = new OrderFormLogic();
- $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
- $form->attributes = Yii::$app->request->post();
- $form['action'] = AppEnum::ACTION_CREATE;
- // 初始化订单数据
- $orderInitDataLogic = new OrderInitDataLogic();
- $form = $orderInitDataLogic->execute($form, $form->type);
- if ($form === false) return $this->error($orderInitDataLogic->getError());
- $is_bind_popups = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id);
- if($is_bind_popups == 1) return $this->error('请进入商品详情页,绑定邀请码!');
- // 购买限制验证
- $orderLimitLogic = new OrderLimitLogic();
- $form = $orderLimitLogic->run($form);
- if ($form === false) return $this->error($orderLimitLogic->getError());
- // 必须安装了插件,必须开启了插件,下单必须传了参数
- if (!MallAddons::isInstall($form->mall_id, AddonsEnum::ADDONS_NAME_VALET_ORDER)) {
- return $this->error('非法请求');
- }
- $config = \Yii::$app->services->setting->addons->get($form->mall_id, AddonsEnum::ADDONS_NAME_VALET_ORDER, 'basic');
- if (empty($config['is_enable']) || empty($form->attributes['data']['extra_info']['valet_order']['mobile'])) {
- return $this->error('非法请求');
- }
- // 判断商品是否开启代客下单
- $goodsIdFind = GoodsAttr::find()->select('goods_id');
- GoodsAttr::paramPack(['where' => [['<>', 'status', StatusEnum::DELETE], ['id' => $form->data['attr_id']]]], $goodsIdFind);
- if (!ValetOrderGoods::find()->where(['goods_id' => $goodsIdFind->scalar(), 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall])->count()) {
- return $this->error('非法请求');
- }
- // 数据校验
- $flag = OrderValidateLogic::run($form);
- if (is_string($flag)) return $this->error($flag);
- // 营销减免活动
- $orderService = new OrderService();
- $form = $orderService->orderMarketing($form);
- $form = $this->jsTanCheck($form);
- $goods_ids = array_column($form['order_goods'],'goods_id');
- if(MallAddons::isInstall($this->mall_id,'CloudStock')){
- (new RestrictPurchases())->orderCreate(['user_id' => $this->user_id,'mall_id' => $this->mall_id,'type' => 'order','goods_ids'=>$goods_ids]);
- }
- $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]);
- if($need_auth === true){
- //需要先进行实名认证
- return $this->error('请先进行实名认证',[],ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
- }
- // print_r($form);exit;
- $res = Order::createOrder($form);
- if ($res === false) return $this->error(Order::getStaticError());
- return $this->success('操作成功', $res);
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 聚水潭处理
- *
- * @param $form
- */
- protected function jsTanCheck($form)
- {
- if (
- MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_JSTAN_ERP) &&
- !empty($form->data['marketing_type']) &&
- $form->data['marketing_type'] === AddonsEnum::ADDONS_NAME_JSTAN_ERP
- ) {
- $service = new \addons\JsTanErp\common\service\OrderService(['mall_id' => $this->mall_id]);
- $form = $service->createBeforeCheck($form);
- }
- return $form;
- }
- }
|