企业微信自建应用-获取客户群id
概述
最后更新:2018/10/17
企业微信JS-SDK是企业微信面向网页开发者提供的基于企业微信内的网页开发工具包。
通过使用企业微信JS-SDK,网页开发者可借助企业微信高效地使用拍照、选图、语音、位置等手机系统的能力,同时可以直接使用企业微信分享、扫一扫等企业微信特有的能力,为企业微信用户提供更优质的网页体验。
agentConfig的作用
config注入的是企业的身份与权限,而agentConfig注入的是应用的身份与权限。尤其是当调用者为第三方服务商时,通过config无法准确区分出调用者是哪个第三方应用,而在部分场景下,又必须严谨区分出第三方应用的身份,此时即需要通过agentConfig来注入应用的身份信息。
企微官网:
const sha1 = require('node-sha1')
const toolsQiWeToken = async (req, res) => {
axios({
url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwd70e******70290f&corpsecret=JrPSfOHlNzPiFvRtn2******UuuHwAxyUdUDu8',
}).then((resData) => {
console.log(resData.data)
let tokenData = resData.data
let access_token = resData.data.access_token
axios({
url: `https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=${access_token}&debug=1`,
}).then((resData) => {
console.log(resData.data)
let ticket = resData.data.ticket
let noncestr = uuidv4()
let timestamp = Date.now()
let url = 'http://yuying-api.xutongbao.top/index1.html'
console.log('ticket', ticket)
console.log('noncestr', noncestr)
let string1 = `jsapi_ticket=${ticket}&noncestr=${noncestr}×tamp=${timestamp}&url=${url}`
console.log(string1)
let signature = sha1(string1)
let corpData = {
tokenData,
ticket: resData.data,
string1,
ticket,
noncestr,
timestamp,
url,
signature,
}
axios({
url: `https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=${access_token}&type=agent_config&debug=1`,
}).then((resData) => {
console.log('agent', resData.data)
let ticket = resData.data.ticket
let noncestr = uuidv4()
let timestamp = Date.now()
let url = 'http://yuying-api.xutongbao.top/index1.html'
console.log('ticket', ticket)
console.log('noncestr', noncestr)
let string1 = `jsapi_ticket=${ticket}&noncestr=${noncestr}×tamp=${timestamp}&url=${url}`
console.log(string1)
let signature = sha1(string1)
let agentData = {
string1,
ticket,
noncestr,
timestamp,
url,
signature,
}
res.send({
code: 200,
data: {
corpData,
agentData,
},
message: '成功',
})
})
})
})
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script
src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"
referrerpolicy="origin"
ref
></script>
<script src="lib/axios.min.js"></script>
</head>
<body>
666
<script src="lib/vconsole.min.js"></script>
<script>
// VConsole will be exported to `window.VConsole` by default.
var vConsole = new window.VConsole()
</script>
<script>
axios({
url: `http://yuying-api.xutongbao.top/api/light/tools/qiWeToken`,
method: 'post',
}).then((res) => {
console.log(res)
const agentFun = () => {
const { timestamp, noncestr, signature } = res.data.data.agentData
wx.agentConfig({
corpid: 'wwd70ee******70290f', // 必填,企业微信的corpid,必须与当前登录的企业一致
agentid: '1000004', // 必填,企业微信的应用id (e.g. 1000247)
timestamp, // 必填,生成签名的时间戳
nonceStr: noncestr, // 必填,生成签名的随机串
signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
jsApiList: ['getCurExternalChat'], //必填,传入需要使用的接口名称
success: function (res) {
console.log(666, res)
wx.invoke('getCurExternalChat', {}, function (res) {
console.log(777, res)
if (res.err_msg == 'getCurExternalChat:ok') {
let chatId = res.chatId //返回当前外部群的群聊ID
} else {
//错误处理
}
})
// 回调
},
fail: function (res) {
console.log(res)
if (res.errMsg.indexOf('function not exist') > -1) {
alert('版本过低请升级')
}
},
})
console.log(timestamp, noncestr, signature)
}
agentFun()
})
</script>
</body>
</html>
错误码检查工具:
https://open.work.weixin.qq.com/devtool/query?e=81013