From 147fdcc79af5e286e524fc77902675f48203c6f8 Mon Sep 17 00:00:00 2001 From: yvan.ni Date: Fri, 13 May 2022 11:44:08 +0800 Subject: [PATCH] 物流运算的公共函数 --- utils/util.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+), 0 deletions(-) diff --git a/utils/util.js b/utils/util.js index c5dbcd7..6dc7f0a 100644 --- a/utils/util.js +++ b/utils/util.js @@ -514,6 +514,83 @@ function wx_back() { } } + +//-------------------计算物流--------------- +function calculatewuliu(code, o_shipping_price, goods_weight, out_of_weight, + goods_piece, user_addr, back_data,rs) { + var price = 0; + price += parseFloat(o_shipping_price); + if (user_addr == null) { + return 0; + } + //如果是包邮 + if (back_data && back_data.is_by_all && !back_data.no_free_goods && out_of_weight >= 0) { + return 0; + } + //计算物流的config item; + var item = null; + + //------------循环获取config----------- + function get_wuliu_config(region_id, code, rs) { + var item = null, rslist = rs.pageData; + for (var i = 0; i < rslist.length; i++) { + if (rslist[i].code == code && rslist[i].region_id == region_id) { + item = rslist[i]; + } + } + return item; + } + //-------循环获取config,code default------- + function get_wuliu_default(code, rs) { + var item = null, rslist = rs.pageData; + for (var i = 0; i < rslist.length; i++) { + if (rslist[i].shipping_code == code && rslist[i].is_default == 1) { + item = rslist[i]; + } + } + return item; + } + + //先根据 镇 县 区找计算的config + item = get_wuliu_config(user_addr.district, code, rs); + if (item == null) item = get_wuliu_config(user_addr.city, code, rs); + if (item == null) item = get_wuliu_config(user_addr.province, code, rs); + if (item == null) item = get_wuliu_default(code, rs); + if (item == null) return o_shipping_price; + var fw_price = 0, fp_price = 0; + item = item.config; + if (item == null) return o_shipping_price; + + //------超出重量---------- + if (back_data && back_data.is_by_all && out_of_weight<0){ + goods_weight+=out_of_weight; + if(goods_weight<=0) goods_weight=-1; + } + + //------按重量---------- + if (goods_weight >= 0 && item['money']) { + fw_price = parseFloat(item['money']); + if (goods_weight > item['first_weight']) { + var fw = goods_weight - item['first_weight']; + var n = Math.ceil(fw / item['second_weight']) + fw_price = fw_price + n * parseFloat(item['add_money']); + } + } + + //------按件数---------- + if (goods_piece > 0 && item['piecemoney']) { + fp_price = parseFloat(item['piecemoney']); + if (goods_piece > item['first_piece']) { + var fp = goods_piece - item['first_piece']; + var m = Math.ceil(fp / item['second_piece']) + fp_price = fp_price + m * parseFloat(item['add_piecemoney']); + } + } + + var rspice = parseFloat(price + fw_price + fp_price); + return rspice; +} + module.exports = { formatTime: function(e, r) { var t = e ? new Date(1e3 * e) : new Date(), n = t.getFullYear(), o = t.getMonth() + 1, a = t.getDate(), u = t.getHours(), i = t.getMinutes(), f = t.getSeconds(), s = function(e) { @@ -627,4 +704,5 @@ module.exports = { ajax_ok2:ajax_ok2, //将JS数组对象按其某个键值重组成Map对象 wx_back:wx_back, _debounce, + calculatewuliu:calculatewuliu, //计算物流的函数进行抽象 }; -- libgit2 0.21.4