feat(im): add WeChat integration with QR code login and long-polling support
- Extended the IMChannel interface to include WeChat as a platform, supporting long-polling mode and full output mode. - Implemented WeChat QR code login functionality, allowing users to authenticate via a QR code. - Added new API endpoints for generating and polling the status of WeChat QR codes. - Updated the frontend to handle WeChat-specific UI elements and interactions, including dynamic form adjustments based on the selected platform. - Enhanced localization files to include WeChat-related translations. - Introduced a WeChat adapter for message handling and file downloading via the iLink Bot API.
This commit is contained in:
@@ -89,13 +89,14 @@
|
||||
<!-- Platform -->
|
||||
<div class="form-item">
|
||||
<label class="form-label">{{ $t('agentEditor.im.platform') }}</label>
|
||||
<t-radio-group v-model="formData.platform" :disabled="!!editingChannel">
|
||||
<t-radio-group v-model="formData.platform" :disabled="!!editingChannel" @change="onPlatformChange">
|
||||
<t-radio-button value="wecom">{{ $t('agentEditor.im.wecom') }}</t-radio-button>
|
||||
<t-radio-button value="feishu">{{ $t('agentEditor.im.feishu') }}</t-radio-button>
|
||||
<t-radio-button value="slack">{{ $t('agentEditor.im.slack') }}</t-radio-button>
|
||||
<t-radio-button value="telegram">{{ $t('agentEditor.im.telegram') }}</t-radio-button>
|
||||
<t-radio-button value="dingtalk">{{ $t('agentEditor.im.dingtalk') }}</t-radio-button>
|
||||
<t-radio-button value="mattermost">{{ $t('agentEditor.im.mattermost') }}</t-radio-button>
|
||||
<t-radio-button value="wechat">{{ $t('agentEditor.im.wechat') }}</t-radio-button>
|
||||
</t-radio-group>
|
||||
</div>
|
||||
|
||||
@@ -105,8 +106,8 @@
|
||||
<t-input v-model="formData.name" :placeholder="$t('agentEditor.im.channelNamePlaceholder')" />
|
||||
</div>
|
||||
|
||||
<!-- Mode -->
|
||||
<div class="form-item">
|
||||
<!-- Mode (hidden for WeChat) -->
|
||||
<div v-if="formData.platform !== 'wechat'" class="form-item">
|
||||
<label class="form-label">{{ $t('agentEditor.im.mode') }}</label>
|
||||
<t-radio-group v-model="formData.mode">
|
||||
<t-radio-button value="websocket" :disabled="formData.platform === 'mattermost'">WebSocket</t-radio-button>
|
||||
@@ -116,8 +117,8 @@
|
||||
<p v-else class="form-hint">{{ $t('agentEditor.im.modeHint') }}</p>
|
||||
</div>
|
||||
|
||||
<!-- Output mode -->
|
||||
<div class="form-item">
|
||||
<!-- Output mode (hidden for WeChat) -->
|
||||
<div v-if="formData.platform !== 'wechat'" class="form-item">
|
||||
<label class="form-label">{{ $t('agentEditor.im.outputMode') }}</label>
|
||||
<t-radio-group v-model="formData.output_mode">
|
||||
<t-radio-button value="stream">{{ $t('agentEditor.im.outputStream') }}</t-radio-button>
|
||||
@@ -339,16 +340,62 @@
|
||||
<p class="form-hint">{{ $t('agentEditor.im.mattermostPostToMainHint') }}</p>
|
||||
</div>
|
||||
</template>
|
||||
<!-- WeChat credentials (QR code binding) -->
|
||||
<template v-if="formData.platform === 'wechat'">
|
||||
<p class="form-hint">{{ $t('agentEditor.im.wechatHint') }}</p>
|
||||
|
||||
<!-- Already bound state -->
|
||||
<div v-if="wechatBound" class="wechat-bound-status">
|
||||
<t-icon name="check-circle-filled" class="bound-icon" />
|
||||
<span>{{ $t('agentEditor.im.wechatBindSuccess') }}</span>
|
||||
<t-button size="small" variant="outline" theme="default" @click="startWeChatBinding">
|
||||
{{ $t('agentEditor.im.wechatRebind') }}
|
||||
</t-button>
|
||||
</div>
|
||||
|
||||
<!-- QR code binding flow -->
|
||||
<div v-else class="wechat-qr-section">
|
||||
<!-- Initial state: show bind button -->
|
||||
<div v-if="!wechatQRImgUrl" class="wechat-bind-action">
|
||||
<t-button theme="default" variant="outline" :loading="wechatLoading" @click="startWeChatBinding">
|
||||
<template #icon><t-icon name="scan" /></template>
|
||||
{{ $t('agentEditor.im.wechatScanBind') }}
|
||||
</t-button>
|
||||
</div>
|
||||
|
||||
<!-- QR code displayed -->
|
||||
<div v-else class="wechat-qr-display">
|
||||
<div class="qr-container">
|
||||
<img :src="wechatQRImgUrl" alt="WeChat QR Code" class="qr-image" />
|
||||
<div v-if="wechatQRStatus === 'expired'" class="qr-expired-overlay" @click="startWeChatBinding">
|
||||
<t-icon name="refresh" class="refresh-icon" />
|
||||
<span>{{ $t('agentEditor.im.wechatQRExpired') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<p class="qr-hint">
|
||||
<template v-if="wechatQRStatus === 'scaned'">
|
||||
{{ $t('agentEditor.im.wechatBinding') }}
|
||||
</template>
|
||||
<template v-else>
|
||||
{{ $t('agentEditor.im.wechatScanning') }}
|
||||
</template>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</t-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, watch } from 'vue';
|
||||
import { ref, onMounted, watch, onUnmounted, computed } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { MessagePlugin } from 'tdesign-vue-next';
|
||||
import { listIMChannels, createIMChannel, updateIMChannel, deleteIMChannel, toggleIMChannel } from '@/api/agent';
|
||||
import {
|
||||
listIMChannels, createIMChannel, updateIMChannel, deleteIMChannel, toggleIMChannel,
|
||||
getWeChatQRCode, pollWeChatQRCodeStatus,
|
||||
} from '@/api/agent';
|
||||
import { listKnowledgeBases } from '@/api/knowledge-base';
|
||||
import type { IMChannel } from '@/api/agent';
|
||||
|
||||
@@ -366,12 +413,21 @@ const editingChannel = ref<IMChannel | null>(null);
|
||||
// Knowledge base options for file-to-KB feature
|
||||
const knowledgeBases = ref<{ id: string; name: string }[]>([]);
|
||||
|
||||
// WeChat QR code binding state
|
||||
const wechatQRContent = ref(''); // raw text to encode as QR code
|
||||
const wechatQRImgUrl = ref(''); // generated QR image URL
|
||||
const wechatQRCode = ref(''); // opaque token for polling status
|
||||
const wechatQRStatus = ref<string>('');
|
||||
const wechatLoading = ref(false);
|
||||
let wechatPollActive = false;
|
||||
let wechatPollTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const defaultCredentials = (): Record<string, any> => ({});
|
||||
|
||||
const formData = ref({
|
||||
platform: 'wecom' as 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost',
|
||||
platform: 'wecom' as 'wecom' | 'feishu' | 'slack' | 'telegram' | 'dingtalk' | 'mattermost' | 'wechat',
|
||||
name: '',
|
||||
mode: 'websocket' as 'webhook' | 'websocket',
|
||||
mode: 'websocket' as 'webhook' | 'websocket' | 'longpoll',
|
||||
output_mode: 'stream' as 'stream' | 'full',
|
||||
session_mode: 'user' as 'user' | 'thread',
|
||||
knowledge_base_id: '',
|
||||
@@ -401,6 +457,101 @@ watch(
|
||||
}
|
||||
},
|
||||
);
|
||||
// Whether WeChat credentials are already bound
|
||||
const wechatBound = computed(() => {
|
||||
return formData.value.platform === 'wechat' &&
|
||||
formData.value.credentials.bot_token &&
|
||||
formData.value.credentials.ilink_bot_id;
|
||||
});
|
||||
|
||||
|
||||
function onPlatformChange(val: string | number | boolean) {
|
||||
formData.value.credentials = defaultCredentials();
|
||||
stopWeChatPolling();
|
||||
wechatQRContent.value = '';
|
||||
wechatQRImgUrl.value = '';
|
||||
wechatQRCode.value = '';
|
||||
wechatQRStatus.value = '';
|
||||
// WeChat uses fixed mode/output
|
||||
if (val === 'wechat') {
|
||||
formData.value.mode = 'longpoll';
|
||||
formData.value.output_mode = 'full';
|
||||
} else {
|
||||
formData.value.mode = 'websocket';
|
||||
formData.value.output_mode = 'stream';
|
||||
}
|
||||
}
|
||||
|
||||
async function startWeChatBinding() {
|
||||
stopWeChatPolling();
|
||||
wechatLoading.value = true;
|
||||
wechatQRContent.value = '';
|
||||
wechatQRImgUrl.value = '';
|
||||
wechatQRStatus.value = '';
|
||||
|
||||
try {
|
||||
const res = await getWeChatQRCode();
|
||||
// qrcode_url is the text content to encode as QR code (e.g. a weixin:// URL)
|
||||
wechatQRContent.value = res.data.qrcode_url;
|
||||
wechatQRCode.value = res.data.qrcode;
|
||||
wechatQRStatus.value = 'wait';
|
||||
|
||||
// Generate QR code image via public API (no extra npm dependency needed)
|
||||
wechatQRImgUrl.value = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(res.data.qrcode_url)}`;
|
||||
|
||||
// Start long-polling for scan status
|
||||
startStatusPolling();
|
||||
} catch (e: any) {
|
||||
MessagePlugin.error(e?.message || 'Failed to generate QR code');
|
||||
} finally {
|
||||
wechatLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function startStatusPolling() {
|
||||
wechatPollActive = true;
|
||||
pollOnce();
|
||||
}
|
||||
|
||||
async function pollOnce() {
|
||||
if (!wechatPollActive) return;
|
||||
try {
|
||||
const statusRes = await pollWeChatQRCodeStatus(wechatQRCode.value);
|
||||
if (!wechatPollActive) return;
|
||||
wechatQRStatus.value = statusRes.data.status;
|
||||
|
||||
if (statusRes.data.status === 'confirmed' && statusRes.data.credentials) {
|
||||
formData.value.credentials = {
|
||||
bot_token: statusRes.data.credentials.bot_token,
|
||||
ilink_bot_id: statusRes.data.credentials.ilink_bot_id,
|
||||
ilink_user_id: statusRes.data.credentials.ilink_user_id,
|
||||
};
|
||||
stopWeChatPolling();
|
||||
wechatQRContent.value = '';
|
||||
wechatQRImgUrl.value = '';
|
||||
MessagePlugin.success(t('agentEditor.im.wechatBindSuccess'));
|
||||
return;
|
||||
}
|
||||
if (statusRes.data.status === 'expired') {
|
||||
stopWeChatPolling();
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// transient error
|
||||
}
|
||||
// Schedule next poll with a short delay (the backend already long-polled ~35s)
|
||||
if (wechatPollActive) {
|
||||
wechatPollTimer = setTimeout(pollOnce, 500);
|
||||
}
|
||||
}
|
||||
|
||||
function stopWeChatPolling() {
|
||||
wechatPollActive = false;
|
||||
if (wechatPollTimer) {
|
||||
clearTimeout(wechatPollTimer);
|
||||
wechatPollTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadChannels() {
|
||||
loading.value = true;
|
||||
@@ -461,6 +612,11 @@ function editChannel(channel: IMChannel) {
|
||||
|
||||
function resetForm() {
|
||||
editingChannel.value = null;
|
||||
stopWeChatPolling();
|
||||
wechatQRContent.value = '';
|
||||
wechatQRImgUrl.value = '';
|
||||
wechatQRCode.value = '';
|
||||
wechatQRStatus.value = '';
|
||||
formData.value = {
|
||||
platform: 'wecom',
|
||||
name: '',
|
||||
@@ -474,6 +630,12 @@ function resetForm() {
|
||||
|
||||
async function handleSave() {
|
||||
try {
|
||||
// For WeChat, validate that credentials are bound
|
||||
if (formData.value.platform === 'wechat' && !formData.value.credentials.bot_token) {
|
||||
MessagePlugin.warning(t('agentEditor.im.wechatScanBind'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (editingChannel.value) {
|
||||
await updateIMChannel(editingChannel.value.id, {
|
||||
name: formData.value.name,
|
||||
@@ -527,6 +689,10 @@ async function handleDelete(id: string) {
|
||||
onMounted(() => {
|
||||
loadChannels();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
stopWeChatPolling();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
@@ -670,6 +836,11 @@ onMounted(() => {
|
||||
background: rgba(25, 42, 77, 0.08);
|
||||
color: #192a4d;
|
||||
}
|
||||
|
||||
&.wechat {
|
||||
background: rgba(7, 193, 96, 0.08);
|
||||
color: #07c160;
|
||||
}
|
||||
}
|
||||
|
||||
.channel-name {
|
||||
@@ -815,4 +986,86 @@ onMounted(() => {
|
||||
color: var(--td-text-color-placeholder);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
// --- WeChat QR code binding ---
|
||||
.wechat-bound-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 12px 16px;
|
||||
background: rgba(7, 193, 96, 0.06);
|
||||
border: 1px solid rgba(7, 193, 96, 0.2);
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
color: var(--td-text-color-primary);
|
||||
|
||||
.bound-icon {
|
||||
font-size: 18px;
|
||||
color: #07c160;
|
||||
}
|
||||
}
|
||||
|
||||
.wechat-qr-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.wechat-bind-action {
|
||||
padding: 24px 0;
|
||||
}
|
||||
|
||||
.wechat-qr-display {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 16px 0;
|
||||
}
|
||||
|
||||
.qr-container {
|
||||
position: relative;
|
||||
width: 200px;
|
||||
height: 200px;
|
||||
border: 1px solid var(--td-component-stroke);
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
// QR code images are always black-on-white; force white background
|
||||
// so the code remains scannable in dark mode.
|
||||
background: #fff;
|
||||
|
||||
.qr-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
}
|
||||
|
||||
.qr-expired-overlay {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
background: rgba(0, 0, 0, 0.6);
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
|
||||
.refresh-icon {
|
||||
font-size: 24px;
|
||||
}
|
||||
}
|
||||
|
||||
.qr-hint {
|
||||
font-size: 13px;
|
||||
color: var(--td-text-color-secondary);
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user