13693261870
2025-06-24 8565bd83fcd670ec8379084d600eb97d18037d21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
window.isBusy = false;
 
// 创建验证码
function createCode() {
    var codeLength = 4, code = ""; // 验证码的长度
    var checkCode = document.getElementById("checkCode");
 
    var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    for (var i = 0; i < codeLength; i++) {
        var charNum = Math.floor(Math.random() * 10);
        code += codeChars[charNum];
    }
    if (checkCode) {
        checkCode.innerHTML = code;
    }
}
 
// 获取公钥
function getPublicKey() {
    $.get("getPublicKey", function (rs) {
        if (rs && rs.code == 200) {
            window.encrypt = new JSEncrypt();
            encrypt.setPublicKey(rs.result);
        }
    });
}
 
// 验证
function sysValidate() {
    // 验证用户名与密码
    var theUsername = document.getElementById("username").value;
    if (theUsername.length <= 0) {
        $("#eMsg").html("提示:请输入用户名!");
        $("#username").focus();
        return false;
    }
    var thePassword = document.getElementById("password").value;
    if (thePassword.length <= 0) {
        $("#eMsg").html("提示:请输入密码!");
        $("#password").focus();
        return false;
    }
    /*if (!isValid(thePassword)) {
      $("#eMsg").html("提示:密码为8-20位包含字母、数字和特殊字符!");
      $("#password").focus();
      return false;
    }*/
 
    // 验证验证码
    var inputCode = document.getElementById("inputCode").value;
    if (inputCode.length <= 0) {
        $("#eMsg").html("提示:请输入验证码!");
        $("#inputCode").focus();
        return false;
    }
    var code = document.getElementById("checkCode").innerHTML;
    if (inputCode.toUpperCase() != code.toUpperCase()) {
        $("#eMsg").html("提示:验证码输入有误!");
        $("#inputCode").val("").focus();
        createCode();
        return false;
    }
 
    return true;
}
 
// login
function sysLogin() {
    if (isBusy || !sysValidate()) {
        return;
    }
 
    isBusy = true;
    var username = $.trim($("#username").val());
    var password = $("#password").val();
    var service = getQueryStr("service");
 
    var data = {
        "uid": encrypt.encrypt(username),
        "pwd": encrypt.encrypt(password)
    };
    ajax("login", "POST", JSON.stringify(data), null, null, function (rs) {
        isBusy = false;
        if (!rs || rs.code !== 200) {
            createCode();
            var msg = rs && rs.msg ? rs.msg : "登录失败!";
            $("#eMsg").html(msg);
            return;
        }
        if (rs.msg) alert(rs.msg);
 
        var service = getQueryStr("service");
        if (service) {
            service += (service.indexOf("?") > -1 ? "&" : "?") + "token=" + rs.result.token;
            location.href = service;
            return;
        }
 
        location.href = location.href.replace("/sign/toLogin", "/sign/toIndex");
    }, function () {
        isBusy = false;
        alert("登录出错,请联系管理员!");
    });
}
 
// Ajax
function ajax(url, type, data, dataType, contentType, fn, ex) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        dataType: dataType || "json", // html、json、jsonp、script、text
        contentType: contentType || "application/json", // "application/x-www-form-urlencoded"
        success: function (data) {
            fn(data);
        },
        error: function (e) {
            console.error(e);
            ex(e);
        }
    });
}
 
// 获取URL参数
function getQueryStr(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return decodeURI(r[2]);
    }
    return null;
}
 
// 键盘按下事件
document.onkeydown = function (e) {
    var ev = window.event || e;
    var code = ev.keyCode || ev.which || ev.charCode;
    if (code == 13) {
        sysLogin();
    }
}
 
// 密码是否合规
function isValid(pwd) {
    if (!pwd) return false;
 
    var regex = new RegExp('^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W!@#$%^&*`~()\\-_+=,.?;<>]+$)(?![a-z0-9]+$)(?![a-z\W!@#$%^&*`~()\\-_+=,.?;<>]+$)(?![0-9\W!@#$%^&*`~()\\-_+=,.?;<>]+$)[a-zA-Z0-9\W!@#$%^&*`~()\\-_+=,.?;<>]{12,20}$');
    return regex.test(pwd);
}