Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Q
qk_backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
quanku
qk_backend
Commits
055f9475
Commit
055f9475
authored
Oct 24, 2023
by
shiyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ai会话
parent
ee7f85ea
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
222 additions
and
19 deletions
+222
-19
ch-core/src/main/java/com/wwdz/ch/core/consts/IMEnum.java
ch-core/src/main/java/com/wwdz/ch/core/consts/IMEnum.java
+10
-0
ch-core/src/main/java/com/wwdz/ch/core/entity/im/IMChatRequestDto.java
...ain/java/com/wwdz/ch/core/entity/im/IMChatRequestDto.java
+3
-0
ch-core/src/main/java/com/wwdz/ch/core/entity/im/IMHistoryMsgRequestDto.java
...va/com/wwdz/ch/core/entity/im/IMHistoryMsgRequestDto.java
+4
-2
ch-core/src/main/java/com/wwdz/ch/core/entity/im/vo/HistoryMsgResponse.java
...ava/com/wwdz/ch/core/entity/im/vo/HistoryMsgResponse.java
+36
-0
ch-core/src/main/java/com/wwdz/ch/core/entity/im/vo/HistoryMsgVo.java
...main/java/com/wwdz/ch/core/entity/im/vo/HistoryMsgVo.java
+42
-0
ch-core/src/main/java/com/wwdz/ch/core/im/api/ConversationApi.java
...rc/main/java/com/wwdz/ch/core/im/api/ConversationApi.java
+81
-5
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/IMCallbackController.java
...rc/main/java/com/wwdz/ch/wx/api/IMCallbackController.java
+42
-10
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/ImServiceImpl.java
...-api/src/main/java/com/wwdz/ch/wx/impl/ImServiceImpl.java
+4
-2
No files found.
ch-core/src/main/java/com/wwdz/ch/core/consts/IMEnum.java
View file @
055f9475
...
...
@@ -36,6 +36,16 @@ public class IMEnum {
return
null
;
}
public
static
Integer
getCodeByValue
(
String
value
)
{
for
(
IMEnum
.
MsgTypeEnum
msgTypeEnum
:
IMEnum
.
MsgTypeEnum
.
values
())
{
if
(
msgTypeEnum
.
getValue
().
equals
(
value
))
{
return
msgTypeEnum
.
getCode
();
}
}
return
null
;
}
public
int
getCode
()
{
return
code
;
}
...
...
ch-core/src/main/java/com/wwdz/ch/core/entity/im/IMChatRequestDto.java
View file @
055f9475
...
...
@@ -4,6 +4,9 @@ import com.wwdz.ch.db.dto.request.BaseRequestDto;
import
com.xxdxxs.entity.Entity
;
import
lombok.Data
;
/**
* 拉取会话
*/
@Data
public
class
IMChatRequestDto
extends
BaseRequestDto
implements
Entity
{
...
...
ch-core/src/main/java/com/wwdz/ch/core/entity/im/IMHistoryMsgRequestDto.java
View file @
055f9475
...
...
@@ -3,6 +3,8 @@ package com.wwdz.ch.core.entity.im;
import
com.xxdxxs.entity.Entity
;
import
lombok.Data
;
import
java.util.Date
;
/**
* 历史聊天记录请求参数类
*/
...
...
@@ -27,12 +29,12 @@ public class IMHistoryMsgRequestDto implements Entity {
/**
* 请求的消息时间范围的最小值(单位:秒)
*/
private
Integer
minTime
;
private
Date
minTime
;
/**
* 请求的消息时间范围的最大值(单位:秒)
*/
private
Integer
maxTime
;
private
Date
maxTime
;
/**
* 上一次拉取到的最后一条消息的 MsgKey,续拉时需要填该字段
...
...
ch-core/src/main/java/com/wwdz/ch/core/entity/im/vo/HistoryMsgResponse.java
0 → 100644
View file @
055f9475
package
com.wwdz.ch.core.entity.im.vo
;
import
com.xxdxxs.entity.Entity
;
import
lombok.Data
;
import
java.util.List
;
@Data
public
class
HistoryMsgResponse
implements
Entity
{
private
List
<
HistoryMsgVo
>
dataList
;
/**
* 本次拉取到的消息里的最后一条消息的标识
*/
private
String
lastMsgKey
;
/**
* 本次拉取到的消息里的最后一条消息的时间
*/
private
Integer
lastMsgTime
;
/**
* 是否全部拉取:
* 0:表示未全部拉取,需要续拉
* 1:表示已全部拉取
*/
private
Integer
complete
;
/**
* 本次拉取到的消息条数
*/
private
Integer
msgCnt
;
}
ch-core/src/main/java/com/wwdz/ch/core/entity/im/vo/HistoryMsgVo.java
0 → 100644
View file @
055f9475
package
com.wwdz.ch.core.entity.im.vo
;
import
com.xxdxxs.entity.Entity
;
import
lombok.Data
;
@Data
public
class
HistoryMsgVo
implements
Entity
{
private
String
fromAccount
;
private
String
toAccount
;
private
String
msgKsy
;
/**
* 消息发送时间
*/
private
Long
msgTimeStamp
;
/**
* 该条消息的属性:
* 0:表示正常消息
* 8:表示被撤回的消息
*/
private
Integer
msgFlagBits
;
/**
* 消息接收方是否发送该条消息的已读回执。
* 0:表示未发送
* 1:表示已发送
*/
private
Integer
isPeerRead
;
/**
* 消息类型
*/
private
Integer
msgType
;
private
Object
msgContent
;
}
ch-core/src/main/java/com/wwdz/ch/core/im/api/ConversationApi.java
View file @
055f9475
...
...
@@ -4,14 +4,18 @@ package com.wwdz.ch.core.im.api;
import
com.wwdz.ch.core.consts.IMEnum
;
import
com.wwdz.ch.core.entity.im.IMChatMsg
;
import
com.wwdz.ch.core.entity.im.IMChatRequestDto
;
import
com.wwdz.ch.core.entity.im.IMHistoryMsgRequestDto
;
import
com.wwdz.ch.core.entity.im.SendChatMsgRequestDto
;
import
com.wwdz.ch.core.entity.im.vo.ConversationResponse
;
import
com.wwdz.ch.core.entity.im.vo.HistoryMsgResponse
;
import
com.wwdz.ch.core.entity.im.vo.HistoryMsgVo
;
import
com.wwdz.ch.core.entity.im.vo.IMChatVo
;
import
com.wwdz.ch.core.type.Result
;
import
com.wwdz.ch.core.util.IMUtil
;
import
com.wwdz.ch.core.util.OkHttpUtil
;
import
com.xxdxxs.utils.JsonUtils
;
import
com.xxdxxs.utils.MapUtils
;
import
com.xxdxxs.utils.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -32,6 +36,8 @@ public class ConversationApi {
private
static
final
String
GET_CHAT_LIST_URL
=
"https://console.tim.qq.com/v4/recentcontact/get_list"
;
private
static
final
String
GET_HISTORY_CHAT_URL
=
"https://console.tim.qq.com/v4/openim/admin_getroammsg"
;
@Autowired
TLSSigApi
tlsSigApi
;
...
...
@@ -43,7 +49,7 @@ public class ConversationApi {
* @param sendChatMsgRequestDto
* @return
*/
public
Result
sendChatMsg
(
SendChatMsgRequestDto
sendChatMsgRequestDto
)
{
public
Result
<
String
>
sendChatMsg
(
SendChatMsgRequestDto
sendChatMsgRequestDto
)
{
Random
random
=
new
Random
(
4294967295L
);
int
randomNum
=
random
.
nextInt
();
String
url
=
imUtil
.
formatIMUrl
(
SEND_MSG_URL
);
...
...
@@ -64,8 +70,9 @@ public class ConversationApi {
logger
.
error
(
"用户id : {}, 发送对话信息失败, errorCode : {}, errmsg : {} "
,
imChatMsg
.
getFrom_Account
(),
errorCode
,
errorInfo
);
return
Result
.
failed
(
errorInfo
);
}
String
msgKey
=
JsonUtils
.
getValueByPath
(
responseStr
,
"MsgKey"
);
logger
.
info
(
"============= 发送对话信息成功 ==========="
);
return
Result
.
success
();
return
Result
.
success
(
msgKey
);
}
...
...
@@ -127,12 +134,81 @@ public class ConversationApi {
/**
* 拉取历史聊天记录
* @param im
Chat
RequestDto
* @param im
HistoryMsg
RequestDto
* @return
*/
public
Result
getHistoryMsg
(
IMChatRequestDto
imChatRequestDto
)
{
public
Result
getHistoryMsg
(
IMHistoryMsgRequestDto
imHistoryMsgRequestDto
)
{
try
{
List
<
HistoryMsgVo
>
historyMsgVos
=
new
ArrayList
<>();
String
url
=
imUtil
.
formatIMUrl
(
GET_HISTORY_CHAT_URL
);
OkHttpUtil
okHttpUtil
=
OkHttpUtil
.
builder
().
url
(
url
);
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"Operator_Account"
,
imHistoryMsgRequestDto
.
getOperatorAccount
());
map
.
put
(
"Peer_Account"
,
imHistoryMsgRequestDto
.
getPeerAccount
());
map
.
put
(
"MaxCnt"
,
imHistoryMsgRequestDto
.
getMaxCnt
());
map
.
put
(
"MinTime"
,
imHistoryMsgRequestDto
.
getMinTime
().
getTime
());
map
.
put
(
"MaxTime"
,
imHistoryMsgRequestDto
.
getMaxTime
().
getTime
());
String
lastMsgKey
=
imHistoryMsgRequestDto
.
getLastMsgKey
();
if
(
StringUtils
.
hasLength
(
lastMsgKey
))
{
map
.
put
(
"LastMsgKey"
,
imHistoryMsgRequestDto
.
getLastMsgKey
());
}
logger
.
info
(
"============ 拉取历史聊天记录参数 : {}"
,
JsonUtils
.
from
(
map
));
okHttpUtil
.
addParams
(
map
);
okHttpUtil
.
post
(
true
);
String
responseStr
=
okHttpUtil
.
async
();
logger
.
info
(
"拉取历史聊天记录 response :{}"
,
responseStr
);
String
actionStatus
=
JsonUtils
.
getValueByPath
(
responseStr
,
"ActionStatus"
);
String
errorCode
=
JsonUtils
.
getValueByPath
(
responseStr
,
"ErrorCode"
);
String
errorInfo
=
JsonUtils
.
getValueByPath
(
responseStr
,
"ErrorInfo"
);
if
(!
"OK"
.
equals
(
actionStatus
))
{
logger
.
error
(
"用户id : {}, 拉取历史聊天记录失败, errorCode : {}, errmsg : {} "
,
imHistoryMsgRequestDto
.
getOperatorAccount
(),
errorCode
,
errorInfo
);
return
Result
.
failed
(
errorInfo
);
}
//是否拉取完成1:完成,0:未拉取完
int
complete
=
Integer
.
valueOf
(
JsonUtils
.
getValueByPath
(
responseStr
,
"Complete"
));
//本次拉取的消息条数
int
msgCnt
=
Integer
.
valueOf
(
JsonUtils
.
getValueByPath
(
responseStr
,
"MsgCnt"
));
int
lastMsgTime
=
Integer
.
valueOf
(
JsonUtils
.
getValueByPath
(
responseStr
,
"LastMsgTime"
));
lastMsgKey
=
JsonUtils
.
getValueByPath
(
responseStr
,
"LastMsgKey"
);
return
null
;
String
msgList
=
JsonUtils
.
getValueByPath
(
responseStr
,
"MsgList"
);
List
<
Map
<
String
,
Object
>>
msgMapList
=
JsonUtils
.
toMapList
(
msgList
);
for
(
Map
<
String
,
Object
>
m
:
msgMapList
)
{
String
fromAccount
=
m
.
get
(
"From_Account"
).
toString
();
String
toAccount
=
m
.
get
(
"To_Account"
).
toString
();
long
msgTimeStamp
=
(
long
)
m
.
get
(
"MsgTimeStamp"
);
int
msgFlagBits
=
(
int
)
m
.
get
(
"MsgFlagBits"
);
int
isPeerRead
=
(
int
)
m
.
get
(
"IsPeerRead"
);
String
msgKsy
=
m
.
get
(
"MsgKey"
).
toString
();
//消息体
String
msgBody
=
m
.
get
(
"MsgBody"
).
toString
();
List
<
Map
<
String
,
Object
>>
msgBodyList
=
JsonUtils
.
toMapList
(
msgBody
);
String
msgType
=
msgBodyList
.
get
(
0
).
get
(
"MsgType"
).
toString
();
String
contentJsonStr
=
msgBodyList
.
get
(
0
).
get
(
"MsgContent"
).
toString
();
HistoryMsgVo
historyMsgVo
=
new
HistoryMsgVo
();
historyMsgVo
.
setFromAccount
(
fromAccount
);
historyMsgVo
.
setToAccount
(
toAccount
);
historyMsgVo
.
setMsgType
(
IMEnum
.
MsgTypeEnum
.
getCodeByValue
(
msgType
));
historyMsgVo
.
setMsgContent
(
contentJsonStr
);
historyMsgVo
.
setIsPeerRead
(
isPeerRead
);
historyMsgVo
.
setMsgFlagBits
(
msgFlagBits
);
historyMsgVo
.
setMsgTimeStamp
(
msgTimeStamp
);
historyMsgVo
.
setMsgKsy
(
msgKsy
);
historyMsgVos
.
add
(
historyMsgVo
);
}
HistoryMsgResponse
historyMsgResponse
=
new
HistoryMsgResponse
();
historyMsgResponse
.
setDataList
(
historyMsgVos
);
historyMsgResponse
.
setMsgCnt
(
msgCnt
);
historyMsgResponse
.
setLastMsgKey
(
lastMsgKey
);
historyMsgResponse
.
setLastMsgTime
(
lastMsgTime
);
historyMsgResponse
.
setComplete
(
complete
);
logger
.
info
(
"============= 拉取历史聊天记录成功 ============="
);
return
Result
.
success
(
historyMsgResponse
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"拉取历史聊天记录 error:{}"
,
e
);
}
return
Result
.
failed
();
}
}
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/IMCallbackController.java
View file @
055f9475
package
com.wwdz.ch.wx.api
;
import
com.alibaba.fastjson.JSONObject
;
import
com.wwdz.ch.core.consts.IMEnum
;
import
com.wwdz.ch.db.domain.ImRecord
;
import
com.wwdz.ch.wx.service.ImRecordService
;
import
com.xxdxxs.utils.JsonUtils
;
import
com.xxdxxs.utils.StringUtils
;
...
...
@@ -10,6 +12,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
@RestController
@RequestMapping
(
"/imCallback"
)
public
class
IMCallbackController
{
...
...
@@ -27,33 +33,59 @@ public class IMCallbackController {
* 、Web(使用 Web SDK 发送请求)、Android、iOS、Windows、Mac、iPad、Unknown(使用未知类型的设备发送请求)
* @return
*/
@RequestMapping
(
value
=
"/getCallbackInfo
/{appid}
"
)
@RequestMapping
(
value
=
"/getCallbackInfo"
)
public
Object
getCallbackInfo
(
@PathVariable
String
appid
,
@RequestParam
(
required
=
false
,
name
=
"SdkAppid"
)
String
sdkAppid
,
@RequestParam
(
required
=
false
,
name
=
"OptPlatform"
)
String
optPlatform
,
@RequestParam
(
required
=
false
,
name
=
"CallbackCommand"
)
String
callbackCommand
,
@RequestBody
(
required
=
false
)
String
jsonStr
)
{
try
{
logger
.
info
(
"
接收推送信息校验
-> SdkAppid: {}, OptPlatform:{}, CallbackCommand:{}"
,
sdkAppid
,
optPlatform
,
callbackCommand
);
logger
.
info
(
"
消息发送之后的回调url参数
-> SdkAppid: {}, OptPlatform:{}, CallbackCommand:{}"
,
sdkAppid
,
optPlatform
,
callbackCommand
);
if
(!
sdkAppid
.
equals
(
String
.
valueOf
(
SDKAppID
)))
{
logger
.
info
(
">>>>>>>>>>> IM回调sdkappid 不一致 <<<<<<<<<<<"
);
return
false
;
}
logger
.
info
(
"回调推送的消息原文 -> {}"
,
jsonStr
);
logger
.
info
(
"消息发送之后的回调推送原文 -> {}"
,
jsonStr
);
ImRecord
imRecord
=
new
ImRecord
();
//发送消息后回调信息处理
String
fromAccount
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"From_Account"
);
String
toAccount
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"From_Account"
);
String
MsgTime
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"MsgTime"
);
String
MsgKey
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"MsgKey"
);
String
OnlineOnlyFlag
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"OnlineOnlyFlag"
);
String
SendMsgResult
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"SendMsgResult"
);
String
ErrorInfo
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"ErrorInfo"
);
long
msgTime
=
Long
.
valueOf
(
JsonUtils
.
getValueByPath
(
jsonStr
,
"MsgTime"
));
String
msgKey
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"MsgKey"
);
String
onlineOnlyFlag
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"OnlineOnlyFlag"
);
String
sendMsgResult
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"SendMsgResult"
);
String
errorInfo
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"ErrorInfo"
);
if
(!
"0"
.
equals
(
sendMsgResult
))
{
logger
.
error
(
"msgKey:{}, 消息下发失败错误信息:{}"
,
msgKey
,
errorInfo
);
imRecord
.
setState
(
IMEnum
.
MsgStateEnum
.
FAILED
.
getCode
());
}
else
{
imRecord
.
setState
(
IMEnum
.
MsgStateEnum
.
READED
.
getCode
());
}
//消息体
String
msgBody
=
JsonUtils
.
getValueByPath
(
jsonStr
,
"MsgBody"
);
JsonUtils
.
toMapList
(
msgBody
);
List
<
Map
<
String
,
Object
>>
msgBodyList
=
JsonUtils
.
toMapList
(
msgBody
);
String
msgType
=
msgBodyList
.
get
(
0
).
get
(
"MsgType"
).
toString
();
String
contentJsonStr
=
msgBodyList
.
get
(
0
).
get
(
"MsgContent"
).
toString
();
String
content
=
null
;
if
(
IMEnum
.
MsgTypeEnum
.
TIMTextElem
.
getValue
().
equals
(
msgType
))
{
content
=
JsonUtils
.
getValueByPath
(
contentJsonStr
,
"Text"
);
}
imRecord
.
setFromAccount
(
fromAccount
);
imRecord
.
setToAccount
(
toAccount
);
imRecord
.
setMsgTime
(
new
Date
(
msgTime
));
imRecord
.
setMsgKey
(
msgKey
);
imRecord
.
setMsgType
(
IMEnum
.
MsgTypeEnum
.
getCodeByValue
(
msgType
));
imRecord
.
setSendMsgResult
(
Integer
.
valueOf
(
sendMsgResult
));
imRecord
.
setOnlineOnlyFlag
(
Integer
.
valueOf
(
onlineOnlyFlag
));
imRecord
.
setMsgContent
(
content
);
Date
now
=
new
Date
();
imRecord
.
setCreateTime
(
now
);
imRecord
.
setUpdateTime
(
now
);
imRecordService
.
create
(
imRecord
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"
视频号回调推送
error : {}"
,
e
);
logger
.
error
(
"
消息发送之后的回调
error : {}"
,
e
);
}
return
"success"
;
}
...
...
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/ImServiceImpl.java
View file @
055f9475
...
...
@@ -116,7 +116,7 @@ public class ImServiceImpl implements IMService {
@Override
public
Result
sendAiChatMsg
(
SendChatMsgRequestDto
dto
)
{
try
{
Result
askResult
=
conversationApi
.
sendChatMsg
(
dto
);
Result
<
String
>
askResult
=
conversationApi
.
sendChatMsg
(
dto
);
if
(!
askResult
.
getSuccess
())
{
return
askResult
;
}
...
...
@@ -136,6 +136,7 @@ public class ImServiceImpl implements IMService {
imRecord
.
setCreateTime
(
now
);
imRecord
.
setUpdateTime
(
now
);
imRecord
.
setState
(
IMEnum
.
MsgStateEnum
.
READED
.
getCode
());
imRecord
.
setMsgKey
(
askResult
.
getData
());
imRecordService
.
create
(
imRecord
);
...
...
@@ -156,7 +157,7 @@ public class ImServiceImpl implements IMService {
if
(
redisUtils
.
hasKey
(
dto
.
getReqId
())){
return
Result
.
success
(
"回答已被终止"
);
}
Result
answerResult
=
conversationApi
.
sendChatMsg
(
answerDto
);
Result
<
String
>
answerResult
=
conversationApi
.
sendChatMsg
(
answerDto
);
if
(!
answerResult
.
getSuccess
())
{
logger
.
info
(
"=============== 发送问题的ai答复消息失败 ============="
);
return
answerResult
;
...
...
@@ -166,6 +167,7 @@ public class ImServiceImpl implements IMService {
ImRecord
answerImRecord
=
new
ImRecord
();
answerImRecord
.
setFromAccount
(
dto
.
getToAccount
());
answerImRecord
.
setToAccount
(
dto
.
getFromAccount
());
imRecord
.
setMsgKey
(
answerResult
.
getData
());
answerImRecord
.
setMsgType
(
IMEnum
.
MsgTypeEnum
.
TIMTextElem
.
getCode
());
answerImRecord
.
setMsgContent
(
answer
);
answerImRecord
.
setOnlineOnlyFlag
(
0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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