创建于2024-09-27更新于 2025-03-22
03. 企微 SDK 封装
以下是对这段代码中各个方法的介绍和使用示例:
一、config 方法
-
功能介绍:
- 用于配置企业微信 JS-SDK,区分应用级别的权限,根据企微官方文档判断。
- 首先调用
obtainCompanySDK获取企业微信的配置信息,然后使用获取到的信息配置wx对象。如果配置成功,会触发wx.ready回调,并在其中调用agentConfig方法进一步配置应用级别的信息。如果配置失败,会触发wx.error回调。
-
使用示例:
import { config } from './your-file'; async function initWeChatSDK() { try { await config(); console.log('企业微信 JS-SDK 配置成功'); } catch (error) { console.error('企业微信 JS-SDK 配置失败', error); } }
二、agentConfig 方法
-
功能介绍:
- 获取企业微信应用的配置信息。
- 调用
obtainAppSDK获取应用 SDK 的配置信息,然后使用这些信息配置wx.agentConfig。如果配置成功,会更新状态管理对象useBasicsStore的isJsdkChange属性为true,并返回配置成功的 Promise 对象。如果配置失败,返回配置失败的 Promise 对象。
-
使用示例:
import { agentConfig } from './your-file'; async function configureAppSDK() { try { const isSuccess = await agentConfig(); if (isSuccess) { console.log('企业微信应用配置成功'); } else { console.error('企业微信应用配置失败'); } } catch (error) { console.error('配置过程中发生错误', error); } }
三、getContext 方法
-
功能介绍:
- 获取进入 H5 页面的入口环境。
- 通过调用
wx.invoke('getContext', {}, callback)获取入口类型,如果入口类型在给定的entry_string中或者entry_string为空,则返回入口类型,否则返回false。
-
使用示例:
import { getContext } from './your-file'; async function determineEntry() { const entry = await getContext('single_chat_tools'); if (entry) { console.log('入口类型为', entry); } else { console.error('未识别的入口类型'); } }
四、sendChatMessage 方法
-
功能介绍:
- 用于发送聊天消息,可以发送不同类型的消息,如图片、文件、视频、文字、链接、小程序等。
- 根据消息的类型构建消息内容,然后调用
wx.invoke('sendChatMessage', params, callback)发送消息。如果发送成功,并且消息类型为radar,会调用addLinkRadarUserLog添加雷达点击日志。
-
使用示例:
import { sendChatMessage } from './your-file'; async function sendMessageToChat() { const message = { type: 'text', content: 'Hello, World!', }; try { const isSuccess = await sendChatMessage(message); if (isSuccess) { console.log('消息发送成功'); } else { console.error('消息发送失败'); } } catch (error) { console.error('发送消息过程中发生错误', error); } }
五、getCurExternalChat 方法
-
功能介绍:
- 获取当前客户群的群 ID。
- 通过调用
wx.invoke('getCurExternalChat', {}, callback)获取群 ID,如果获取成功,返回群 ID,否则返回错误信息。
-
使用示例:
import { getCurExternalChat } from './your-file'; async function getCurrentChatId() { try { const chatId = await getCurExternalChat(); if (chatId) { console.log('当前客户群 ID 为', chatId); } else { console.error('获取客户群 ID 失败'); } } catch (error) { console.error('获取过程中发生错误', error); } }
六、getCurExternalContact 方法
-
功能介绍:
- 获取当前外部联系人的 userid。
- 通过调用
wx.invoke('getCurExternalContact', {}, callback)获取用户 ID,如果获取成功,返回用户 ID,否则返回null。
-
使用示例:
import { getCurExternalContact } from './your-file'; async function getExternalUserId() { const userId = await getCurExternalContact(); if (userId) { console.log('当前外部联系人 ID 为', userId); } else { console.error('获取外部联系人 ID 失败'); } }
七、navigateToAddCustomer 方法
-
功能介绍:
- 进入添加客户界面。
- 通过调用
wx.invoke('navigateToAddCustomer', {}, callback)进入添加客户界面,如果成功,返回true。
-
使用示例:
import { navigateToAddCustomer } from './your-file'; async function addCustomer() { try { const isSuccess = await navigateToAddCustomer(); if (isSuccess) { console.log('进入添加客户界面成功'); } else { console.error('进入添加客户界面失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
八、openEnterpriseChat 方法
-
功能介绍:
- 打开会话,可以是企业内部成员会话,也可以是外部联系人会话。
- 通过调用
wx.openEnterpriseChat(options, callback)打开会话,如果成功,返回true,如果失败,根据错误信息进行处理。
-
使用示例:
import { openEnterpriseChat } from './your-file'; async function openChat(externalUserIds) { try { const isSuccess = await openEnterpriseChat(externalUserIds); if (isSuccess) { console.log('打开会话成功'); } else { console.error('打开会话失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
九、openUserProfile 方法
-
功能介绍:
- 打开用户资料页面,可以是企业成员或外部联系人。
- 通过调用
wx.invoke('openUserProfile', options, callback)打开用户资料页面,如果成功,返回false,如果失败,进行错误处理。
-
使用示例:
import { openUserProfile } from './your-file'; async function viewUserProfile(userid) { try { const isSuccess = await openUserProfile(userid); if (!isSuccess) { console.log('打开用户资料页面成功'); } else { console.error('打开用户资料页面失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十、shareToExternalMoments 方法
-
功能介绍:
- 发表朋友圈。
- 通过调用
wx.invoke('shareToExternalMoments', options, callback)发表朋友圈,如果用户取消分享,返回true,否则返回false。
-
使用示例:
import { shareToExternalMoments } from './your-file'; async function shareToMoments(content, dataArr) { try { const isSuccess = await shareToExternalMoments(content, dataArr); if (isSuccess) { console.log('分享到朋友圈成功'); } else { console.error('分享到朋友圈失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十一、shareAppMessage 方法
-
功能介绍:
- 自定义分享到会话。
- 通过调用
wx.invoke('shareAppMessage', options, callback)进行自定义分享到会话,如果成功,返回true,否则返回false。
-
使用示例:
import { shareAppMessage } from './your-file'; async function customShareToChat(data) { try { const isSuccess = await shareAppMessage(data); if (isSuccess) { console.log('分享到会话成功'); } else { console.error('分享到会话失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十二、shareWechatMessage 方法
-
功能介绍:
- 自定义分享到朋友圈。
- 通过调用
wx.invoke('shareWechatMessage', options, callback)进行自定义分享到朋友圈,如果成功,返回true,否则返回false。
-
使用示例:
import { shareWechatMessage } from './your-file'; async function customShareToMoments(data) { try { const isSuccess = await shareWechatMessage(data); if (isSuccess) { console.log('分享到朋友圈成功'); } else { console.error('分享到朋友圈失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十三、launchMiniprogram 方法
-
功能介绍:
- 跳转到小程序。
- 通过调用
wx.invoke('launchMiniprogram', options, callback)跳转到小程序,如果成功,返回true,否则返回false。
-
使用示例:
import { launchMiniprogram } from './your-file'; async function startMiniProgram(data) { try { const isSuccess = await launchMiniprogram(data); if (isSuccess) { console.log('启动小程序成功'); } else { console.error('启动小程序失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十四、shareToExternalContact 方法
-
功能介绍:
- 群发消息给外部联系人。
- 通过调用
wx.invoke('shareToExternalContact', options, callback)群发消息给外部联系人,如果成功,返回true,否则返回false。
-
使用示例:
import { shareToExternalContact } from './your-file'; async function sendMessageToExternalContacts(content, externalUserIds, attachments) { try { const isSuccess = await shareToExternalContact(content, externalUserIds, attachments); if (isSuccess) { console.log('群发消息给外部联系人成功'); } else { console.error('群发消息给外部联系人失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十五、scanQRCode 方法
-
功能介绍:
- 企业微信扫一扫。
- 通过调用
wx.invoke('scanQRCode', options, callback)进行扫码操作,如果成功,返回扫码结果,否则返回false。
-
使用示例:
import { scanQRCode } from './your-file'; async function scanCode() { try { const result = await scanQRCode(); if (result) { console.log('扫码结果', result); } else { console.error('扫码失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十六、hideOptionMenu 方法
-
功能介绍:
- 隐藏右上角菜单接口。
- 直接调用
wx.hideOptionMenu(),并返回一个 Promise 对象,resolve 为true。
-
使用示例:
import { hideOptionMenu } from './your-file'; async function hideMenu() { try { const isSuccess = await hideOptionMenu(); if (isSuccess) { console.log('右上角菜单隐藏成功'); } else { console.error('右上角菜单隐藏失败'); } } catch (error) { console.error('操作过程中发生错误', error); } }
十七、showOptionMenu 方法
-
功能介绍:
- 显示右上角菜单接口。
- 直接调用
wx.showOptionMenu(),并返回一个 Promise 对象,表示是否成功显示选项菜单。
-
使用示例:
import { showOptionMenu } from './your-file'; async function showMenu() { try { const isSuccess = await showOptionMenu(); if (isSuccess) { console.log('右上角菜单显示成功'); } else { console.error('右上角菜单显示失败'); } catch (error) { console.error('操作过程中发生错误', error); } }