js

$(function () {


//事件处理
var portal = {
baseUrl: sysConfig.ucenterUrl,
user: {
userCode: "",
userName: "",
neo4jId: null,
graphicUrl: "",
sessionId: ""
},
ready: false
};

//显示登录框
portal.showLogin = function () {
$('form[name="loginForm"]')[0].reset();
$("#loginModal").show();
};
//显示
portal.setCurrentAppModule = function (appId) {
$("#appModule li>a").removeClass('cur');
$("#appModule li>a[data-appid='" + appId + "']").addClass('cur');
};

/**
* 用户登录
* @param loginName
* @param pw
*/
portal.login = function (loginName, pw, rememberMe, CallBack) {
$.ajax({
url: this.baseUrl + "/sys/login",
data: JSON.stringify({ "loginName": loginName, "password": pw, "rememberMe": rememberMe }),
type: "POST",
async: false,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data, textStatus) {
if (data.success) {
portal.user.userCode = data.loginUser.code;
portal.user.userName = data.loginUser.name;
portal.user.graphicUrl = data.loginUser.graphicUrl;
portal.user.neo4jId = data.loginUser.neo4jId;
portal.user.sessionId = data.loginUser.sessionId;
portal.writeCookie();
}
CallBack(data, textStatus);
},
error: function (data) {
console.log(data);
}
});
};


/**
* 显示用户
*/
portal.showUser = function () {
if (portal.user.userCode) {
$(".user-nologin").hide();
$(".user-login.header-userinfo span span:first").html(portal.user.userName);
$(".user-login").show();
$("#ulogout").show();
} else {
$(".user-login").hide();
$("#ulogout").hide();
$(".user-nologin").show();
}
};
/**
* 登录成功后将信息写入Cookie
*/
portal.writeCookie = function () {
$.cookie("web.session.id", portal.user.sessionId, { path: '/' });
$.cookie("sessionId", portal.user.sessionId, { path: '/' });
$.cookie("user", JSON.stringify(portal.user), { path: '/' });
};

/**
* 清理Cookie
*/
portal.clearCookie = function () {
$.cookie("web.session.id", "", { expires: -1 })
$.cookie("sessionId", "", { expires: -1 })
$.cookie("user", "", { expires: -1 })
};

//获取门户列表
portal.getPortals = function (userCode, CallBack) {
$.ajax({
url: this.baseUrl + "/exgroup/getGroupsByUser?userCode=" + userCode,
type: "GET",
dataType: 'json',
async: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.success) {
$("#org").attr("title", '科技北斗');
$("#org").children('option').remove();

var checkeCode = false;
//获取企业编号
var eCode = location.href.getUrlParam('enterpriseCode');
if (eCode == '1' || eCode == "" || eCode == undefined)
{
$("#org").append('<option data-logo="images/logo.png" selected="selected" value = "1">科技北斗</option>');
checkeCode = true;
}
else
{
$("#org").append('<option data-logo="images/logo.png" value = "1">科技北斗</option>');
}
var groupBaseList = data.groupBaseList;
for (var i = 0; i < groupBaseList.length; i++)
{
var orgItem = groupBaseList[i];
if (checkeCode) {
$("#org").append('<option data-ustate="' + orgItem.admin + '" data-logo="' + orgItem.head + '"title="' + orgItem.name + '" value = "' + orgItem.id + '">' + orgItem.name + '</option>');
}
else
{
if (eCode == orgItem.id)
{
$("#org").append('<option selected="selected" data-ustate="' + orgItem.admin + '" data-logo="' + orgItem.head + '"title="' + orgItem.name + '" value = "' + orgItem.id + '">' + orgItem.name + '</option>');
checkeCode = true;
}
}
}
}
CallBack(data);
}
});
};

/**
* 获取当前用户信息,包含所在的门户,所在的应用
*/
portal.getCurrentUser = function () {
var currentUser = {};
currentUser.user = portal.user;

currentUser.portal = {};
currentUser.portal.groupId = $("#org").val();
currentUser.portal.groupName = $("#org").text().trim();

currentUser.appModule = {};
currentUser.appModule.moduleId = $("#appModule li>a.cur").attr("data");
currentUser.appModule.moduleName = $("#appModule li>a.cur").text().trim();

return currentUser;
};

/**
* 获取模块列表
* @param orgId
*/
portal.getAppModules = function (orgId, CallBack) {
$.ajax({
url: this.baseUrl + "/app/queryGroupApps?enterpriseCode=" + orgId,
type: "GET",
dataType: 'json',
async: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log("ucenter::getAppModulees::" + data.success);
if (data.success) {
$("#appModule").children('li').remove();
$("#searchModule").children('option').remove();
var appList = data.appSelectList;
for (var i = 0; i < appList.length; i++) {
var appItem = appList[i];
// 选中模块
if (CurrentModule == appItem.appId)
{
$("#appModule").append('<li><a class="cur" data-appid="' + appItem.appId + '" href="' + appItem.url + '?enterpriseCode=' + orgId + '" target="_blank">' + appItem.appName + '</a></li>');
$("#searchModule").append('<option selected="selected" data-searchurl="' + appItem.searchUrl + '?enterpriseCode=' + orgId + '" value = "' + appItem.appId + '">' + appItem.appName + '</option>');
} else {
$("#appModule").append('<li><a data-appid="' + appItem.appId + '" href="' + appItem.url + '?enterpriseCode=' + orgId + '" target="_blank">' + appItem.appName + '</a></li>');
$("#searchModule").append('<option data-searchurl="' + appItem.searchUrl + '?enterpriseCode=' + orgId + '" value = "' + appItem.appId + '">' + appItem.appName + '</option>');
}


//if ("true" == $("#org").find("option:selected").attr("data-ustate")) {
// $("#searchModuleAdd").show();
//} else {
// $("#searchModuleAdd").hide();
//}
}

//// 选择企业
//var currentOrgId = window.location.href.getUrlParam("orgId");
//if (currentOrgId) {
// $("#org").val(currentOrgId);
//}

// 事件触发
//if (appList.length > 0) {
// console.log("ucenter::getAppModulees::trigger::1");
// $.event.trigger({
// type: "HeaderCompleteEvent",
// message: {
// orgId: $("#org").val(),
// appModule: appList[0].appId
// }
// });
//} else {
// console.log("ucenter::getAppModulees::trigger::undefined");
// $.event.trigger({
// type: "HeaderCompleteEvent",
// message: {
// orgId: $("#org").val(),
// appModule: undefined
// }
// });
//}
CallBack(data);
}
}
});
};

/**
* 是否active
*/
portal.isActive = function (CallBack) {
$.ajax({
url: this.baseUrl + "/sys/isActive",
type: "GET",
async: false,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: CallBack
});
};

/**
* 从Cookie中获取用户信息
*/
portal.readCookie = function () {
if ($.cookie("user"))
portal.user = JSON.parse($.cookie("user"));
};

/**
* 用户登出
*/
portal.logout = function (CallBack) {
$.ajax({
url: this.baseUrl + "/sys/logout",
type: "GET",
async: false,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
portal.clearCookie();
portal.user = {};
window.location.href = "/index.html";
CallBack(data)
}
});
};


portal.init = function () {
portal.isActive(function (data) {
if (data.success) {
console.log("用户依然在线");
//$("#loginBtndiv").hide();
//$("#showNamediv").show();
} else {
console.log("用户离线,清除用户信息");
portal.clearCookie();
portal.user.userCode = "";
//$("#showNamediv").hide();
//$("#loginBtndiv").show();
}
// 如果cookie中有值,尝试从cookie中读取用户信息。
portal.readCookie();
// 初始化登录信息
portal.showUser();

//if(portal.user.userCode){
// 获取用户门户
portal.getPortals(portal.user.userCode, function (data) {
if (data.success) {
console.log("ucenter::init::getPortals");

//// 选中当前企业
//var eCode = location.href.getUrlParam('enterpriseCode');
//if (eCode) {
// $('#org').val(eCode);
//}
// 获取门户应用模块
portal.getAppModules($("#org").val(), function () {
portal.ready = true;
});


}
});
//}
});

};
//验证是否登录
portal.checkLogin = function () {
var result = false;
portal.isActive(function (data) {
if (data.success) {
result=true;
}
else
{
portal.showLogin();
result=false;
}
})
return result;
}


portal.init();
window.portal = portal;

 

 

 

// 绑定登录事件
$("#ulogin").click(portal.showLogin);

// 关闭
$("#loginModal .close").click(close);
function close() {
$("#loginModal").hide();
}

// 登录事件
$("#loginBtn").click(
function(){
login();
});

//登录内部方法
function login() {
var loginName = $("#loginName").val();
var password = $("#password").val();
var rememberMe = $("#rememberMe").val();
portal.login(loginName, password, rememberMe, function (data) {
if (data.success) {
portal.showUser();
$("#loginModal").hide();

// 获取用户门户
portal.getPortals(portal.user.userCode, function (data) {
if (data.success) {
console.log("ucenter::login::getPortals");
// 获取门户应用模块
portal.getAppModules(portal.getCurrentUser().portal.groupId, function () {
console.log("ucenter::login::success");
$.event.trigger({
type: "LoginEvent",
message: {
}
});
}, function () { });
}
});
} else {
$("#errorMsg .error").html(data.error);
$("#errorMsg").show();
}
});
};


//更改url参数
function changeURLPar(destiny, par, par_value) {
var pattern = par + '=([^&]*)';
var replaceText = par + '=' + par_value;
if (destiny.match(pattern)) {
var tmp = '/\\' + par + '=[^&]*/';
tmp = destiny.replace(eval(tmp), replaceText);
return (tmp);
}
else {
if (destiny.match('[\?]')) {
return destiny + '&' + replaceText;
}
else {
return destiny + '?' + replaceText;
}
}
return destiny + '\n' + par + '\n' + par_value;
}

// 门户选择时
$("#org").change(function () {
//$(".nlogo img").attr("src", $("#org").find("option:selected").attr("data-logo"));
//$("#org").attr("title", $("#org").text().trim());

var url = "http://" + window.location.host + indexUrl;
if ($("#org").val())
{
window.location.href = changeURLPar(url, "enterpriseCode", $("#org").val());
}
else {
window.location.href = changeURLPar(url, "enterpriseCode", "1");
}

});


// 登出事件
$("#ulogout").click(function () {
portal.logout(function () {
console.log("ucenter::logout::success");
$.event.trigger({
type: "LogOutEvent",
message: {
}
});
});
});

// 注册跳转
$("#uregister").click(showRegister);
function showRegister() {
window.location.href = "/reg.html#/cell";
};


// 手机二维码浮动
$(".nwarp .phone").hover(function () {
$('.nwarp .phone div').css('display', 'block');
},
function () {
$('.nwarp .phone div').css('display', 'none');
});
//个人信息
$("#uinfo").click(function () {
window.location.href = "/userInfo.html#/base";
});
$("#uattention").click(function () {
window.location.href = "userInfo.html#/attention/1";
});
$("#ucollection").click(function () {
window.location.href = "userInfo.html#/collection";
});

// input变动
$("#loginModal input").change(loginInfoChange);
function loginInfoChange() {
$("#errorMsg").hide();
};

// 头像菜单下拉
$("#userInfoSpan").hover(function () {
$('#menu').css('display', 'block');
},function () {
$('#menu').css('display', 'none');
});


// 搜索功能
$('#searchInput').bind('keypress', function (event) {
if (event.keyCode == "13") {
search();
}
});
$('form[name="searchForm"] .ninput-group button').click(search);
function search() {
var query = $("#searchInput").val();
if (query) {
return;
}
var searchUrl = $("#searchModule").find("option:selected").attr("data-searchurl");
var app = $("#searchModule").val();
window.open(portal.baseUrl + searchUrl + "&query=" + query);
};


})

 

posted @ 2016-08-17 17:05  haifeng_0712  阅读(168)  评论(0编辑  收藏  举报