基於PHP後端 設計商城快遞運費模版系統

先看成品效果:

在這裏插入圖片描述

編輯頁面展示:(以模版一爲例)

在這裏插入圖片描述
先分析下思路,我們最後寫一個接口,通過三個參數自動計算快遞費用.

首先我們要定義一個全國通用的運費計算方式(即默認運費),即不指定地區的時候,我們按全國通用費用來計算運費.
例如當購買不超過10件時,費用設置爲10元,每增加一件,運費漲2元.

其次如果指定地區來收取運費,那麼就按照我們定義的地區規則來執行
例如我們設置新疆在購買10件內運費50元,每增加一件,收取5元.
可以指定多個地區,甚至國外…這個是可選的

同時我們還可以指定包郵的地區,同時設置在多少件包郵,如果超過了範圍則執行全國默認規則.這個是可選的

數據庫:

快遞地址省市縣表 mxd_express_city
CREATE TABLE mxd_express_city (
id int(11) NOT NULL DEFAULT ‘0’,
pid int(11) DEFAULT NULL,
cityname varchar(255) CHARACTER SET utf8 DEFAULT NULL,
type int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT=‘快遞地址省市縣表’;
在這裏插入圖片描述

快遞模版表 mxd_express_delivery
CREATE TABLE mxd_express_delivery (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL DEFAULT ‘’ COMMENT ‘模版名稱’,
baby_address varchar(1000) NOT NULL DEFAULT ‘’ COMMENT ‘寶貝地址’,
send_time int(10) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘限定發貨時間’,
is_free int(255) NOT NULL COMMENT ‘是否包郵 0=不包郵,1=包郵’,
price_method int(8) NOT NULL DEFAULT ‘0’ COMMENT ‘計價方式’,
specify_conditions int(8) NOT NULL DEFAULT ‘0’ COMMENT ‘是否指定條件包郵’,
default_pieces int(255) DEFAULT NULL COMMENT ‘默認件數’,
default_price decimal(10,2) DEFAULT NULL COMMENT ‘默認價格’,
add_pieces int(10) DEFAULT NULL COMMENT ‘續件’,
add_price decimal(10,2) DEFAULT NULL COMMENT ‘續費’,
create_time int(10) NOT NULL DEFAULT ‘0’ COMMENT ‘添加時間’,
update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ‘更新時間’,
PRIMARY KEY (id) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=‘快遞模版表’;

在這裏插入圖片描述

運送方式表 mxd_express_shipping_method

CREATE TABLE mxd_express_shipping_method (
id int(11) NOT NULL AUTO_INCREMENT,
template_id int(10) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘關聯模板ID’,
fast_address varchar(10000) NOT NULL DEFAULT ‘0’ COMMENT ‘運送地區’,
first_pieces int(10) unsigned DEFAULT ‘0’ COMMENT ‘首件’,
first_weight decimal(10,2) NOT NULL COMMENT ‘首重’,
first_volume decimal(10,2) NOT NULL DEFAULT ‘0.00’ COMMENT ‘首體積’,
first_amount decimal(10,2) DEFAULT NULL COMMENT ‘首費’,
add_pieces int(10) NOT NULL DEFAULT ‘0’ COMMENT ‘續件’,
add_weight decimal(10,2) NOT NULL COMMENT ‘續重’,
add_volume decimal(10,2) unsigned DEFAULT ‘0.00’ COMMENT ‘續體積’,
add_amount decimal(10,2) NOT NULL DEFAULT ‘0.00’ COMMENT ‘續費’,
create_time varchar(255) NOT NULL,
update_time timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=‘運送方式表’;

包郵條件表 mxd_express_free

CREATE TABLE mxd_express_free (
id int(11) NOT NULL AUTO_INCREMENT,
template_id int(10) unsigned DEFAULT ‘0’ COMMENT ‘關聯模板ID’,
address varchar(1000) DEFAULT ‘’ COMMENT ‘包郵地區’,
pieces int(10) unsigned DEFAULT ‘1’ COMMENT ‘包郵件數’,
weight decimal(6,2) NOT NULL DEFAULT ‘0.00’ COMMENT ‘包郵重量’,
volume decimal(8,2) NOT NULL DEFAULT ‘0.00’ COMMENT ‘包郵體積’,
create_time varchar(255) NOT NULL,
update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=‘包郵條件表’;

先把前端自己做出來,前端的難度不是很大,增刪改查先把後臺做出來.

然後前端頁面
在這裏插入圖片描述
我們後端寫一個接口自動計算運費.

接口思路是這樣的:
我們需要三個參數,首先佳貝艾特2段這個商品用了哪個快遞模版,那麼這個模版id是必須的,
然後是購買的多少件,
然後是發到地址.

所以我們需要這三個參數就可以自動計算運費了

第一步,我們通過模版id,判斷該商品是否包郵,如果包郵,則不執行後續操作,直接
$freight = 0;截斷了事.

第二步,我們判斷有沒有增加指定地區的運費規則,如果有的話,那麼我們先判斷件數,分兩種情況

第三步,我們判斷有沒有指定地區包郵,如果有包郵的,那麼 $freight = 0;直接截斷.

第四步,以上三種情況走完後,最後往下繼續走,先判斷件數,也是兩種情況,算出運費即可

最後, return $freight; 即可.

下面貼出這個功能,只要調用了這個接口,運費就自動計算好了:


    /**
     * 通過地址和件數計算運費
     * @param $addr   郵寄地址
     * @param $pieces  郵寄件數
     * @param $express_template_id  快遞模板id
     * @return int|string
     */
    public function get_freight($addr,$pieces,$express_template_id){
        $freight = 0;
        $express_template_info = D('ExpressDelivery')->where(['id'=>$express_template_id])->find();

        //包郵
        if($express_template_info['is_free'] == 1){
            $freight = 0;
            return  $freight;
        }

        $express_template_info['child1'] = M()
            ->table('mxd_express_shipping_method')
            ->where(['template_id' => $express_template_id])
            ->select();

        $express_template_info['child2'] = M()
            ->table('mxd_express_free')
            ->where(['template_id'=>$express_template_id])
            ->select();


        $city_id = M()->table('mxd_express_city')
            ->where(['cityname'=>$addr,'type'=>1])
            ->field('id')
            ->find()['id'];


        //首先判斷地址在不在child1中,如果在的話就按其規則計算,如果不在就按通用運費計算
        if(!empty($express_template_info['child1'])){
            foreach ($express_template_info['child1'] as $i => $node){

                if(in_array($city_id,explode(',',$node['fast_address']))){
                    //按照指定地區的快遞費用規則計算
                    if($pieces <= $node['first_pieces']){
                        $freight = $node['first_amount'];
                        return $freight;
                    }else{
                        if($node['add_pieces']){
                            $add_pieces = bcdiv(($pieces - $node['first_pieces']) , $node['add_pieces'],2);
                            $freight = $node['first_amount'] +  bcmul($add_pieces,$node['add_amount'],2) ;
                            return $freight;
                        }else{
                            $freight = $node['first_amount'];
                            return $freight;
                        }
                    }
                }
            }
        }


        //判斷是否在包郵地區
        if(!empty($express_template_info['child2'])){
            foreach ($express_template_info['child2'] as $j => $val){
                if(in_array($city_id,explode(',',$val['address']))){
                    if($pieces <= $val['pieces']){
                        $freight = 0;
                        return  $freight;
                    }
                }
            }
        }



        //獲取默認件數
        $default_pieces = $express_template_info['default_pieces'];
        //獲取默認運費
        $default_price = $express_template_info['default_price']  ;

        //按默認規則計算運費
        if($pieces <= $default_pieces){
            $freight = $default_price;
        }else{
            if($express_template_info['add_pieces']){
                $add_pieces = bcdiv(($pieces - $default_pieces) , $express_template_info['add_pieces'],2);
                $freight = $default_price +  bcmul($add_pieces,$express_template_info['add_price'],2) ;
            }else{
                $freight = $default_price;
            }
        }

        return $freight;

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章