Commit 147fdcc79af5e286e524fc77902675f48203c6f8
1 parent
5d832b12
物流运算的公共函数
Showing
1 changed file
with
78 additions
and
0 deletions
utils/util.js
... | ... | @@ -514,6 +514,83 @@ function wx_back() { |
514 | 514 | } |
515 | 515 | } |
516 | 516 | |
517 | + | |
518 | +//-------------------计算物流--------------- | |
519 | +function calculatewuliu(code, o_shipping_price, goods_weight, out_of_weight, | |
520 | + goods_piece, user_addr, back_data,rs) { | |
521 | + var price = 0; | |
522 | + price += parseFloat(o_shipping_price); | |
523 | + if (user_addr == null) { | |
524 | + return 0; | |
525 | + } | |
526 | + //如果是包邮 | |
527 | + if (back_data && back_data.is_by_all && !back_data.no_free_goods && out_of_weight >= 0) { | |
528 | + return 0; | |
529 | + } | |
530 | + //计算物流的config item; | |
531 | + var item = null; | |
532 | + | |
533 | + //------------循环获取config----------- | |
534 | + function get_wuliu_config(region_id, code, rs) { | |
535 | + var item = null, rslist = rs.pageData; | |
536 | + for (var i = 0; i < rslist.length; i++) { | |
537 | + if (rslist[i].code == code && rslist[i].region_id == region_id) { | |
538 | + item = rslist[i]; | |
539 | + } | |
540 | + } | |
541 | + return item; | |
542 | + } | |
543 | + //-------循环获取config,code default------- | |
544 | + function get_wuliu_default(code, rs) { | |
545 | + var item = null, rslist = rs.pageData; | |
546 | + for (var i = 0; i < rslist.length; i++) { | |
547 | + if (rslist[i].shipping_code == code && rslist[i].is_default == 1) { | |
548 | + item = rslist[i]; | |
549 | + } | |
550 | + } | |
551 | + return item; | |
552 | + } | |
553 | + | |
554 | + //先根据 镇 县 区找计算的config | |
555 | + item = get_wuliu_config(user_addr.district, code, rs); | |
556 | + if (item == null) item = get_wuliu_config(user_addr.city, code, rs); | |
557 | + if (item == null) item = get_wuliu_config(user_addr.province, code, rs); | |
558 | + if (item == null) item = get_wuliu_default(code, rs); | |
559 | + if (item == null) return o_shipping_price; | |
560 | + var fw_price = 0, fp_price = 0; | |
561 | + item = item.config; | |
562 | + if (item == null) return o_shipping_price; | |
563 | + | |
564 | + //------超出重量---------- | |
565 | + if (back_data && back_data.is_by_all && out_of_weight<0){ | |
566 | + goods_weight+=out_of_weight; | |
567 | + if(goods_weight<=0) goods_weight=-1; | |
568 | + } | |
569 | + | |
570 | + //------按重量---------- | |
571 | + if (goods_weight >= 0 && item['money']) { | |
572 | + fw_price = parseFloat(item['money']); | |
573 | + if (goods_weight > item['first_weight']) { | |
574 | + var fw = goods_weight - item['first_weight']; | |
575 | + var n = Math.ceil(fw / item['second_weight']) | |
576 | + fw_price = fw_price + n * parseFloat(item['add_money']); | |
577 | + } | |
578 | + } | |
579 | + | |
580 | + //------按件数---------- | |
581 | + if (goods_piece > 0 && item['piecemoney']) { | |
582 | + fp_price = parseFloat(item['piecemoney']); | |
583 | + if (goods_piece > item['first_piece']) { | |
584 | + var fp = goods_piece - item['first_piece']; | |
585 | + var m = Math.ceil(fp / item['second_piece']) | |
586 | + fp_price = fp_price + m * parseFloat(item['add_piecemoney']); | |
587 | + } | |
588 | + } | |
589 | + | |
590 | + var rspice = parseFloat(price + fw_price + fp_price); | |
591 | + return rspice; | |
592 | +} | |
593 | + | |
517 | 594 | module.exports = { |
518 | 595 | formatTime: function(e, r) { |
519 | 596 | 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 = { |
627 | 704 | ajax_ok2:ajax_ok2, //将JS数组对象按其某个键值重组成Map对象 |
628 | 705 | wx_back:wx_back, |
629 | 706 | _debounce, |
707 | + calculatewuliu:calculatewuliu, //计算物流的函数进行抽象 | |
630 | 708 | }; | ... | ... |