-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
82 lines (74 loc) · 2.72 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
const express = require('express')
const AV = require('leancloud-storage');
const { Query, User } = AV;
const app = express()
const port = 80
AV.init({
appId: process.env.appId,
appKey: process.env.appKey,
serverURL: process.env.serverURL
});
const query = new AV.Query('likeCount');
const account = AV.Object.createWithoutData('likeCount', process.env.objectId);
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
app.use('/static', express.static(__dirname + '/static'));
app.use('/demo', express.static(__dirname + '/demo'));
app.get('/', function getState(req,res){
res.setHeader('Content-Type', 'text/html');
res.sendFile(__dirname + '/public/index.html');
})
app.get('/info', (req,res) => {
query.find().then(function (results) {
const data = {code: '200', msg: 'success', data: {count: results[0].attributes.count}}
res.send(data);
}
).catch(function (error) {
const data = {code: '201', msg: 'error'}
res.send(data);
});
})
app.get('/like', (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
const queryIP = new AV.Query('likeUser').equalTo('ip', ip);
queryIP.find().then(function (results) {
console.log(results)
if (results.length > 0) {
const data = {code: '201', msg: '你的爱意已经收到啦~'}
res.send(data);
} else {
const Like = AV.Object.extend('likeUser');
const like = new Like();
like.set('ip', ip);
const acl = new AV.ACL();
acl.setPublicReadAccess(true);
like.setACL(acl);
like.save().then((like) => {
account.increment('count', +1);
account.save(null, {
query: new AV.Query('likeCount').greaterThanOrEqualTo('count', +1),fetchWhenSave: true
}).then((account) => {
const data = {code: '200', msg: 'success', data: {count: account.attributes.count}}
res.send(data);
}, (error) => {
if (error.code === 305) {
const data = {code: '201', msg: 'error'}
res.send(data);
}
});
}, (error) => {
console.error('Failed to create new object, with error message: ' + error.message);
});
}
}).catch(function (error) {
const data = {code: '201', msg: 'error'}
res.send(data);
});
})
app.listen(port, () => {})