diff --git a/components/wxwork-contact/wxwork-contact.vue b/components/wxwork-contact/wxwork-contact.vue index 76e7805..65701bf 100644 --- a/components/wxwork-contact/wxwork-contact.vue +++ b/components/wxwork-contact/wxwork-contact.vue @@ -38,8 +38,28 @@ type: String, default: '添加企业微信客服' }, - // 企业微信配置 + // 企业ID(必需) corpId: { + type: String, + required: true + }, + // 应用ID + agentId: { + type: String, + default: '' + }, + // 时间戳 + timestamp: { + type: String, + default: '' + }, + // 随机字符串 + nonceStr: { + type: String, + default: '' + }, + // 签名 + signature: { type: String, default: '' }, @@ -71,24 +91,16 @@ /** * 初始化企业微信SDK */ - async initWxWork() { + initWxWork() { try { - // 获取企业微信配置 - const res = await this.$api.sendRequest({ - url: '/api/wxwork/config', - data: { - corp_id: this.corpId - } - }); - - if (res.code === 0 && res.data) { + if (this.corpId) { this.wxWorkSDK = new WxWork(); const initResult = this.wxWorkSDK.init({ - corpId: res.data.corp_id, - agentId: res.data.agent_id, - timestamp: res.data.timestamp, - nonceStr: res.data.nonceStr, - signature: res.data.signature, + corpId: this.corpId, + agentId: this.agentId, + timestamp: this.timestamp, + nonceStr: this.nonceStr, + signature: this.signature, jsApiList: ['openUserProfile', 'openEnterpriseChat'] }); @@ -97,7 +109,7 @@ } } } catch (error) { - console.error('获取企业微信配置失败:', error); + console.error('初始化企业微信SDK失败:', error); } }, @@ -118,22 +130,26 @@ confirmAdd() { this.closePopup(); + // 直接使用props传递的配置 + const contactUrl = this.contactUrl; + const contactId = this.contactId; + // #ifdef MP-WEIXIN - if (this.contactUrl) { + if (contactUrl) { // 方案1:直接跳转到企业微信活码 - this.jumpToWxWorkContact(); - } else if (this.contactId) { + this.jumpToWxWorkContact(contactUrl); + } else if (contactId) { // 方案2:使用SDK打开用户资料 - this.openUserProfile(); + this.openUserProfile(contactId); } else { this.showError('未配置企业微信客服信息'); } // #endif // #ifdef H5 - if (this.contactUrl) { + if (contactUrl) { // H5环境直接跳转 - window.location.href = this.contactUrl; + window.location.href = contactUrl; } else { this.showError('未配置企业微信客服信息'); } @@ -143,10 +159,10 @@ /** * 跳转到企业微信客服 */ - jumpToWxWorkContact() { + jumpToWxWorkContact(contactUrl) { uni.navigateToMiniProgram({ appId: 'wxeb490c6f9b154ef9', // 企业微信小程序AppID - path: `pages/contacts/externalContactDetail?url=${encodeURIComponent(this.contactUrl)}`, + path: `pages/contacts/externalContactDetail?url=${encodeURIComponent(contactUrl)}`, success: () => { console.log('跳转企业微信成功'); this.$util.showToast({ @@ -164,14 +180,14 @@ /** * 打开用户资料 */ - openUserProfile() { + openUserProfile(contactId) { if (!this.wxWorkSDK) { this.showError('企业微信SDK未初始化'); return; } this.wxWorkSDK.addContact({ - userId: this.contactId + userId: contactId }, (res) => { console.log('打开用户资料成功:', res); }, (err) => { diff --git a/pages/contact/contact.vue b/pages/contact/contact.vue index 0f652ca..8b48055 100644 --- a/pages/contact/contact.vue +++ b/pages/contact/contact.vue @@ -22,6 +22,10 @@ { - if (res.code === 0 && res.data) { - this.wxworkConfig = { - enabled: true, - corpId: res.data.corp_id, - contactId: res.data.contact_id, - contactUrl: res.data.contact_url - }; - } else { - this.wxworkConfig = { enabled: false }; - } - }, - fail: () => { - this.wxworkConfig = { enabled: false }; - } - }); + // 从全局store获取企业微信配置 + const wxworkConfig = this.$store.state?.wxworkConfig; + if (wxworkConfig) { + this.wxworkConfig = { + enabled: true, + corpId: wxworkConfig.corp_id, + contactId: wxworkConfig.contact_id, + contactUrl: wxworkConfig.contact_url + }; + } else { + this.wxworkConfig = { enabled: false }; + } }, /** diff --git a/store/index.js b/store/index.js index 34eb0f6..2d875ee 100644 --- a/store/index.js +++ b/store/index.js @@ -64,6 +64,7 @@ const store = new Vuex.Store({ cartPosition: null, // 购物车所在位置 componentRefresh: 0, // 组件刷新 servicerConfig: null, // 客服配置 + wxworkConfig: null, // 企业微信配置 diySeckillInterval: 0, diyGroupPositionObj: {}, diyGroupShowModule: '', @@ -160,6 +161,11 @@ const store = new Vuex.Store({ state.servicerConfig = value; uni.setStorageSync('servicerConfig', value); }, + // 企业微信配置 + setWxworkConfig(state, value) { + state.wxworkConfig = value; + uni.setStorageSync('wxworkConfig', value); + }, setDiySeckillInterval(state, value) { state.diySeckillInterval = value; }, @@ -215,6 +221,11 @@ const store = new Vuex.Store({ this.commit('setSiteInfo', data.site_info); this.commit('setServicerConfig', data.servicer); + + // 企业微信配置 + if (data.wxwork) { + this.commit('setWxworkConfig', data.wxwork); + } this.commit('setCopyright', data.copyright);