wechat.js
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* @Descripttion: 企业微信原生API
* @version:1.0.0
* @Author: limin
* @Date: 2021-06-08 11:41:40
* @LastEditors: limin
* @LastEditTime: 2021-06-20 19:35:46
*/
import api from '@/api'
const wechat = {
init(url = window.location.href) {
return new Promise((resolve, reject) => {
api.common.getagentconfig(url).then(res => {
const { corpId: corpid, agentId: agentid, ...cfg } = res.data
wx.agentConfig({
corpid,
agentid,
...cfg,
jsApiList: ['getCurExternalContact', 'openEnterpriseChat'], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
success: function (res) {
resolve()
},
fail: function (res) {
if (res.errMsg.indexOf('function not exist') > -1) {
alert('版本过低请升级')
}
reject()
}
})
})
})
},
sign(opts) {
const str = Object.keys(opts)
.map(key => `${key}=${opts[key]}`)
.join('&')
return sha1(str)
},
/**
* 获取当前外部联系人userid
*/
getCurExternalContact() {
return new Promise((resolve, reject) => {
wx.invoke('getCurExternalContact', {}, function (res) {
if (res.err_msg == 'getCurExternalContact:ok') {
resolve(res.userId)
} else {
reject(res)
}
})
})
},
/**
* 打开会话
* @param {*} userIds
* @param {*} externalUserIds
* @param {*} groupName
* @param {*} chatId
* @returns
*/
openEnterpriseChat(externalUserIds = '', userIds = '', groupName = '', chatId = '') {
return new Promise((resolve, reject) => {
wx.openEnterpriseChat({
// 注意:userIds和externalUserIds至少选填一个。内部群最多2000人;外部群最多500人;如果有微信联系人,最多40人
userIds, //参与会话的企业成员列表,格式为userid1;userid2;...,用分号隔开。
externalUserIds, //'wmEAlECwAAHrbWYDOK5u3Bf13xlYDAAA;wmEAlECwAAHibWYDOK5u3Af13xlYDAAA', // 参与会话的外部联系人列表,格式为userId1;userId2;…,用分号隔开。
groupName, // 会话名称。单聊时该参数传入空字符串""即可。
chatId, // 若要打开已有会话,需指定此参数。如果是新建会话,chatId必须为空串
success: function (res) {
var chatId = res.chatId //返回当前群聊ID,仅当使用agentConfig注入该接口权限时才返回chatId
resolve(chatId)
},
fail: function (res) {
if (res.errMsg.indexOf('function not exist') > -1) {
alert('版本过低请升级')
}
}
})
})
}
}
export default wechat