王佳豪
2021-04-07 ec444c3a240a5679fc83ec12fa0d7cf41826e932
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
150
151
152
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, target-densitydpi=high-dpi, initial-scale=1.0, user-scalable=no"/>
    <title>浙江中扬储存设备有限公司</title>
    <script type="text/javascript" src="../../static/js/common.js" charset="utf-8"></script>
    <style>
        html {
            background-color: #fff;
        }
        body {
            text-align: center;
            padding: 10px 30px;
        }
        .login-form {
            text-align: left;
        }
 
        .login-form input {
            display: block;
        }
        .login-form button {
            display: block;
        }
    </style>
</head>
<body>
    <h2>系统登录</h2>
    <div class="login-form">
        <div>
            <span>账号</span>
            <input type="text" id="mobile" value="super">
        </div>
        <div style="margin-top: 5px">
            <span>密码</span>
            <input type="password" id="password" value="xltys1995">
        </div>
        <div style="margin-top: 8px;height: 20px;">
            <button id="login" onclick="login()" style="padding: 5px 3px 0 3px">登录</button>
        </div>
 
    </div>
</body>
<script type="text/javascript" src="../../static/js/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../../static/js/tools/md5.js"></script>
<script type="text/javascript">
    document.onkeyup = function (e) {
        if (window.event)//如果window.event对象存在,就以此事件对象为准
            e = window.event;
        var code = e.charCode || e.keyCode;
        if (code === 13) {
            login()
        }
    }
    document.onkeydown = function (e) {
        if (window.event)//如果window.event对象存在,就以此事件对象为准
            e = window.event;
        var code = e.charCode || e.keyCode;
        if (code === 13) {
            document.getElementById("login");
        }
    }
    function login(){
        httpRequest({
            httpUrl: baseUrl+"/login.action",
            type: 'post',
            data: {
                mobile: document.getElementById('mobile').value,
                password: hex_md5(document.getElementById('password').value)
            }
        }, function (res) {
            if (res.code === 200) {
                // localStorage.setItem("token", res.data.token);
                // localStorage.setItem("username", res.data.username);
                setCookie("token", res.data.token);
                window.location.href = "index.html";
            } else {
                alert(res.msg);
            }
 
        })
    }
 
    // 设置cookie
    function setCookie(objName, objValue){//添加cookie
        var str = objName + "=" + encodeURIComponent(objValue);
        // if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失
        //     var date = new Date();
        //     var ms = objHours * 3600 * 1000;
        //     date.setTime(date.getTime() + ms);
        //     str += "; expires=" + date.toUTCString();
        // }
        str += "; path=/";
        document.cookie = str;
    }
 
    function httpRequest(paramObj,fun,errFun) {
        var xmlhttp = null;
        /*创建XMLHttpRequest对象,
         *老版本的 Internet Explorer(IE5 和 IE6)使用 ActiveX 对象:new ActiveXObject("Microsoft.XMLHTTP")
         * */
        if(window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }else if(window.ActiveXObject) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        /*判断是否支持请求*/
        if(xmlhttp == null) {
            alert('你的浏览器不支持XMLHttp');
            return;
        }
        /*请求方式,并且转换为大写*/
        var httpType = (paramObj.type || 'GET').toUpperCase();
        /*数据类型*/
        var dataType = paramObj.dataType || 'json';
        /*请求接口*/
        var httpUrl = paramObj.httpUrl || '';
        /*是否异步请求*/
        var async = paramObj.async || true;
        /*请求参数--post请求参数格式为:foo=bar&lorem=ipsum*/
        var paramData = paramObj.data || [];
        var requestData = '';
        for(var name in paramData) {
            requestData += name + '='+ paramData[name] + '&';
        }
        requestData = requestData === '' ? '' : requestData.substring(0,requestData.length - 1);
 
        /*请求接收*/
        xmlhttp.onreadystatechange = function() {
            if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                /*成功回调函数*/
                fun(JSON.parse(xmlhttp.responseText));
            }else{
                /*失败回调函数*/
                errFun;
            }
        }
 
        /*接口连接,先判断连接类型是post还是get*/
        if(httpType === 'GET') {
            xmlhttp.open("GET",httpUrl,async);
            xmlhttp.send(null);
        }else if(httpType === 'POST'){
            xmlhttp.open("POST",httpUrl,async);
            //发送合适的请求头信息
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send(requestData);
        }
    }
</script>
</html>