备忘录提供跨域访问

This commit is contained in:
luchenqun 2018-12-13 16:03:25 +08:00
parent 57fe592d02
commit b823f2ef95
2 changed files with 35 additions and 25 deletions

View File

@ -1407,37 +1407,47 @@ api.post('/addNote', function(req, res) {
api.get('/notes', function(req, res) { api.get('/notes', function(req, res) {
console.log("getNotes username = ", req.session.username); console.log("getNotes username = ", req.session.username);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', '*');
var params = req.query; var params = req.query;
if (!params.shareNote && !req.session.user) { if (!params.shareNote && !req.session.user) {
res.send(401); res.send(401);
return; return;
} }
if (params.shareNote) { if (params.shareNote) {
db.getNote(params.shareNote) if(params.json) {
.then((data) => res.send(` db.getNote(params.shareNote)
<body style="margin:0px;height:100%;"> .then((data) => res.json(data))
<head> .catch((err) => console.log('notes', err));
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"> } else {
<script> db.getNote(params.shareNote)
if(screen && screen.availWidth <= 1024) { .then((data) => res.send(`
setTimeout(() => { <body style="margin:0px;height:100%;">
document.getElementById("note-div").style.width = "100%"; <head>
document.getElementById("note-div").style["background-color"] = "#F3F4F5"; <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
document.getElementById("note").style.width = "95%"; <script>
}, 100); if(screen && screen.availWidth <= 1024) {
} setTimeout(() => {
</script> document.getElementById("note-div").style.width = "100%";
</head> document.getElementById("note-div").style["background-color"] = "#F3F4F5";
<div id="note-div" style="text-align:center;"> document.getElementById("note").style.width = "95%";
<pre id="note" style="background-color:RGB(243,244,245); padding:0px 10px 0px 10px; margin:0px; width:60%; min-height:100%;display: inline-block;text-align: left; font-size: 15px; font-family:italic arial,sans-serif;word-wrap: break-word;white-space: pre-wrap;">\n\n${data}\n\n</pre> }, 100);
</div> }
</body>`)) </script>
.catch((err) => console.log('notes', err)); </head>
} else { <div id="note-div" style="text-align:center;">
params.user_id = req.session.user.id; <pre id="note" style="background-color:RGB(243,244,245); padding:0px 10px 0px 10px; margin:0px; width:60%; min-height:100%;display: inline-block;text-align: left; font-size: 15px; font-family:italic arial,sans-serif;word-wrap: break-word;white-space: pre-wrap;">\n\n${data}\n\n</pre>
db.getNotes(params) </div>
.then((data) => res.json(data)) </body>`))
.catch((err) => console.log('notes', err)); .catch((err) => console.log('notes', err));
}
} else {
params.user_id = req.session.user.id;
db.getNotes(params)
.then((data) => res.json(data))
.catch((err) => console.log('notes', err));
} }
}); });

View File

@ -89,7 +89,7 @@ drop table if exists notes;
CREATE TABLE `notes` ( CREATE TABLE `notes` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- id `id` int(11) NOT NULL AUTO_INCREMENT, -- id
`user_id` int(11) NOT NULL, -- 用户id `user_id` int(11) NOT NULL, -- 用户id
`content` varchar(8092) DEFAULT NULL, -- 备忘内容 `content` text DEFAULT NULL, -- 备忘内容
`tag_id` int(11) DEFAULT NULL, -- 分类id `tag_id` int(11) DEFAULT NULL, -- 分类id
`created_at` datetime DEFAULT now(), -- 创建时间 `created_at` datetime DEFAULT now(), -- 创建时间
`public` tinyint(4) DEFAULT '1', -- 是否公开 1 公开0 不公开 `public` tinyint(4) DEFAULT '1', -- 是否公开 1 公开0 不公开