forked from wangxinleo/wechat-public-account-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
80 lines (73 loc) · 2.19 KB
/
main.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
import schedule from 'node-schedule'
import dayjs from 'dayjs'
import {
getAccessToken,
sendMessageReply,
getAggregatedData,
getCallbackTemplateParams,
} from './src/services/index.js'
import config from './config/exp-config.js'
import cornTime from './config/server-config.js'
import mainForTest from './main-for-test.js'
export default async function mainForProd() {
// 获取accessToken
console.log('\n\n')
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'))
console.log('---')
console.log('【连接微信公众平台】开始')
const accessToken = await getAccessToken()
if (!accessToken) {
console.log('遇到错误,执行终止!')
return
}
console.log('【连接微信公众平台】结束')
console.log('---')
// 处理好的用户数据
console.log('---')
console.log('【数据获取】开始')
const aggregatedData = await getAggregatedData()
console.log('【数据获取】结束')
console.log('---')
// 公众号推送消息
console.log('---')
console.log('【常规模板】推送开始')
const {
needPostNum,
successPostNum,
failPostNum,
successPostIds,
failPostIds,
} = await sendMessageReply(aggregatedData, accessToken)
console.log('【常规模板】推送结束')
console.log('---')
// 获取回执信息
const callbackTemplateParams = getCallbackTemplateParams({
needPostNum,
successPostNum,
failPostNum,
successPostIds,
failPostIds,
})
// 发送回执
if (config.CALLBACK_TEMPLATE_ID) {
console.log('---')
console.log('【推送完成提醒】推送开始')
await sendMessageReply(config.CALLBACK_USERS, accessToken, config.CALLBACK_TEMPLATE_ID, callbackTemplateParams)
console.log('【推送完成提醒】推送结束')
console.log('---')
}
}
const main = () => {
if (process.env.APP_MODE === 'params-log') {
mainForTest()
} else if (process.env.APP_MODE === 'server') {
console.log('======【定时推送服务已启动, enjoying it】======')
console.log(`目前定时推送的配置为:【${cornTime}】`)
schedule.scheduleJob(cornTime, () => {
mainForProd()
})
} else if (process.env.APP_MODE === 'prod') {
mainForProd()
}
}
main()