企微考试提交
This commit is contained in:
@@ -1,20 +1,71 @@
|
||||
const {getSignature, decrypt} = require("@wecom/crypto");
|
||||
const {addOrUpdate} = require("../../utils/dbUitls");
|
||||
const {Parser} = require("xml2js");
|
||||
const {query} = require("../../utils/dbUitls");
|
||||
const dayjs = require("dayjs");
|
||||
const axios = require("axios");
|
||||
const {getAccessToken} = require("./getUserInfo");
|
||||
const token = "pnYAdXEHYzYhIyzE6Qbs2L"
|
||||
const encodingAESKey = "fHkOHrUGSVUmPjFmshLEFN2XbaqF3OxsuYgnJu6DB1G"
|
||||
|
||||
let accessToken
|
||||
|
||||
const reply = (agentid, to, content) => {
|
||||
axios.post("https://qyapi.weixin.qq.com/cgi-bin/message/send", {
|
||||
touser: to, agentid, msgtype: "markdown", markdown: {
|
||||
content
|
||||
}
|
||||
}, {params: {access_token: accessToken}})
|
||||
}
|
||||
/**
|
||||
* 获取当前值班人并将信息发送给询问人,同时通知值班人员
|
||||
* @param params
|
||||
*/
|
||||
const handleReplyDutyInfo = (params) => {
|
||||
const {FromUserName: touser, AgentID: agentid} = params,
|
||||
now = dayjs().format("YYYY-MM-DD HH:mm:ss"),
|
||||
sql = `select * from node_wx_test_duty where dutyStartTime<='${now}' and dutyEndTime>='${now}'`
|
||||
query(sql).then(res => {
|
||||
const info = res?.[0] || {}
|
||||
if (info.dutyUserId) {
|
||||
getAccessToken().then(access_token => {
|
||||
accessToken = access_token
|
||||
/**
|
||||
* 回复查询值班信息给查询人
|
||||
*/
|
||||
reply(agentid, touser, `当前值班信息如下\n>值班时间:${info.dutyStartTime} - ${info.dutyEndTime}\n值班人员:${info.dutyUserName}\n`)
|
||||
/**
|
||||
* 通知提醒值班人员
|
||||
*/
|
||||
reply(agentid, info.dutyUserId, `今天轮到你值班了,请知悉~`)
|
||||
})
|
||||
} else {
|
||||
/**
|
||||
* 无值班人员
|
||||
*/
|
||||
reply(agentid, touser, `今天没有值班人员哦~`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
action: "/node/wxtest/action",
|
||||
method: "post",
|
||||
execute: (request, response) => {
|
||||
const {msg_signature, timestamp, nonce, echostr} = request.query
|
||||
const signature = getSignature(token, timestamp, nonce, echostr);
|
||||
const {msg_signature, timestamp, nonce} = request.query,
|
||||
{xml: {encrypt}} = request.body
|
||||
const signature = getSignature(token, timestamp, nonce, encrypt[0]);
|
||||
if (msg_signature == signature) {
|
||||
const context = decrypt(encodingAESKey, echostr)
|
||||
const {name, mobile: phone, email, userId} = context
|
||||
addOrUpdate({
|
||||
table: "sys_user", form: {name, email, phone, userId}
|
||||
const context = decrypt(encodingAESKey, encrypt[0])
|
||||
const parser = new Parser({explicitArray: false})
|
||||
parser.parseString(context.message, (err, result) => {
|
||||
if (!err) {
|
||||
if (result.xml.Content == '值班') {
|
||||
handleReplyDutyInfo(result.xml)
|
||||
}
|
||||
response.send({code: 0, message: "success"})
|
||||
} else response.send({code: 1, err})
|
||||
})
|
||||
response.send({code: 0, message: "success"})
|
||||
}else response.send({code: 1, message: "error"})
|
||||
} else response.send({code: 1, message: "error"})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user