| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace common\logic\order\limit;
- use common\enums\AppEnum;
- use common\enums\GoodsTypeEnum;
- use common\enums\PreviewTypeEnum;
- use common\models\user\UserAddress;
- use Exception;
- use yii\helpers\Json;
- class RegionLimit extends BaseLimit
- {
- public function execute( $form)
- {
- try {
- } catch (Exception $e) {
- $this->setError('区域限制验证不通过,原因:' . $e->getMessage());
- return false;
- }
- }
- /**
- * 购买区域限制
- * User: wniu
- * Date: 2021/10/7
- * Time: 3:20 下午
- * * @param $form
- * @throws Exception
- */
- public static function compute($form)
- {
- if ($form->action == AppEnum::ACTION_PREVIEW) { // 预览不做处理
- return $form;
- }
- // 如果是预约类型的订单那么不做判断
- if ($form->type == PreviewTypeEnum::RESERVE_NOW) return $form;
- $district_id = 0;
- $town_id = 0;
- if ($form->address_id == 0) {
- $where[] = ['user_id' => $form['user_id'], 'status' => 1, 'is_default' => 1];
- /**
- * @var $user_address UserAddress
- */
- $user_address = UserAddress::getOne($where, false, [], false, 'district_id, town_id');
- if (empty($user_address)) {
- $where1[] = ['user_id' => $form['user_id'], 'status' => 1];
- $user_address = UserAddress::getOne($where1, false, [], false, 'district_id, town_id');
- }
- } else {
- $where2[] = ['id' => $form->address_id, 'status' => 1];
- $user_address = UserAddress::getOne($where2, false, [], false, 'district_id, town_id');
- }
- $is_area_limit_town = \Yii::$app->services->setting->mall->get($form['mall_id'], 'area.is_area_limit_town');
- foreach ($form->sku as $item) {
- if ($item['goods']['is_on_sale'] == 0) {
- throw new Exception('当前商品已下架');
- }
- if ($item['goods']['is_area_limit'] == 1) {
- $area_limit = Json::decode($item['goods']['area_limit']);
- $address_ids = [];
- if(!empty($area_limit)&&is_array($area_limit)){
- $address_ids = array_column($area_limit, 'id');
- }
- if (!empty($user_address)) {
- $district_id = $user_address->district_id;
- $town_id = $user_address->town_id;
- }
- // 如果区县/乡镇有其中一个满足条件的话那么就需要抛出异常
- if (empty($is_area_limit_town)) {
- if ((!empty($district_id) && !in_array($district_id, $address_ids))) {
- throw new Exception('当前收货地址不允许购买');
- }
- } else if (!empty($town_id) && !in_array("_$town_id", $address_ids)) {
- throw new Exception('当前收货地址不允许购买!');
- }
- }else{
- $is_area_limit = \Yii::$app->services->setting->mall->get($form['mall_id'], 'area.is_area_limit');
- //创建订单时在判断限购局域
- if($is_area_limit && $is_area_limit==1 && $item['goods']['goods_type'] != GoodsTypeEnum::GoodsTypeVirtual && $form->action == 'create_order'){ //虚拟商品部限制
- $config = \Yii::$app->services->setting->mall->get($form['mall_id'], 'area.area_limit');
- $common_address_ids = [];
- if ($config) {
- $config = \Qiniu\json_decode($config, true);
- $common_address_ids = array_column($config, 'id');
- }
- // var_dump($district_id,$form->address_id,$user_address->district_id,$common_address_ids);die;
- if (!empty($common_address_ids)) {
- // 是否仅限制第三级
- if (empty($is_area_limit_town)) {
- if ($district_id != 0) {
- if (!in_array($district_id, $common_address_ids)) {
- throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域', 1);
- }
- }else if($user_address->district_id){
- if (!in_array($user_address->district_id, $common_address_ids)) {
- throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域', 1);
- }
- }
- } else {
- if ($town_id != 0) {
- if (!in_array("_$town_id", $common_address_ids))
- throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域!', 1);
- } elseif (!empty($user_address->town_id)) {
- if (!in_array("_$user_address->town_id", $common_address_ids)) {
- throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域!', 1);
- }
- }
- }
- }
- }
- }
- }
- return $form;
- }
- }
|