1 Star 0 Fork 2

Cyber/mp-push-leancloud

forked from xyz9836/mp-push-leancloud 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
routes.js 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
brucx 提交于 2018-02-02 21:31 . v1.0.0
const router = require('express').Router()
const { wxMiddlewareBuilder } = require('./utils')
const { User, Channel } = require('./models')
router.use('/wx', wxMiddlewareBuilder(async (message, req, res, next) => {
const { FromUserName, Content } = message
console.log('收到微信消息:', { message })
let user = new User(FromUserName)
await user.login()
const params = Content.trim().split(' ')
if (params[0] !== 'link') {
res.reply({
type: 'text',
content: `目前仅支持 link 命令。`
})
} else {
let channelName = params[1]
if (channelName) {
try {
let channel = new Channel(channelName)
await channel.init()
await user.linkChannel(channel)
} catch (e) {
console.error(e)
return res.reply({
type: 'text',
content: 'Someting went wrong...'
})
}
}
// 返回用户的 Channel 列表
let channels = await user.findChannels()
let content = channels.length ? channels.reverse().join('\n') : '您还未绑定频道'
return res.reply({
type: 'text',
content
})
}
})
)
router.use('/push', async function (req, res) {
const { channelName, text } = req.body
console.log(req.body)
if (!channelName || !text) {
return res.json({
error: 1,
message: 'Bad params!'
})
}
let channel = new Channel(channelName)
await channel.init()
if (!channel.exist) {
return res.json({
error: 1,
message: 'Channel not exist!'
})
}
await channel.push(text)
res.json({
error: 0,
message: 'Done!'
})
})
module.exports = router
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/cybertec/mp-push-leancloud.git
git@gitee.com:cybertec/mp-push-leancloud.git
cybertec
mp-push-leancloud
mp-push-leancloud
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385