diff --git a/src/config/db.js b/src/config/db.js index df8a305..241d016 100644 --- a/src/config/db.js +++ b/src/config/db.js @@ -3,6 +3,6 @@ module.exports = { user: "root", port: 3306, password: "Cwy@2019", - database: "dvcp_oms_dev", + database: "dvcp_v2_dev", multipleStatements: true } diff --git a/src/rest/index.js b/src/rest/index.js index f68fee0..dd5a134 100644 --- a/src/rest/index.js +++ b/src/rest/index.js @@ -9,6 +9,8 @@ module.exports = { log(`${chalk.bgBlue.black(" REST ")} ${rest.action}`) if (rest.method == "post") { ins.post(rest.action, (req, res) => rest.execute(req, res)) + } else if (rest.method == "get") { + ins.get(rest.action, (req, res) => rest.execute(req, res)) } } }) diff --git a/src/rest/wxtest/action.js b/src/rest/wxtest/action.js new file mode 100644 index 0000000..452880d --- /dev/null +++ b/src/rest/wxtest/action.js @@ -0,0 +1,20 @@ +const {getSignature, decrypt} = require("@wecom/crypto"); +const {addOrUpdate} = require("../../utils/dbUitls"); +const token = "pnYAdXEHYzYhIyzE6Qbs2L" +const encodingAESKey = "fHkOHrUGSVUmPjFmshLEFN2XbaqF3OxsuYgnJu6DB1G" +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); + 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} + }) + response.send({code: 0, message: "success"}) + }else response.send({code: 1, message: "error"}) + } +} diff --git a/src/rest/wxtest/actionGet.js b/src/rest/wxtest/actionGet.js new file mode 100644 index 0000000..58a3097 --- /dev/null +++ b/src/rest/wxtest/actionGet.js @@ -0,0 +1,18 @@ +const {getSignature, decrypt} = require("@wecom/crypto"); +const token = "pnYAdXEHYzYhIyzE6Qbs2L" +const encodingAESKey = "fHkOHrUGSVUmPjFmshLEFN2XbaqF3OxsuYgnJu6DB1G" +module.exports = { + action: "/node/wxtest/action", + method: "get", + execute: (request, response) => { + const {msg_signature, timestamp, nonce, Encrypt} = request.query + const signature = getSignature(token, timestamp, nonce, Encrypt); + console.log(msg_signature, signature) + if (msg_signature == signature) { + const context = decrypt(encodingAESKey, Encrypt) + response.send(context) + } else { + response.send({code: 1, message: "验证不通过"}) + } + } +} diff --git a/src/rest/wxtest/add.js b/src/rest/wxtest/add.js new file mode 100644 index 0000000..cfba574 --- /dev/null +++ b/src/rest/wxtest/add.js @@ -0,0 +1,16 @@ +const dbUtils = require("../../utils/dbUitls"); +module.exports = { + action: "/node/duty/addOrUpdate", + method: "post", + execute: (request, response) => { + let {id, dutyUserId,dutyUserName,dutyStartTime,dutyEndTime} = request.body + dbUtils.addOrUpdate({ + table: 'node_wx_test_duty', + form: {id, dutyUserId,dutyUserName,dutyStartTime,dutyEndTime} + }).then(data => { + response.send({code: 0, data}) + }).catch(err => { + response.send({code: 1, err: err?.sqlMessage || err || ""}) + }) + } +} diff --git a/src/rest/wxtest/getUserInfo.js b/src/rest/wxtest/getUserInfo.js new file mode 100644 index 0000000..1fb0c93 --- /dev/null +++ b/src/rest/wxtest/getUserInfo.js @@ -0,0 +1,38 @@ +const axios = require("axios"); +const CORPID = "ww596787bb70f08288" +const SECRET = "Bh3GT11_bzxSm03xZBY8otjw_WLWeLsduzDQweUohAY" +/** + * 获取access_token + */ +const getAccessToken = (corpid = CORPID, secret = SECRET) => { + const url = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpid}&corpsecret=${secret}` + return axios.get(url).then(res => { + return res.data?.access_token + }) +} +/** + * 获取 userId及其他信息 + */ +const getUserInfo = (accessToken, code) => { + return axios.get(`https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo`, { + params: {access_token: accessToken, code} + }) +} +module.exports = { + action: "/node/wxtest/token", + method: "post", + execute: (request, response) => { + const {code} = request.query + getAccessToken().then(token => getUserInfo(token, code)).then(res => { + if (res?.data?.data) { + response.send({code: 0, data: res.data.data}) + } else { + console.log(res.data) + response.send({code: 1, err: res.data}) + } + }).catch(err => { + console.log(err) + response.send({code: 1, err}) + }) + } +} diff --git a/src/rest/wxtest/list.js b/src/rest/wxtest/list.js new file mode 100644 index 0000000..0c14a66 --- /dev/null +++ b/src/rest/wxtest/list.js @@ -0,0 +1,16 @@ +const dbUtils = require("../../utils/dbUitls"); +module.exports = { + action: "/node/duty/list", + method: "post", + execute: (request, response) => { + let {size, current} = request.query + dbUtils.list({ + table: 'node_wx_test_duty', + search: {size, current}, sort: 'dutyStartTime', con: "dutyUserName" + }).then(data => { + response.send({code: 0, data}) + }).catch(err => { + response.send({code: 1, err: err?.sqlMessage || err || ""}) + }) + } +} diff --git a/src/rest/addressBook/token.js b/src/rest/wxtest/token.js similarity index 91% rename from src/rest/addressBook/token.js rename to src/rest/wxtest/token.js index a8585fe..00e78e8 100644 --- a/src/rest/addressBook/token.js +++ b/src/rest/wxtest/token.js @@ -9,7 +9,7 @@ const getAccessToken = (corpid = CORPID, secret = SECRET) => { } module.exports = { - action: "/wxwork/addressBook/token", + action: "/node/wxtest/token", method: "post", execute: (request, response) => { getAccessToken().then(token => { @@ -18,5 +18,6 @@ module.exports = { console.log(err) response.send({code: 1, err}) }) - } + }, + getAccessToken }