田源
2024-04-07 2ac55ce0edf4870a29691b56bfad59f4830a11a2
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//处理登录页面的相关动作
//weidy@2013-08-01
 
var path = "";//后台路径
var configData = {};
var browserInfo = null;
var processStep = 0;
var processInterval = null;
var usedTime = 0;
var projectName = "";
var pathName = "";
var jq = null;
$(document).ready(function() {
    jq = $;
    // 先判断浏览器版本
    browserInfo = checkBrowser();
    pathName = window.document.location.pathname;
    projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1) + "/";
    //处理页面的内容
    getConfig(function(){
        initDefaultConfig();
        //checkIsLogin(function(){
            initPage();//获取配置后,检查是否已经登录过,如果没有登录则在初始化页面的内容
        //});
    });
});
function getConfig(callback){
    $.getJSON(projectName + "config.json?v=" +  (new Date()).getTime(),function(data){
        configData = data;
        if(configData.isDebug == "true" || configData.isDebug == true){
            path = configData.backPath;
        }else{
            path = projectName;
            if(configData.compatibility) {
                if (pathName.indexOf("/") > -1) {
                    if(""!=configData.unCorsPath){
                        path = path.substring(0, path.lastIndexOf("/")) + "/" + configData.unCorsPath + "/";
                    }else{
                        path = path.substring(0, path.lastIndexOf("/")) + "/";
                    }
                }
            }
        }
        if(callback){
            callback();
        }
    });
}
 
function initDefaultConfig(){
    if(!configData.backgroundimg ){
        configData.backgroundimg = "style/images/base/login/background-classic.png";
    }
    if(!configData.defaultPhoto){
        configData.defaultPhoto = "style/images/base/login/userphotoman.png";
    }
    if(!configData.loginUrl){
        configData.loginUrl = "framework/loginController/login";
    }
    if(!configData.sessionInfoUrl){
        configData.sessionInfoUrl = "framework/loginController/getSessionInfo";
    }
    if(!configData.logoutUrl){
        configData.logoutUrl = "framework/loginController/logout";
    }
}
 
function initPage(){
    $(document).attr("title",configData.title);
    $("#loginTitle").html(configData.title);
    $("#background img").attr("src", projectName + configData.backgroundimg );
    if ($.cookie('useridcookie') != null && $.cookie('useridcookie') != "" && $.cookie('useridcookie') != "null") {
        $("#userid").val($.cookie('useridcookie'));
    }
    //清除锁定窗口的cookie
    $.cookie("isLockWindow", "false");
    $.cookie('AuthorizationToken','',{path:"/"});
    //设置用户的头像
    if($.cookie('userphotocookie') != null && $.cookie('userphotocookie') != "" && $.cookie('userphotocookie') != "null"){
        $("#userPhoto").attr("src",projectName + $.cookie('userphotocookie'));
    }else{
        $("#userPhoto").attr("src",projectName  + configData.defaultPhoto);
    }
    $("#loginbt").click(function() {
        if (checkEmpty())// 先检查是否为空
            login();
    });
    // 在密码框上点击了回车键
    $("#password").bind("keydown", function(e) {
        var key = e.which;
        if (key == 13){// 按下回车键{
            if (checkEmpty())// 先检查是否为空
                login();
        }
    });
    if(configData.unSecret){
        $("#secretFlag").show();
        $("#secretFlag").html(configData.unSecretText);
        $(".loading").css('margin-top','170px');
    }
}
 
function checkIsLogin(callback){
    crosPost(configData.sessionInfoUrl,{},function(result){
        //如果在线,那直接提示用户是否要继续
        if(result.success){
            $webUtil.deptOid = result.obj.deptOid; // 保存用户部门Oid
            var sessionInfo = result.obj;
            if (window.confirm('当前浏览器中已经使用[' + sessionInfo.userName + ']登录了,如果您现在要使用别的账户登录,'
                    + sessionInfo.userName + '将会自动注销,您是否要继续?')) {
                crosPost(configData.logoutUrl,{"userid":sessionInfo.userId},function(result){
                    window.location.href = window.location.href;
                },function(xhr,error){
                    window.location.href = window.location.href;
                });
            } else {
                if(browserInfo.browser == "IE"){
                    window.open(",’_parent’,");
                    window.close();
                }else{
                    try{
                        window.opener = null;
                        window.open('', '_self');
                    }catch(e){
                        window.close();
                    }
                }
            }
        }else{
            if(callback){
                callback();
            }
        }
    },function(xhr,error){
        if(callback){
            callback();
        }
    });
}
 
function crosPost(url1,data,callback,errorCallback){
    var ajaxOperation ={
        type:'post',
        url:path + url1,
        data:data,
        success:function(data){
            var result = data;
            if(callback){
                callback(result);
            }
        },
        error:function(xhr,error){
            if(errorCallback){
                errorCallback(xhr,error);
            }
        }
    };
    if(configData.isDebug){
        jQuery.support.cors=true;
        // jQuery.ajaxSetup({
        //     xhr: function() {
        //         if (window.ActiveXObject) {
        //             return new window.ActiveXObject("Microsoft.XMLHTTP");
        //         } else {
        //             return new window.XMLHttpRequest();
        //         }
        //     }
        // });
        // ajaxOperation['xhrFields'] = {
        //     withCredentials: true
        // };
        // ajaxOperation['crossDomain']=true;//(true == !(document.all));
    }
    jq.ajax(ajaxOperation);
}
 
// 检查用户名和密码是否为空
function checkEmpty() {
    if (($("#userid").val() == "") && ($("#password").val() == "")) {
        showError("登录账号和密码不能为空");
        return false;
    } else if ($("#userid").val() == "") {
        showError("登录账号不能为空");
        return false;
    } else if ($("#password").val() == "") {
        showError("密码不能为空");
        return false;
    } else
        return true;
}
 
// 显示错误
function showError(text) {
    $("#msg").html( text);
    hideError(5000);
}
 
// 错误显示div隐藏值
function hideError(time) {
    window.setTimeout(function() {
        $("#msg").html("");
    }, time);
}
 
// 登录
function login() {
    $("#loginbt").attr("disabled", true);
    var userid = $("#userid").val();
    if($("#isForceLogin").attr("checked") == "checked"){
        $.cookie('useridcookie',userid,{expires:30});
    }else{
        $.cookie('useridcookie',null);
    }
    //var password = MD5($("#password").val());
    //新平台已经支持md5加密了
    var password = configData.compatibility?$("#password").val():MD5($("#password").val());
    var isForceLogin = $("#isForceLogin").is(":checked");
    showLoading();
    var loginData = {
        userId : userid,
        password : password,
        forceLogin : isForceLogin,
        browser:browserInfo.browser,
        browserversion:browserInfo.browserversion,
        osversion: browserInfo.osversion 
    };
    //因为每个项目,在登录后获取的内容可能会有不一样,所以登录的路径支持配置configData.loginUrl
    crosPost(configData.loginUrl,loginData,function(result){
        hideLoading();
        if(result == undefined || result == null){
            showError("登录出现了错误!请查看服务器是否开启");
            $("#loginbt").attr("disabled", false);
        }    
        if (result.success){
            //$.cookie('AuthorizationToken',result.obj.sessionInfo.token,{path:"/"});
            createCookie('AuthorizationToken',result.obj.sessionInfo.token);
            createCookie("msg","");
            if(configData.mainDisplay == "open" && browserInfo.browser == 'IE'){
                window.open(projectName + "main.html",'_blank','menubar=no,fullscreen=1,toolbar=no,resizable=no,location=no,status=no');
                window.close();
            }else {
                window.location.href = projectName + "main.html";
            }
        }else {
            showError(result.msg);
            $("#loginbt").attr("disabled", false);
        }
    },function(xhr,error){
        hideLoading();
        showError(error); 
    });
}
 
function createCookie(name,value,days) {
    $.cookie(value,name,{path:"/"});
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else{
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}
 
function showLoading(){
    $(".loadbg").show();
    $(".processbar").show();
    $(".loading").show();
    var preWidth = $(".loading").css("width");
    if(preWidth.indexOf("px")>-1){
        preWidth = preWidth.replace("px","");
    }
    $("#useTime").html(usedTime );
    preWidth = preWidth*1/10;
    processInterval = window.setInterval(function(){
        $("#useTime").html(usedTime + 1);
        $(".processbar").css("width",preWidth*(processStep+1));
        usedTime ++;
        processStep ++ ;
        if(processStep == 10){
            processStep = 0;
        }
        if(usedTime>10){
            $(".loading").css('color','rgb(249,0,69)')
        }
    },1000);
}
function hideLoading(){
    $(".loadbg").hide();
    $(".processbar").hide();
    $(".loading").hide().css({color:'#333333'});
    processStep = 0;
    usedTime =0;
    if(processInterval != null){
        window.clearInterval(processInterval);
    }
}