企业微信jdk 授权 记录
1、npm install @wecom/jssdk
2、index.html 引入
<script src="https://wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-2.0.2.js"></script>
3、创建js import * as ww from "@wecom/jssdk";
/*** 获取企业微信jdk局方法--------------------------1*/
import { accessConfig, accessAgentConfig } from "../api/sdk/index";
import * as ww from "@wecom/jssdk";
/***** 作者: Lenovo-【Lindon】*** 文件名称: weChat*** 文件创建日期: 2024/4/30****/
// 注册企业微信应用信息
// 注册企业微信应用信息
export async function registerWeChat() {// ✅ 前置处理:确保URL处理一致const getCleanUrl = () => window.location.href.split('#')[0];// ✅ 企业签名(同步改造)const getConfigSignature = async () => {try {console.log('[DEBUG] 开始获取企业签名');const res = await accessConfig({ path: getCleanUrl() });if (res?.code !== 200) throw new Error('企业签名接口异常');console.log('[DEBUG] 企业签名数据:', res.data);return res.data; // ✅ 直接返回后端结构体} catch (error) {console.error('[ERROR] 企业签名失败:', error);throw error; // 必须抛出以中断流程}};// ✅ 应用签名(保持原有正确结构)const getAgentConfigSignature = async (urlFromSDK) => {try {console.log('[DEBUG] 开始获取应用签名,SDK传入URL:', urlFromSDK);const pureUrl = new URL(urlFromSDK.split('#')[0]).href; // ✅ 使用SDK提供的URLconst res = await accessAgentConfig({ path: pureUrl });if (res?.code !== 200) throw new Error('应用签名接口异常');console.log('[DEBUG] 应用签名数据:', res.data);return res.data;} catch (error) {console.error('[ERROR] 应用签名失败:', error);throw error;}};// ✅ 注册时开启调试模式ww.register({corpId: "wwf823be6e18d7191f",agentId: 1000003,jsApiList: ["getCurExternalContact","scanQRCode"],getConfigSignature,getAgentConfigSignature,debug: true, // ✅ 关键!开启调试模式onConfigSuccess(res) {console.log('[SUCCESS] 配置成功:', res);},onConfigFail(res) {console.error('[FAIL] 配置失败:', res);alert('SDK配置失败,请检查控制台');}});
}//后续在哪个页面如果需要使用到sdk,都需要先注册registerWeChat,然后再调用对应的sdk方法
4、将方法引入到 需要使用的页面 中 然后调用 registerWeChat() 进行授权 可以在 加载时Mounted 去授权
5、使用示例
点击事件function fafacc() {ww.getCurExternalContact({success(res) {console.log('获取外部联系人id成功', res)},fail(res) {console.log('获取外部联系人id失败', res)},})
}