Skip to content

Commit

Permalink
enabled data setting
Browse files Browse the repository at this point in the history
  • Loading branch information
sam authored and sam committed Dec 23, 2015
1 parent 6f0e60b commit 28f53cd
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 73 deletions.
1 change: 1 addition & 0 deletions Makefile
@@ -1,4 +1,5 @@
run:
rm -rf dist
gulp run
node dist/cli.js
test:
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Expand Up @@ -84,7 +84,7 @@ gulp.task('clean', function () {

gulp.task('watch', function() {
gulp.watch(frontends, ['copy']);
gulp.watch(backends, ['nsp', 'babel', 'test']);
gulp.watch(backends, ['nsp', 'babel', 'test', 'copy']);
});

gulp.task('prepublish', ['nsp', 'babel']);
Expand Down
10 changes: 10 additions & 0 deletions lib/cli.js
Expand Up @@ -3,6 +3,7 @@
var meow = require('meow');
var path = require('path');
var express = require('express');
var session = require('./mySession');


var cli = meow({
Expand All @@ -25,6 +26,15 @@ var statics = path.resolve(path.dirname(dir), 'statics');
var views = path.resolve(path.dirname(dir), 'views');

http.use('/assets', express.static(statics));
http.use(function(req, res, next) {
var id = session.get(null, 'id');
console.log(id);
if (!id && req.path !== '/config/app') {
res.redirect('/config/app');
}
next();
});

http.set('views', views);

http.set('view engine', 'ejs'); // register the template engine
Expand Down
7 changes: 7 additions & 0 deletions lib/mySettings.js
Expand Up @@ -4,6 +4,9 @@ var settingsConf = {
};

function get(id, key) {
console.log('get');

console.log(id, key, settingsConf);
if (settingsConf[id] && settingsConf[id][key]) {
return settingsConf[id][key];
}
Expand All @@ -15,7 +18,11 @@ function set(id, key, value) {
settingsConf[id] = {};
}
settingsConf[id][key] = value;
console.log(id, key, value, settingsConf);
console.log('set');

}

settings.registerSet(set);
settings.registerGet(get);
export default settings;
135 changes: 72 additions & 63 deletions lib/router/express.js
Expand Up @@ -3,6 +3,7 @@ var express = require('express');
var Router = express.Router;

var session = require('../mySession');
var settings = require('../mySettings');


function api() {
Expand All @@ -14,7 +15,7 @@ function api() {
function template() {
var router = new Router();
router.all('/jssdk', function (req, res) {
var host = session.get(req, 'host');
var host = session.get(null, 'host');
res.render('jssdk-main', {
host: host
});
Expand All @@ -38,73 +39,81 @@ function template() {
function config() {
var router = new Router();
router.all('/:type', function (req, res) {
console.log(req.body);
console.log(req.query);
var app = session.get(req, 'app') || {};
if (!app.id) {
if (req.body) {
console.log(body);
session.set(req, 'app', req.body);
app = req.body;
}
}
var type = 'app';
var file = 'index';
var host = session.get(null, 'host') || '';
var prefix = session.get(null, 'prefix') || 'weixin';
var id = session.get(null, 'id');
var data = {};
console.log(req.params);
switch (req.params.type) {
case 'host':
var host = session.get(req, 'host');
if (req.body.host) {
host = req.body.host;
session.set(req, 'host', host);
session.set(null, 'host', host);
}
var prefix = session.get(req, 'prefix') || 'weixin';
console.log(prefix);
console.log(host);
var urls = {
auth: {
ack: host + '/' + prefix + '/auth/ack'
},
jssdk: {
config: host + '/' + prefix + '/jssdk/config'
},
oauth: {
access: host + '/' + prefix + '/oauth/access',
success: host + '/' + prefix + '/oauth/success',
redirect: host + '/page/oauth'
},
pay: {
callback: host + '/' + prefix + '/pay/callback',
redirect: host + '/page/pay'
data.updated = false;

var type = req.params.type;
if (id) {
switch (req.params.type) {
case 'host':
if (req.body.host) {
host = req.body.host;
settings.set(id, 'host', host);
}
};
session.set(req, 'urls', urls);
session.set(null, 'urls', urls);
data.urls = urls || {auth:{}, oauth:{}, pay: {}};
break;
case 'app':
case 'merchant':
case 'oauth':
if (req.body) {
session.set(req, type, req.body);
}
break;
}
var app = session.get(req, 'app') || {};
if (!app.id) {
type = 'app';
var urls = {
auth: {
ack: host + '/' + prefix + '/auth/ack'
},
jssdk: {
config: host + '/' + prefix + '/jssdk/config'
},
oauth: {
access: host + '/' + prefix + '/oauth/access',
success: host + '/' + prefix + '/oauth/success',
redirect: host + '/page/oauth'
},
pay: {
callback: host + '/' + prefix + '/pay/callback',
redirect: host + '/page/pay'
}
};
settings.set(id, 'urls', urls);
data.urls = urls || {auth: {}, oauth: {}, pay: {}};
break;
case 'app':
if (Object.keys(req.body).length !== 0) {
id = req.body.id;
session.set(null, 'id', id);
}
case 'merchant':
case 'message':
case 'pay':

if (Object.keys(req.body).length !== 0) {
data.updated = false;
console.log('setting');
console.log(type);
settings.set(id, type, req.body);
}
break;
}
} else {
type = req.params.type;
type = 'app';
switch (req.params.type) {
case 'app':
if (Object.keys(req.body).length !== 0) {
id = req.body.id;
session.set(null, 'id', id);
data.updated = false;
settings.set(id, type, req.body);
}
}
}
file = 'config/' + type;


data.host = session.get(req, 'host');
data.urls = session.get(req, 'urls');
data[type] = session.get(req, type);
console.log(file);
if (Object.keys(req.body).length !== 0) {
console.log(req.body);
console.log();
data.updated = true;
}
var file = 'config/' + type;
data.host = host;
data.type = type;
data.urls = settings.get(id, 'urls') || {};
console.log(id, type);
data[type] = settings.get(id, type) || {};
console.log(data);
res.render(file, data);
});
Expand Down
4 changes: 3 additions & 1 deletion lib/views/config/app.ejs
Expand Up @@ -6,6 +6,9 @@
<% include ../left-sidebar.ejs %>
</div>
<div class="col-md-8 col-xs-12">
<% if (updated) { %>
<div class="alert alert-success" role="alert"><%=type%>信息已经保存!<%=updated%></div>
<% } %>
<form class="form-horizontal" method="post">
<div class="form-group">
<label for="id">App Id</label>
Expand All @@ -27,4 +30,3 @@
</div>
</div>
<% include ../footer.ejs %>

10 changes: 6 additions & 4 deletions lib/views/config/host.ejs
Expand Up @@ -6,6 +6,9 @@
<% include ../left-sidebar.ejs %>
</div>
<div class="col-md-8 col-xs-12">
<% if (updated) { %>
<div class="alert alert-success" role="alert"><%=type%>信息已经保存!<%=updated%></div>
<% } %>
<form class="form-horizontal" method="post">
<div class="form-group">
<label for="host">主机域名(如:weixin.qq.com)</label>
Expand All @@ -28,19 +31,19 @@
<div class="form-group">
<label class="col-sm-2 control-label">Oauth访问地址</label>
<div class="col-sm-10">
<p class="form-control-static"><a href="<%=urls.access%>" target="_blank"><%=urls.access%></a></p>
<p class="form-control-static"><a href="<%=urls.oauth.access%>" target="_blank"><%=urls.oauth.access%></a></p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Oauth访问成功请求地址</label>
<div class="col-sm-10">
<p class="form-control-static"><a href="<%=urls.success%>" target="_blank"><%=urls.success%></a></p>
<p class="form-control-static"><a href="<%=urls.oauth.success%>" target="_blank"><%=urls.oauth.success%></a></p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Oauth成功重定向页面地址</label>
<div class="col-sm-10">
<p class="form-control-static"><a href="<%=urls.redirect%>" target="_blank"><%=urls.redirect%></a></p>
<p class="form-control-static"><a href="<%=urls.oauth.redirect%>" target="_blank"><%=urls.oauth.redirect%></a></p>
</div>
</div>
<div class="form-group">
Expand All @@ -62,4 +65,3 @@
</div>
</div>
<% include ../footer.ejs %>

7 changes: 5 additions & 2 deletions lib/views/config/merchant.ejs
Expand Up @@ -6,14 +6,17 @@
<% include ../left-sidebar.ejs %>
</div>
<div class="col-md-8 col-xs-12">
<% if (updated) { %>
<div class="alert alert-success" role="alert"><%=type%>信息已经保存!<%=updated%></div>
<% } %>
<form class="form-horizontal" method="post">
<div class="form-group">
<label for="id">Mercahnt Id</label>
<input type="text" name="id" class="form-control" id="id" placeholder="Merchant Id">
<input type="text" name="id" class="form-control" id="id" placeholder="Merchant Id" value="<%=merchant.id%>">
</div>
<div class="form-group">
<label for="key">Mercahnt Key</label>
<input type="text" name="key" class="form-control" id="key" placeholder="Merchant key">
<input type="text" name="key" class="form-control" id="key" placeholder="Merchant key" value="<%=merchant.key%>">
</div>
<hr/>
<button type="submit" class="btn btn-default btn-block btn-success">Submit</button>
Expand Down
5 changes: 4 additions & 1 deletion lib/views/config/message.ejs
Expand Up @@ -6,10 +6,13 @@
<% include ../left-sidebar.ejs %>
</div>
<div class="col-md-8 col-xs-12">
<% if (updated) { %>
<div class="alert alert-success" role="alert"><%=type%>信息已经保存!<%=updated%></div>
<% } %>
<form class="form-horizontal" method="post">
<div class="form-group">
<label for="aes">Encoding AES Key</label>
<input type="text" name="aes" class="form-control" id="aes" placeholder="Encoding AES Key">
<input type="text" name="aes" class="form-control" id="aes" placeholder="Encoding AES Key" value="<%=message.aes%>">
</div>
<hr/>
<button type="submit" class="btn btn-default btn-block btn-success">Submit</button>
Expand Down
3 changes: 3 additions & 0 deletions lib/views/config/oauth.ejs
Expand Up @@ -4,6 +4,9 @@
<input type="text" name="state" class="form-control" id="state" placeholder="State">
</div>
<div class="form-group">
<% if (updated) { %>
<div class="alert alert-success" role="alert"><%=type%>信息已经保存!<%=updated%></div>
<% } %>
<label for="state">scope</label>
<input type="text" name="state" class="form-control" id="state" placeholder="State">
</div>
Expand Down
3 changes: 3 additions & 0 deletions lib/views/config/pay.ejs
Expand Up @@ -6,6 +6,9 @@
<% include ../left-sidebar.ejs %>
</div>
<div class="col-md-8 col-xs-12">
<% if (updated) { %>
<div class="alert alert-success" role="alert"><%=type%>信息已经保存!<%=updated%></div>
<% } %>
<form class="form-horizontal" method="post">
<div class="form-group">
<label for="id">私钥文件(p12)</label>
Expand Down
2 changes: 1 addition & 1 deletion lib/views/left-sidebar.ejs
Expand Up @@ -4,8 +4,8 @@

<!-- List group -->
<ul class="list-group">
<li class="list-group-item"><a href="/config/host">域名配置</a></li>
<li class="list-group-item"><a href="/config/app">App基本信息配置</a></li>
<li class="list-group-item"><a href="/config/host">域名配置</a></li>
<li class="list-group-item"><a href="/config/merchant">商户信息配置</a></li>
<li class="list-group-item"><a href="/config/pay">支付配置</a></li>
<li class="list-group-item"><a href="/config/message">消息配置</a></li>
Expand Down

0 comments on commit 28f53cd

Please sign in to comment.