う三余无梦生

快递单、运单号自动联想功能

做电子商务的,都需要设计到扫描快递单。

如果是批量速打的话,能够将运单号联想起来,将会带来许多方便。

就目前仅知道的几家快递公司的规律,小弟做出一个运单号联想的web版,共享出来。

demo需要使用Jquery.

/*
2012.10.12 vinbo 
请对文本框统一使用类名:
ec_logistics
请使用<input type="hidden" id="EC_ExpressCode" value="STO" />设置快递单编码格式
**/

var EC_ArrayLength = 500; //设置最多的500个面单(因为快递面单是500个一箱)
var FirstCode = ""; //记录扫描的第一个单号
var EC_defultCssName = ".ec_logistics";//默认的文本框样式名称
var EC_logistics; //所有的文本框列表 jquery对象
var startIndex = 0; //用于记录当前扫描时候选中的文本的 索引。以后的自动联想,从这个开始。


$(document).ready(function () {
    var ecode = $("#EC_ExpressCode").val();
    EC_logistics = $(EC_defultCssName); //所有的文本框
    //文本框的变更事件
    EC_logistics.keydown(function (e) {
        if (e.keyCode == 13) {
            var index = $(this).index(EC_defultCssName);
            startIndex = index;
            FirstCode = $(this).val();
            index = index + 1;
            if (index == EC_logistics.length) {
                return;
            } else {
                AllotExpressCodeAuto(ecode);
                $(EC_defultCssName + ":eq(" + index + ")").select();
            }
        }

    });

    //自动分配运单号
    AllotExpressCodeAuto = function (ExpressCode) {
        var strArray;//获取自动联想得到的数组
        switch (ExpressCode) {
            case "STO":
                strArray = STO();
                break;
            case "YTO":
                strArray = YTO();
                break;
            case "EMS":
                strArray = EMS();
                break;
            case "SF":
                strArray = SF();
                break;
            default:
                strArray = GENERAL();
                break;
        }
        var len = strArray.length;
        $.each(EC_logistics, function (i) {
            if (i > len) {
                $(EC_defultCssName + ":eq(" + i + ")").select(); //数组循环完毕,将鼠标放到最后一行
                return;
            }
            else if (i < startIndex) { 
                //跳入下一个循环
            }
            else {
                $(this).val(strArray[i - startIndex]);//显示文字
            }
        });
    }

    //申通
    STO = function () {
        return GETARRAY();
    }

    //圆通
    YTO = function () {
        return GETARRAY();
    }

    //EMS
    EMS = function () {
        return GETARRAY_EMS();
    }

    //顺丰
    SF = function () {
        return GETARRAY_SF();
    }


    //一般方法。只显示扫描上来的一个运单号
    GENERAL = function () {
        var strValue = new Array(1);
        strValue[0] = FirstCode;
        return strValue;
    }


    //自增长型数组
    GETARRAY = function () {
        var strstart = FirstCode.substr(0, FirstCode.length - 5); //截取留下5位数字的字符串
        var num = FirstCode.substr(FirstCode.length - 5, 5); //截取剩余的数字
        var firstNum = parseInt(num); //将剩余的数字字符创转换成为 int类型
        var count = firstNum % EC_ArrayLength; //获取剩下的数组的长度。如果传入500的整数,长度是本身,如果传入001,长度就是500,如果传入499,999的尾数,长度就是2
        var length = ((EC_ArrayLength - count) % EC_ArrayLength) + 1;

        //建立一个数组
        var strValue = new Array(length);
        strValue[0] = FirstCode;
        //默认放入第一个,将扫描出来的结果
        for (var j = 1; j < length; j++) {
            strValue[j] = strstart.toString() + (firstNum + j).toPrecision(5);
        }
        return strValue;
    }

    GETARRAY_EMS = function () {
        var fri;
        var num3, num4, num5, num6, num7, num8, num9, num0;
        var res;
        var mid;
        var i;
        var EMSres = new Array(EC_ArrayLength);
        fri = FirstCode.substr(2, 8);

        var firstM2 = FirstCode.substr(0, 2);
        var lastM2 = FirstCode.substr(FirstCode.length - 2, 2);
        for (i = 1; i <= EC_ArrayLength; i++) {
            num3 = parseInt(fri.substr(0, 1));
            num4 = parseInt(fri.substr(1, 1));
            num5 = parseInt(fri.substr(2, 1));
            num6 = parseInt(fri.substr(3, 1));
            num7 = parseInt(fri.substr(4, 1));
            num8 = parseInt(fri.substr(5, 1));
            num9 = parseInt(fri.substr(6, 1));
            num0 = parseInt(fri.substr(7, 1));
            mid = 8 * num3 + 6 * num4 + 4 * num5 + 2 * num6 + 3 * num7 + 5 * num8 + 9 * num9 + 7 * num0;
            res = 11 - (mid % 11);
            if (res == 10)
                res = 0;
            else if (res == 11)
                res = 5;
            EMSres[i - 1] = firstM2 + fri + res.toString() + lastM2;
            fri = (parseInt(fri) + 1).toString();
        }
        return EMSres;
    }

    GETARRAY_SF = function () {
        //声明
        var fri, Nfri, YunDanHao;
        var num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12;
        var Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num10, Num11, Num12;
        var res;
        var mid;
        var i;
        var SFSres = new Array(EC_ArrayLength);
        //赋值 
        fri = FirstCode.substr(0, 11);
        SFSres[0] = FirstCode;
        YunDanHao = FirstCode;
        for (i = 1; i <= EC_ArrayLength - 1; i++) {
            Nfri = (parseFloat(fri) + 1).toString();
            var j = 0;
            num1 = parseInt(YunDanHao.substr(j++, 1));
            num2 = parseInt(YunDanHao.substr(j++, 1));
            num3 = parseInt(YunDanHao.substr(j++, 1));
            num4 = parseInt(YunDanHao.substr(j++, 1));
            num5 = parseInt(YunDanHao.substr(j++, 1));
            num6 = parseInt(YunDanHao.substr(j++, 1));
            num7 = parseInt(YunDanHao.substr(j++, 1));
            num8 = parseInt(YunDanHao.substr(j++, 1));
            num9 = parseInt(YunDanHao.substr(j++, 1));
            num10 = parseInt(YunDanHao.substr(j++, 1));
            num11 = parseInt(YunDanHao.substr(j++, 1));
            num12 = parseInt(YunDanHao.substr(j++, 1));
            j = 0;
            Num1 = parseInt(Nfri.substr(j++, 1));
            Num2 = parseInt(Nfri.substr(j++, 1));
            Num3 = parseInt(Nfri.substr(j++, 1));
            Num4 = parseInt(Nfri.substr(j++, 1));
            Num5 = parseInt(Nfri.substr(j++, 1));
            Num6 = parseInt(Nfri.substr(j++, 1));
            Num7 = parseInt(Nfri.substr(j++, 1));
            Num8 = parseInt(Nfri.substr(j++, 1));
            Num9 = parseInt(Nfri.substr(j++, 1));
            Num10 = parseInt(Nfri.substr(j++, 1));
            Num11 = parseInt(Nfri.substr(j++, 1));

            if (((Num9 - num9) == 1) && ((num9 % 2) == 1)) {
                if (num12 - 8 >= 0)
                    Num12 = num12 - 8;
                else
                    Num12 = num12 - 8 + 10;
            }
            else if (((Num9 - num9) == 1) && ((num9 % 2) == 0)) {
                if (num12 - 7 >= 0)
                    Num12 = num12 - 7;
                else
                    Num12 = num12 - 7 + 10;
            }
            else {
                if ((num10 == 3) || (num10 == 6) && (num11 == 9)) {
                    if (num12 - 5 >= 0)
                        Num12 = num12 - 5;
                    else
                        Num12 = num12 - 5 + 10;
                }
                else if (num11 == 9) {
                    if (num12 - 4 >= 0)
                        Num12 = num12 - 4;
                    else
                        Num12 = num12 - 4 + 10;
                }
                else {
                    if (num12 - 1 >= 0)
                        Num12 = num12 - 1;
                    else
                        Num12 = num12 - 1 + 10;
                }

            }
            SFSres[i] = (Nfri + (Num12).toString());
            YunDanHao = Nfri + Num12.toString();
            fri = (parseFloat(fri) + 1).toString();
        } //循环结束

        return SFSres;

    }

});

  

 接下来是页面:

<html>
<head>
<title>测试显示运单号</title>
<script src="jquery.js" type="text/javascript"></script>
<script src="ExpressCode.js" type="text/javascript"></script>
</head>
<body>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input class="ec_logistics" type="text"  /><br/>
    <input type="hidden" id="EC_ExpressCode" value="STO" /><!--在这个隐藏文本框,设置快递公司的编号。
    圆通:YTO,申通:STO,顺丰:SF,EMS:EMS,联昊通:LHT,详情参见淘宝给出的具体编码。
    -->
</body>
</html>

目前仅有的支持 SF,EMS,STO,YTO 等常见的。其他的因为没有完整的快递单查看,不清楚具体规律。

posted on 2012-10-12 15:03  う三余无梦生  阅读(666)  评论(0编辑  收藏  举报

导航