From 731ed973cb8378d5322d1d379d1c2cf0ea2aca3d Mon Sep 17 00:00:00 2001 From: xucong <850806214@qq.com> Date: Fri, 16 May 2025 16:34:24 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=8F=90=E4=BA=A4=E7=BC=BA=E5=A4=B1=E7=9A=84?= =?UTF-8?q?=E4=B8=9C=E8=A5=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/logic/FreightLogic.php | 178 +++++++++++++++++++++++++++++++++ app/common/model/Freight.php | 62 ++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 app/api/logic/FreightLogic.php create mode 100644 app/common/model/Freight.php diff --git a/app/api/logic/FreightLogic.php b/app/api/logic/FreightLogic.php new file mode 100644 index 0000000..b1161e8 --- /dev/null +++ b/app/api/logic/FreightLogic.php @@ -0,0 +1,178 @@ + 0){ + $template_list[$good['free_shipping_template_id']][] = $good; + } + } + + foreach ($template_list as $template_id => $template_goods) { + $temp = []; + $temp['template_id'] = $template_id; + $temp['total_volume'] = 0; + $temp['total_weight'] = 0; + $temp['goods_num'] = 0; + foreach ($template_goods as $template_good) { + $temp['total_volume'] += $template_good['volume'] * $template_good['goods_num']; + $temp['total_weight'] += $template_good['weight'] * $template_good['goods_num']; + $temp['goods_num'] += $template_good['goods_num']; + } + $shipping_price += self::calculate($temp, $user_address); + } + + return $shipping_price < 0 ? 0 : $shipping_price; + } + + + /** + * User: 意象信息科技 mjf + * Desc: 计算运费 + * @param $data + * @param $user_address + * @return float|int + */ + public static function calculate($data, $user_address) + { + $shipping_price = 0; + + $freight = FreightLogic::getFreightsByAddress($data['template_id'], $user_address); + + if (empty($freight)){ + return $shipping_price; + } + $unit = 0; + //按重量计算 + if ($freight['charge_way'] == Freight::CHARGE_WAY_WEIGHT){ + $unit = $data['total_weight']; + } + + //按件数计算 + if ($freight['charge_way'] == Freight::CHARGE_WAY_PIECE){ + $unit = $data['goods_num']; + } + + //按体积计算 + if ($freight['charge_way'] == Freight::CHARGE_WAY_VOLUME){ + $unit = $data['total_volume']; + } + + if($unit > $freight['first_unit'] && $freight['continue_unit'] > 0){ + $left = ceil(($unit - $freight['first_unit']) / $freight['continue_unit']);//取整 + return $freight['first_money'] + $left * $freight['continue_money']; + }else{ + return $freight['first_money']; + } + } + + + + /** + * User: 意象信息科技 mjf + * Desc: 通过用户地址获取运费模板 + * @param $address + */ + public static function getFreightsByAddress($template_id, $address) + { + $district_id = $address['district_id']; + $city_id = $address['city_id']; + $province_id = $address['province_id']; + + $freights = Db::name('freight')->alias('f') + ->join('freight_config c', 'c.freight_id = f.id') + ->where('f.id', $template_id) + ->order(['f.id' => 'desc', 'c.id' => 'desc']) + ->select(); + + foreach ($freights as $freight) { + $region_ids = explode(',', $freight['region']); + if (in_array($district_id, $region_ids)) { + return $freight; + } + + if (in_array($city_id, $region_ids)) { + return $freight; + } + + if (in_array($province_id, $region_ids)) { + return $freight; + } + + if ($freight['region'] = 'all'){ + $national_freight = $freight; + } + } + + //会员的省市区id在商家的运费模板(指定地区)中找不到,查一下商家的全国运费模板 + return $national_freight; + } + + /** + * User: 意象信息科技 mjf + * Desc: 模板中指定地区id是否存在 + * @param $freights + * @param $region_id + * @return bool|mixed + */ + public static function isExistRegionId($freights, $region_id) + { + foreach ($freights as $freight) { + $region_ids = explode(',', $freight['region']); + if (in_array($region_id, $region_ids)) { + return $freight; + } + } + return false; + } +} \ No newline at end of file diff --git a/app/common/model/Freight.php b/app/common/model/Freight.php new file mode 100644 index 0000000..0ce35f8 --- /dev/null +++ b/app/common/model/Freight.php @@ -0,0 +1,62 @@ + '按重量计费', + self::CHARGE_WAY_VOLUME => '按体积计费', + self::CHARGE_WAY_PIECE => '按件计费', + ]; + + if ($type === true) { + return $data; + } + + return $data[$type] ?? '未知'; + } + + public function getChargeWayTextAttr($value, $data) + { + return self::getChargeWay($data['charge_way']); + } + + + public function configs() + { + return $this->hasMany('freight_config', 'freight_id', 'id'); + } + +} \ No newline at end of file