应对企微认证考试,写了node版的企微对接
This commit is contained in:
48
src/rest/addressBook/add.js
Normal file
48
src/rest/addressBook/add.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const dbUtils = require("../../utils/dbUitls");
|
||||
const fs = require('fs');
|
||||
const FormData = require('form-data');
|
||||
const fastcsv = require('fast-csv');
|
||||
const axios = require("axios");
|
||||
|
||||
//生成csv并上传至企微后台获取 media_id
|
||||
const uploadCsv = (data = [], access_token) => {
|
||||
// data数据格式
|
||||
// {"name": "张三", "email": "john.doe@example.com",phone:"13388888888"},
|
||||
const csv = data.map(e => {
|
||||
return {
|
||||
"姓名": e.name,
|
||||
"账号": e.phone,
|
||||
"手机号": e.phone,
|
||||
"邮箱": e.email,
|
||||
}
|
||||
})
|
||||
const form = new FormData()
|
||||
const csvStream = fastcsv.format({headers: true})
|
||||
csvStream.pipe(fs.createWriteStream('tmp.csv'))
|
||||
data.forEach(item => csvStream.write(item))
|
||||
csvStream.end()
|
||||
form.append('media', fs.createReadStream('tmp.csv'))
|
||||
return axios.post(`https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=${access_token}&type=file`, {
|
||||
headers: form.getHeaders()
|
||||
}).then(res => {
|
||||
const {media_id} = res.data
|
||||
return media_id
|
||||
})
|
||||
}
|
||||
module.exports = {
|
||||
action: "/wxwork/addressBook/syncUser",
|
||||
method: "post",
|
||||
execute: (request, response) => {
|
||||
const {access_token} = request.body
|
||||
// 获取原CA账号数据
|
||||
dbUtils.list({
|
||||
table: "sys_user",
|
||||
}).then(data => uploadCsv(data, access_token))
|
||||
.then(media_id => {
|
||||
// 上传csv至企微后台
|
||||
return axios.post(`https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser?access_token=${access_token}`, {
|
||||
media_id
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user