forked from newbee1111/egg-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (65 loc) · 2.11 KB
/
app.js
File metadata and controls
70 lines (65 loc) · 2.11 KB
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
// 强制按照model生成表,开发环境下才使用
module.exports = app => {
// if (app.config.env === 'local') {
// app.beforeStart(async () => {
// await app.model.sync({ force: true });
// });
// }
app.sessionStore = {
* get(key) {
const res = yield app.redis.get(key);
if (!res) return null;
return JSON.parse(res);
},
* set(key, value, maxAge = 24 * 60 * 60 * 1000) {
const target = JSON.stringify(value);
yield app.redis.set(key, target, 'PX', maxAge);
},
* destroy(key) {
yield app.redis.del(key);
},
};
// 消息队列
const q = 'tasks';
const open = require('amqplib').connect('amqp://localhost');
app.queue = open.then(function(conn) {
return conn.createChannel();
});
app.queue
.then(function(ch) {
return ch.assertQueue(q).then(function() {
return ch.consume(q, function(msg) {
if (msg !== null) {
console.log(msg.content.toString());
ch.ack(msg);
return 'it is very interesting';
}
return false;
});
});
})
.catch(console.warn);
app.queueName = q;
// 构造微信signature等参数
// const appId = 'wx05630fac5042a808';
// const AppSecret = 'f8d5e0712eaf190b06ec731d3f3b8b2c';
// app.sessionStore.get('signature').then(async res => {
// if (!res) {
// const accessTokenRes = await app.curl(
// `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${AppSecret}`
// );
// const { data: access_token } = accessTokenRes;
// const ticketRes = await app.curl(
// `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`
// );
// const { data: ticket } = ticketRes;
// const nonceStr = Math.random()
// .toString(36)
// .substr(2, 15);
// const timeStamp = parseInt(new Date().getTime() / 1000) + '';
// const url = 'http://'
// const str = `jsapi_ticket=${ticket}&noncestr=${nonceStr}×tamp=${timeStamp}`;
// console.log(ticket);
// }
// });
};