-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
171 lines (154 loc) · 5.75 KB
/
index.js
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
importLayui(function () {
require(["css!plugin/css/custom"]);
var lastAjax = [];
window.doLogin = function (callback) {
$('.loading-wrap').hide();
$('.login-wrap').show();
lastAjax.push(callback);
};
require(["hsForm"], function (hsForm) {
hsForm.init();
});
var $ = layui.jquery;
var element = layui.element;
var form = layui.form;
form.on('submit(test1)', function (data) {
console.log(layui.hsForm.format(data.field));
return false;
});
//自定义配置项
var AppConfig = {
footer: false
};
loadUserTree();
function loadUserTree() {
require(["request"], function (r) {
r.get("menu/user-own/tree", function (e) {
if (e.status === 200) {
initTopMenu(e.result);
element.init();
$($("#top-menu").find("a")[0]).click();
$('.loading-wrap').hide();
$(".main").fadeIn(200)
}
});
})
}
//监听登录
form.on('submit(loginForm)', function (data) {
require(["request"], function (r) {
try {
r.post("authorize/login", {username: data.field.username, password: data.field.password, token_type: "jwt"}, function (e) {
if (e.status === 200) {
layui.sessionData("hsweb-token", {key: "accessToken", value: e.result.token});
//使用后清空
$(lastAjax).each(function () {
this();
});
lastAjax = [];
//隐藏login
$('.login-wrap').hide();
loadUserTree();
} else {
layer.alert(e.message === '{password_error}' ? "密码错误" : e.message, {zIndex: 999999999});
}
// console.log(e);
}, false);
} catch (e) {
console.log(e)
}
});
return false;
});
function initTopMenu(menus) {
var topMenus = $("#top-menu");
topMenus.children().remove();
$(menus).each(function () {
var parent = $("<li>");
parent.addClass("layui-nav-item");
var parentNode = this;
if (this.children && this.children.length > 0) {
var menuLink = $("<a>").attr({"href": "javascript:void(0)"}).text(parentNode.name);
var childrenNode = this.children;
parent.append(menuLink);
if (this.children[0].children && this.children[0].children.length > 0 && this.children[0].children[0].children) {
var dl = $("<dl>").addClass("layui-nav-child");
var hasChildren = false;
$(this.children[0].children).each(function () {
// if(this.children&&this.children.length>0){
// hasChildren=true;
dl.append($("<dd>").append($("<a>").text(this.name)));
// }
});
// if(hasChildren)
parent.append(dl);
} else {
menuLink.on("click", function () {
initLeftMenu(childrenNode);
});
}
}
topMenus.append(parent);
});
}
function loadMenu(menu) {
if ($("[lay-id=" + menu.id + "]").length === 0) {
layui.element.tabAdd('tabs', {
title: menu.name
, content: '<div id="tools-' + menu.id + '"></div>' +
'<div lay-filter="' + menu.id + '" id="container-' + menu.id + '">' +
'</div>'
, id: menu.id
});
require([menu.url], function (page) {
page.init(menu.id);
});
}
layui.element.tabChange('tabs', menu.id);
}
$(".sign-out").on('click', function () {
layer.confirm("确认退出本系统?", {btn: ['退出','取消']},function () {
layer.closeAll();
require(["request"], function (r) {
r.get("authorize/exit", function () {
layui.sessionData("hsweb-token", {key: "accessToken", value: null});
doLogin(function () {
});
})
});
return true;
});
});
function initLeftMenu(menus) {
var leftMenu = $("#left-menu");
leftMenu.children().remove();
$(menus).each(function () {
var menuEl = $("<li>");
menuEl.addClass("layui-nav-item");
var parentNode = this;
var menuLink = $("<a>").text(this.name);
menuEl.append(menuLink);
if (parentNode.children && parentNode.children.length > 0) {
var dl = $("<dl>");
dl.addClass("layui-nav-child");
$(parentNode.children).each(function () {
var link = $("<a>").text(this.name).attr("href", "javascript:void(0)");
var m = this;
link.on("click", function () {
loadMenu(m)
});
dl.append($("<dd>").append(link));
});
menuEl.append(dl);
}
leftMenu.append(menuEl);
});
$(leftMenu.children()[0]).addClass("layui-nav-itemed");
layui.element.init();
}
//读取配置信息
if (!AppConfig.footer) {
$('.layui-footer').hide();
$('.layui-body').css('bottom', '0');
}
});