Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
水玉婷
ai-wechat
Commits
9266ba84
Commit
9266ba84
authored
Feb 05, 2026
by
水玉婷
Browse files
feat:添加appid从接口获取
parent
a4284330
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/utils/wechat.js
View file @
9266ba84
...
...
@@ -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
(
!
r
esponse
.
ok
)
{
throw
new
Error
(
`
HTTP error
! status:
${
r
esponse
.
status
}
`
)
if
(
!
authR
esponse
.
ok
)
{
throw
new
Error
(
`
Auth接口HTTP错误
! status:
${
authR
esponse
.
status
}
`
)
}
const
r
esult
=
await
r
esponse
.
json
()
const
authR
esult
=
await
authR
esponse
.
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
}
`
)
}
...
...
src/views/Home.vue
View file @
9266ba84
...
...
@@ -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
,
};
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment