1.提交缺失的东西

This commit is contained in:
2025-05-16 16:34:24 +08:00
parent ca27e5d27d
commit 731ed973cb
2 changed files with 240 additions and 0 deletions

View File

@ -0,0 +1,178 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\api\logic;
use app\common\model\Freight;
use think\Db;
/**
* 运费逻辑
* Class FreightLogic
* @package app\api\logic
*/
class FreightLogic
{
/**
* User: 意象信息科技 mjf
* Desc: 计算运费
* @param $goods
* @param $user_address
* @return float|int
*/
public static function calculateFreight($goods, $user_address)
{
$shipping_price = 0;
$template_list = [];
if (empty($user_address)){
return $shipping_price;
}
foreach ($goods as $good){
//统一邮费
if ($good['free_shipping_type'] == 2){
$shipping_price += round($good['free_shipping'] * $good['goods_num'], 2);
}
//指定运费模板
if ($good['free_shipping_type'] == 3 && $good['free_shipping_template_id'] > 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;
}
}

View File

@ -0,0 +1,62 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Freight extends Model
{
protected $name = 'freight';
protected $autoWriteTimestamp = true;
//计费方式
const CHARGE_WAY_WEIGHT = 1;//按重量计费
const CHARGE_WAY_VOLUME = 2; //体积计费
const CHARGE_WAY_PIECE = 3;//按件计费
public static function getChargeWay($type)
{
$data = [
self::CHARGE_WAY_WEIGHT => '按重量计费',
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');
}
}