RegionLimit.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace common\logic\order\limit;
  3. use common\enums\AppEnum;
  4. use common\enums\GoodsTypeEnum;
  5. use common\enums\PreviewTypeEnum;
  6. use common\models\user\UserAddress;
  7. use Exception;
  8. use yii\helpers\Json;
  9. class RegionLimit extends BaseLimit
  10. {
  11. public function execute( $form)
  12. {
  13. try {
  14. } catch (Exception $e) {
  15. $this->setError('区域限制验证不通过,原因:' . $e->getMessage());
  16. return false;
  17. }
  18. }
  19. /**
  20. * 购买区域限制
  21. * User: wniu
  22. * Date: 2021/10/7
  23. * Time: 3:20 下午
  24. * * @param $form
  25. * @throws Exception
  26. */
  27. public static function compute($form)
  28. {
  29. if ($form->action == AppEnum::ACTION_PREVIEW) { // 预览不做处理
  30. return $form;
  31. }
  32. // 如果是预约类型的订单那么不做判断
  33. if ($form->type == PreviewTypeEnum::RESERVE_NOW) return $form;
  34. $district_id = 0;
  35. $town_id = 0;
  36. if ($form->address_id == 0) {
  37. $where[] = ['user_id' => $form['user_id'], 'status' => 1, 'is_default' => 1];
  38. /**
  39. * @var $user_address UserAddress
  40. */
  41. $user_address = UserAddress::getOne($where, false, [], false, 'district_id, town_id');
  42. if (empty($user_address)) {
  43. $where1[] = ['user_id' => $form['user_id'], 'status' => 1];
  44. $user_address = UserAddress::getOne($where1, false, [], false, 'district_id, town_id');
  45. }
  46. } else {
  47. $where2[] = ['id' => $form->address_id, 'status' => 1];
  48. $user_address = UserAddress::getOne($where2, false, [], false, 'district_id, town_id');
  49. }
  50. $is_area_limit_town = \Yii::$app->services->setting->mall->get($form['mall_id'], 'area.is_area_limit_town');
  51. foreach ($form->sku as $item) {
  52. if ($item['goods']['is_on_sale'] == 0) {
  53. throw new Exception('当前商品已下架');
  54. }
  55. if ($item['goods']['is_area_limit'] == 1) {
  56. $area_limit = Json::decode($item['goods']['area_limit']);
  57. $address_ids = [];
  58. if(!empty($area_limit)&&is_array($area_limit)){
  59. $address_ids = array_column($area_limit, 'id');
  60. }
  61. if (!empty($user_address)) {
  62. $district_id = $user_address->district_id;
  63. $town_id = $user_address->town_id;
  64. }
  65. // 如果区县/乡镇有其中一个满足条件的话那么就需要抛出异常
  66. if (empty($is_area_limit_town)) {
  67. if ((!empty($district_id) && !in_array($district_id, $address_ids))) {
  68. throw new Exception('当前收货地址不允许购买');
  69. }
  70. } else if (!empty($town_id) && !in_array("_$town_id", $address_ids)) {
  71. throw new Exception('当前收货地址不允许购买!');
  72. }
  73. }else{
  74. $is_area_limit = \Yii::$app->services->setting->mall->get($form['mall_id'], 'area.is_area_limit');
  75. //创建订单时在判断限购局域
  76. if($is_area_limit && $is_area_limit==1 && $item['goods']['goods_type'] != GoodsTypeEnum::GoodsTypeVirtual && $form->action == 'create_order'){ //虚拟商品部限制
  77. $config = \Yii::$app->services->setting->mall->get($form['mall_id'], 'area.area_limit');
  78. $common_address_ids = [];
  79. if ($config) {
  80. $config = \Qiniu\json_decode($config, true);
  81. $common_address_ids = array_column($config, 'id');
  82. }
  83. // var_dump($district_id,$form->address_id,$user_address->district_id,$common_address_ids);die;
  84. if (!empty($common_address_ids)) {
  85. // 是否仅限制第三级
  86. if (empty($is_area_limit_town)) {
  87. if ($district_id != 0) {
  88. if (!in_array($district_id, $common_address_ids)) {
  89. throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域', 1);
  90. }
  91. }else if($user_address->district_id){
  92. if (!in_array($user_address->district_id, $common_address_ids)) {
  93. throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域', 1);
  94. }
  95. }
  96. } else {
  97. if ($town_id != 0) {
  98. if (!in_array("_$town_id", $common_address_ids))
  99. throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域!', 1);
  100. } elseif (!empty($user_address->town_id)) {
  101. if (!in_array("_$user_address->town_id", $common_address_ids)) {
  102. throw new Exception('全局配置设置,当前收货地址不允许购买,请把默认收货地址设置成允许收货地址的局域!', 1);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. return $form;
  111. }
  112. }