From 5e536afeae87d983e0b2a909cd809df6e9477dba Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Mon, 15 Dec 2025 14:26:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=89=93=E5=BC=80?= =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E5=AE=A2=E6=9C=8D=E7=BB=84?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js/wxwork-jssdk.js | 187 +++ components/ns-contact/ns-contact.vue | 386 ++++--- components/wxwork-contact/wxwork-contact.vue | 285 +++++ pages/contact/contact.vue | 1080 +++++++++--------- 4 files changed, 1257 insertions(+), 681 deletions(-) create mode 100644 common/js/wxwork-jssdk.js create mode 100644 components/wxwork-contact/wxwork-contact.vue diff --git a/common/js/wxwork-jssdk.js b/common/js/wxwork-jssdk.js new file mode 100644 index 0000000..bd2932b --- /dev/null +++ b/common/js/wxwork-jssdk.js @@ -0,0 +1,187 @@ +/** + * 企业微信JS-SDK调用 + */ +let WxWork = function () { + // 企业微信JS-SDK + this.wxwork = null; + + /** + * 初始化企业微信JS-SDK + * @param {Object} params - 初始化参数 + * @param {string} params.corpId - 企业ID + * @param {string} params.agentId - 应用ID + * @param {string} params.timestamp - 时间戳 + * @param {string} params.nonceStr - 随机字符串 + * @param {string} params.signature - 签名 + * @param {Array} params.jsApiList - 需要使用的JS接口列表 + */ + this.init = function (params) { + if (typeof wx !== 'undefined' && wx.config) { + // 小程序环境下的企业微信 + this.wxwork = wx; + } else if (typeof WWOpenData !== 'undefined') { + // H5环境下的企业微信 + this.wxwork = WWOpenData; + } else { + console.error('企业微信JS-SDK未加载'); + return false; + } + + this.wxwork.config({ + beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题 + debug: false, // 开启调试模式 + corpId: params.corpId, // 必填,企业号的唯一标识 + agentId: params.agentId, // 必填,企业微信应用ID + timestamp: params.timestamp, // 必填,生成签名的时间戳 + nonceStr: params.nonceStr, // 必填,生成签名的随机串 + signature: params.signature, // 必填,签名 + jsApiList: params.jsApiList || [ + 'openUserProfile', + 'openEnterpriseChat', + 'getContext', + 'getCurExternalContact', + 'openExistedChatWithMsg' + ] // 必填,需要使用的JS接口列表 + }); + + return true; + }; + + /** + * 添加企业微信联系人 + * @param {Object} params - 参数 + * @param {string} params.userId - 用户ID + * @param {Function} success - 成功回调 + * @param {Function} fail - 失败回调 + */ + this.addContact = function (params, success, fail) { + if (!this.wxwork) { + console.error('企业微信JS-SDK未初始化'); + if (fail) fail('企业微信JS-SDK未初始化'); + return; + } + + this.wxwork.ready(() => { + this.wxwork.invoke('openUserProfile', { + type: 'external', // 外部联系人 + userId: params.userId // 用户ID + }, (res) => { + if (res.err_msg === 'openUserProfile:ok') { + if (success) success(res); + } else { + console.error('打开用户资料失败:', res); + if (fail) fail(res.err_msg); + } + }); + }); + }; + + /** + * 打开企业微信客服会话 + * @param {Object} params - 参数 + * @param {string} params.corpId - 企业ID + * @param {string} params.url - 客服URL + * @param {string} params.name - 会话名称 + * @param {Function} success - 成功回调 + * @param {Function} fail - 失败回调 + */ + this.openCustomerService = function (params, success, fail) { + if (!this.wxwork) { + console.error('企业微信JS-SDK未初始化'); + if (fail) fail('企业微信JS-SDK未初始化'); + return; + } + + this.wxwork.ready(() => { + // #ifdef MP-WEIXIN + if (typeof wx !== 'undefined' && wx.openCustomerServiceChat) { + // 微信小程序环境 + wx.openCustomerServiceChat({ + extInfo: { + url: params.url + }, + corpId: params.corpId, + showMessageCard: true, + sendMessageTitle: params.sendMessageTitle || '', + sendMessagePath: params.sendMessagePath || '', + sendMessageImg: params.sendMessageImg || '' + }); + if (success) success(); + } + // #endif + // #ifdef H5 + else if (typeof WWOpenData !== 'undefined') { + // H5环境 + window.location.href = params.url; + if (success) success(); + } + // #endif + else { + // 直接跳转链接 + window.location.href = params.url; + if (success) success(); + } + }); + }; + + /** + * 生成企业微信活码链接 + * @param {Object} params - 参数 + * @param {string} params.configId - 活码配置ID + * @param {string} params.userId - 用户ID + * @returns {string} 活码链接 + */ + this.generateContactUrl = function (params) { + // 企业微信活码链接格式 + const baseUrl = 'https://work.weixin.qq.com/kfid'; + if (params.configId) { + return `${baseUrl}/${params.configId}`; + } + return null; + }; + + /** + * 检查环境是否支持企业微信 + * @returns {boolean} 是否支持 + */ + this.isSupported = function () { + // #ifdef MP-WEIXIN + return typeof wx !== 'undefined' && wx.openCustomerServiceChat; + // #endif + // #ifdef H5 + return typeof WWOpenData !== 'undefined' || /wxwork/i.test(navigator.userAgent); + // #endif + return false; + }; + + /** + * 获取当前环境信息 + * @returns {Object} 环境信息 + */ + this.getEnvironment = function () { + // #ifdef MP-WEIXIN + return { + platform: 'miniprogram', + isWxWork: false, + supportContact: typeof wx !== 'undefined' && wx.openCustomerServiceChat + }; + // #endif + // #ifdef H5 + const isWxWork = /wxwork/i.test(navigator.userAgent); + return { + platform: 'h5', + isWxWork: isWxWork, + supportContact: isWxWork || typeof WWOpenData !== 'undefined' + }; + // #endif + return { + platform: 'unknown', + isWxWork: false, + supportContact: false + }; + }; +} + +export { + WxWork +} \ No newline at end of file diff --git a/components/ns-contact/ns-contact.vue b/components/ns-contact/ns-contact.vue index b69bca8..b3fca42 100644 --- a/components/ns-contact/ns-contact.vue +++ b/components/ns-contact/ns-contact.vue @@ -1,171 +1,217 @@ - - - - - - \ No newline at end of file diff --git a/components/wxwork-contact/wxwork-contact.vue b/components/wxwork-contact/wxwork-contact.vue new file mode 100644 index 0000000..76e7805 --- /dev/null +++ b/components/wxwork-contact/wxwork-contact.vue @@ -0,0 +1,285 @@ + + + + + \ No newline at end of file diff --git a/pages/contact/contact.vue b/pages/contact/contact.vue index 62984d6..0f652ca 100644 --- a/pages/contact/contact.vue +++ b/pages/contact/contact.vue @@ -1,511 +1,569 @@ - - - - - + + + + +