Commit 9266ba84 authored by 水玉婷's avatar 水玉婷
Browse files

feat:添加appid从接口获取

parent a4284330
......@@ -21,7 +21,8 @@ class WeChatLogin {
async silentLogin() {
try {
// 直接从URL获取code
const code = this.getCodeFromURL()
const code = this.getCodeFromURL() || ""
if (!code) {
console.error('未找到code参数,请确保在企业微信内部应用中访问')
return {
......@@ -47,10 +48,11 @@ class WeChatLogin {
}
}
// 获取用户信息 - 使用fetch
// 获取用户信息 - 先执行auth接口,再执行模型信息接口
async getUserInfo(code) {
try {
const response = await fetch('/pedapi/pedService/wxcp/auth', {
// 第一步:执行auth接口获取用户信息和token
const authResponse = await fetch('/pedapi/pedService/wxcp/auth', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
......@@ -60,19 +62,50 @@ class WeChatLogin {
})
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
if (!authResponse.ok) {
throw new Error(`Auth接口HTTP错误! status: ${authResponse.status}`)
}
const result = await response.json()
const authResult = await authResponse.json()
if (result.code === 0) {
// 保存用户信息
localStorage.setItem('wechat_user', JSON.stringify(result.data))
return result.data
} else {
throw new Error(result.message || '获取用户信息失败')
if (authResult.code !== 0) {
throw new Error(authResult.message || '获取用户信息失败')
}
// 第二步:使用auth接口返回的token调用模型信息接口
const token = authResult.data.extMap?.sessionId || ''
if (!token) {
throw new Error('Auth接口未返回token')
}
const modelResponse = await fetch('/agentService/apps/getAppId', {
method: 'GET',
headers: {
'Token': token || '',
'x-session-id': token || '',
'Content-Type': 'application/json',
}
})
if (!modelResponse.ok) {
throw new Error(`模型信息接口HTTP错误! status: ${modelResponse.status}`)
}
const modelResult = await modelResponse.json()
if (modelResult.code !== 0) {
throw new Error(modelResult.message || '获取模型信息失败')
}
// 合并用户信息和模型信息
const userData = {
...authResult.data,
appId: modelResult.data.appId || ''
}
// 保存用户信息
localStorage.setItem('wechat_user', JSON.stringify(userData))
return userData
} catch (error) {
throw new Error(`获取用户信息失败: ${error.message}`)
}
......
......@@ -30,14 +30,14 @@
// 获取token
const userInfo = localStorage.getItem('wechat_user')
const {extMap = {}} = JSON.parse(userInfo || '{}')
const {extMap = {},appId = ''} = JSON.parse(userInfo || '{}')
const userToken = extMap.sessionId;
// 使用环境变量代替硬编码
const appCode = import.meta.env.VITE_APP_CODE || 'ped.qywx';
const time = new Date().getTime();
const chatParams = {
appId: '83b2664019a945d0a438abe6339758d8',
appId: appId || '83b2664019a945d0a438abe6339758d8', // 企业微信应用ID
stage: 'wechat-demo'+time,
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment