chore: 企业微信客服组件完全独立

This commit is contained in:
2025-12-15 15:04:45 +08:00
parent 5e536afeae
commit 08583aa8aa
3 changed files with 70 additions and 45 deletions

View File

@@ -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) => {

View File

@@ -22,6 +22,10 @@
<wxwork-contact
v-if="wxworkConfig && wxworkConfig.enabled"
:corp-id="wxworkConfig.corpId"
:agent-id="wxworkConfig.agentId"
:timestamp="wxworkConfig.timestamp"
:nonce-str="wxworkConfig.nonceStr"
:signature="wxworkConfig.signature"
:contact-id="wxworkConfig.contactId"
:contact-url="wxworkConfig.contactUrl"
btn-text="企业微信客服"
@@ -208,24 +212,18 @@ export default {
* 加载企业微信配置
*/
loadWxWorkConfig() {
this.$api.sendRequest({
url: '/api/wxwork/config',
success: res => {
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 };
}
},
/**

View File

@@ -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;
},
@@ -216,6 +222,11 @@ const store = new Vuex.Store({
this.commit('setServicerConfig', data.servicer);
// 企业微信配置
if (data.wxwork) {
this.commit('setWxworkConfig', data.wxwork);
}
this.commit('setCopyright', data.copyright);
this.commit('setMapConfig', data.map_config);