Skip to content

Commit f29f54c

Browse files
committed
添加安装程序
1 parent cc8f40e commit f29f54c

File tree

6 files changed

+301
-1
lines changed

6 files changed

+301
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.DS_*
22
ant_db_data/
33
web/node_modules/
4-
4+
.vscode/

web/addons/ant.install/client.js

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
;(function($) {
2+
var input_style = 'style="width: 90%;"';
3+
4+
var html_header = '<div id="setting_about" style="font-size: 16px;">' +
5+
' <div>' +
6+
' <h3 style="padding: 9px 0;" class="text-success">安装确认</h3>' +
7+
' <p style="line-height:24px;">下面是你安装配置信息, 请确认无误后, 点击上方「确定并提交」按钮</p><hr>';
8+
var html_install_info = {};
9+
var html_footer = '<hr><h3 style="padding: 5px 0;" class="text-info">联系我们</h3>' +
10+
' <p style="line-height:24px;">在线交流: <a target="_blank" href="https://discord.gg/Uzh5nUf">https://discord.gg/Uzh5nUf</a><br>GitHub: <a href="http://github.com/antswordproject/ant" target="_blank">http://github.com/antswordproject/ant</a></p>' +
11+
' <h3 style="padding: 5px 0;" class="text-danger">免责声明</h3>' +
12+
' <p style="line-height:24px;">本站仅提供一个学习与交流的平台,请勿用于其他非法用途,否则删除帐号并自行承担由此带来的风险!</p>' +
13+
' </div>' +
14+
'</div>';
15+
16+
var noxss = function (str) {
17+
return str.replace(/&/g, "&amp;")
18+
.replace(/>/g, "&gt;")
19+
.replace(/</g, "&lt;")
20+
.replace(/"/g, "&quot;");
21+
};
22+
23+
var install_keys = ['create_admin'];
24+
25+
var ADDON = {
26+
ui: {
27+
create_admin: {
28+
parent_step: '',
29+
next_step: 'submit_form',
30+
name: 'create_admin',
31+
header: '创建管理员',
32+
fields: [{
33+
type: 'email',
34+
field: 'email',
35+
required: true,
36+
html: {
37+
caption: '邮箱',
38+
attr: input_style
39+
}
40+
}, {
41+
type: 'text',
42+
field: 'nickname',
43+
required: true,
44+
html: {
45+
caption: '昵称',
46+
attr: input_style
47+
}
48+
}, {
49+
type: 'password',
50+
field: 'password',
51+
required: true,
52+
html: {
53+
caption: '密码',
54+
attr: input_style
55+
}
56+
}, {
57+
type: 'password',
58+
field: 'password1',
59+
required: true,
60+
html: {
61+
caption: '重复密码',
62+
attr: input_style
63+
}
64+
}],
65+
record: JSON.parse(localStorage.getItem('create_admin')) || {},
66+
toolbar: {
67+
items: [{
68+
type: 'button',
69+
caption: '下一步',
70+
icon: 'fa fa-step-forward',
71+
onClick: function () {
72+
var self = w2ui['create_admin'];
73+
if (self.validate().length === 0) {
74+
if (self.record.password.length < 6) {
75+
$('#password').val('').focus();
76+
return ADDON.warning('密码长度不得少于6位!');
77+
}
78+
if (self.record.password !== self.record.password1) {
79+
$("#password1").val('').focus();
80+
return ADDON.warning('两次输入的密码不一致!');
81+
}
82+
var _t_html = '';
83+
_t_html += '<h4 style="padding: 5px 0;" class="text-info" style="color:#e24444">管理员</h4>';
84+
_t_html += '<p style="margin-left: 20px;line-height:24px;">邮箱: ' + noxss(self.record.email) + '</p>';
85+
_t_html += '<p style="margin-left: 20px;line-height:24px;">昵称: ' + noxss(self.record.nickname) + '</p>';
86+
html_install_info['create_admin'] = _t_html;
87+
w2ui.submit_form.formHTML= '<div class="w2ui-page page-0">' + html_header + (Object.values(html_install_info).join("")) + html_footer + '</div>';
88+
w2ui.submit_form.reload();
89+
localStorage.setItem('create_admin', JSON.stringify(self.record));
90+
w2ui.sidebar.disable('install_' + self.name)
91+
w2ui.sidebar.enable('install_' + self.next_step)
92+
w2ui.sidebar.click('install_' + self.next_step)
93+
};
94+
}
95+
}, {
96+
type: 'break'
97+
}]
98+
}
99+
},
100+
submit_form: {
101+
parent_step: 'create_admin',
102+
next_step: '',
103+
name: 'submit_form',
104+
header: '确认信息',
105+
formHTML: (
106+
html_header + (Object.values(html_install_info).join("")) + html_footer
107+
),
108+
toolbar: {
109+
items: [{
110+
type: 'button',
111+
caption: '上一步',
112+
icon: 'fa fa-step-backward',
113+
onClick: function () {
114+
var self = w2ui['submit_form'];
115+
w2ui.sidebar.disable('install_' + self.name)
116+
w2ui.sidebar.enable('install_' + self.parent_step)
117+
w2ui.sidebar.click('install_' + self.parent_step)
118+
}
119+
}, {
120+
type: 'button',
121+
caption: '确定并提交',
122+
icon: 'fa fa-save',
123+
onClick: function () {
124+
var self = w2ui['submit_form'];
125+
var post_data = {};
126+
install_keys.map(function (key) {
127+
var temp = JSON.parse(localStorage.getItem(key)) || {};
128+
post_data[key] = temp;
129+
});
130+
ADDON.lock('安装中..')
131+
$.post('/addons/ant.install/install', post_data, function (data) {
132+
ADDON.unlock();
133+
if (data.ret) {
134+
ADDON.success('安装成功!');
135+
localStorage.setItem('login_user', post_data['create_admin']['email']);
136+
install_keys.map(function (key) { // 移除本地存储
137+
localStorage.removeItem(key);
138+
});
139+
// 重新加载页面
140+
window.location.href= window.location.origin;
141+
}else{
142+
ADDON.error('安装失败!<br>' + data.err);
143+
}
144+
})
145+
}
146+
}]
147+
}
148+
}
149+
},
150+
init: function () {
151+
$().w2form(this.ui.create_admin);
152+
$().w2form(this.ui.submit_form);
153+
ANT.addonLoaded.reg(function () {
154+
w2ui['sidebar'].click('install_create_admin');
155+
});
156+
}
157+
};
158+
159+
ANT.initAddon({
160+
id: 'ant_install',
161+
text: '系统安装',
162+
group: true,
163+
expanded: true,
164+
nodes: [{
165+
id: 'install_create_admin',
166+
text: '创建管理员',
167+
icon: 'fa fa-user',
168+
disabled: false,
169+
onClick: function () {
170+
ADDON.content(w2ui['create_admin']);
171+
}
172+
}, {
173+
id: 'install_submit_form',
174+
text: '确认信息',
175+
icon: 'fa fa-check',
176+
disabled: true,
177+
onClick: function () {
178+
ADDON.content(w2ui['submit_form']);
179+
}
180+
}]
181+
}, ADDON);
182+
183+
ANT.addonLoaded.reg(function() {
184+
ANT.ROUTE.run();
185+
})
186+
})(jQuery);

web/addons/ant.install/client.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/addons/ant.install/route.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
'use strict'
3+
4+
const fs = require('fs');
5+
6+
module.exports = function(app, db, fc) {
7+
var path = '/addons/ant.install';
8+
//@客户端脚本
9+
app.route(path + '/client.js')
10+
.get(function(req, res) {
11+
fc.sendFile(req, res, __dirname + '/client.min.js');
12+
})
13+
app.route(path + '/install')
14+
.post(function(req, res) {
15+
if(fc.isinstall()) {
16+
// 已经安装
17+
res.json({
18+
err: '已安装过,请不要重复安装',
19+
ret: false
20+
});
21+
return
22+
}
23+
// 创建管理员
24+
var create_admin = req.body.create_admin || {};
25+
if(Object.keys(create_admin).length > 0) {
26+
var mdb = db.get('user');
27+
var _email = fc.toStr(create_admin.email),
28+
_nickname = fc.toStr(create_admin.nickname),
29+
_passwd = fc.md5(fc.toStr(create_admin.password)),
30+
_passwd1 = fc.md5(fc.toStr(create_admin.password1));
31+
if(_passwd !== _passwd1) {
32+
res.json({
33+
err: '创建管理员失败, 两次密码不一致',
34+
ret: false
35+
})
36+
return
37+
}
38+
mdb.findOne({
39+
email: _email
40+
}, function(err, ret) {
41+
if(!ret) { // 邮箱不存在
42+
mdb.findOne({
43+
nickname: _nickname
44+
}, function(err1, ret1) {
45+
if(!ret1){
46+
// 开始注册
47+
new mdb({
48+
email: _email,
49+
coin: 1000,
50+
isadmin: true,
51+
verify: true,
52+
nickname: _nickname,
53+
password: _passwd,
54+
config: '',
55+
regip: fc.getIP(req),
56+
loginip: fc.getIP(req),
57+
regtime: new Date(),
58+
logintime: new Date(),
59+
buy_bomb: true
60+
}).save(function(err2, ret2){
61+
if(!ret2){
62+
res.json({
63+
err: err2,
64+
ret: false
65+
});
66+
return;
67+
}
68+
})
69+
}else{
70+
res.json({
71+
err: '昵称已存在!',
72+
ret: false
73+
})
74+
return;
75+
}
76+
})
77+
}else{
78+
res.json({
79+
err: '邮箱已存在!',
80+
ret: false
81+
})
82+
return;
83+
}
84+
});
85+
}else{
86+
// 未获取管理员
87+
res.json({
88+
err: '未创建管理员',
89+
ret: false,
90+
})
91+
return;
92+
}
93+
fs.writeFileSync('install.lock', '1');
94+
res.json({
95+
ret: true
96+
})
97+
})
98+
}

web/modules/func.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
var fs = require('fs')
55

66
var FC = {
7+
isinstall: function() {
8+
return fs.existsSync('install.lock');
9+
},
710
//@ 判断是否登录
811
islogin: function(req, res, success, error) {
912
if (req.session.login === true) {

web/modules/route.js

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ module.exports = function(app, handler) {
4444
'ant.cache',
4545
'ant.login.user',
4646
'ant.login.other'
47+
],
48+
install: [
49+
'ant.install'
4750
]
4851
}
4952
addons.login.forEach(function(i) {
@@ -58,9 +61,18 @@ module.exports = function(app, handler) {
5861
addons.admin.forEach(function(i) {
5962
require('../addons/' + i + '/route.js')(app, db, fc);
6063
});
64+
65+
addons.install.forEach(function(i) {
66+
require('../addons/' + i + '/route.js')(app, db, fc);
67+
});
68+
6169
//- 插件列表
6270
app.route('/addons')
6371
.get(function(req, res) {
72+
if (!fc.isinstall()) {
73+
res.send(addons.install)
74+
return
75+
}
6476
fc.islogin(req, res,
6577
function(user) {
6678
res.send(user.isadmin ? addons.system.concat(addons.admin) : addons.system)

0 commit comments

Comments
 (0)