This commit is contained in:
richboo111
2023-05-05 14:48:54 +08:00
parent 2b7bfe4e5e
commit 82cd796b5c
11 changed files with 343 additions and 188 deletions

View File

@@ -5,6 +5,10 @@ import (
"errors"
"fmt"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/baseapi/utils/errlist"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/baseapi/platformapi/ebaiapi"
@@ -19,7 +23,6 @@ import (
// SendToVendor 向平台发消息
func SendToVendor(msg []byte) {
var (
//w http.ResponseWriter
sendData SendData
err error
elmAppID = api.EbaiAPI.GetSource()
@@ -36,7 +39,11 @@ func SendToVendor(msg []byte) {
//发送信息
if sendData.VendorID == VendorIDMT {
temp, _ := json.Marshal(sendData.Data)
Send(temp)
if sendData.Data.(map[string]interface{})["app_id"] == nil {
globals.SugarLogger.Debug("SendToVendor appId=null")
return
}
Send(temp, sendData.Data.(map[string]interface{})["app_id"])
}
if sendData.VendorID == VendorIDELM {
param := sendData.Data.(ebaiapi.BusinessSendMsgReq)
@@ -46,23 +53,24 @@ func SendToVendor(msg []byte) {
}
}
if err != nil {
ClientRender(Fail, FailMsg, map[string]string{
"errMsg": fmt.Sprintf("%v", err),
})
return
} else {
ClientRender(SuccessCode, SuccessMsg, map[string]interface{}{
"vendorID": sendData.VendorID,
"msg": "ok",
})
return
}
//if err != nil {
// ClientRender(Fail, FailMsg, map[string]string{
// "errMsg": fmt.Sprintf("%v", err),
// })
// return err
//} else {
// ClientRender(SuccessCode, SuccessMsg, map[string]interface{}{
// "vendorID": sendData.VendorID,
// "msg": "ok",
// })
// return nil
//}
return
}
func Send(data []byte) {
//生成完整url
fullUrl := GenFullUrl() //clientID暂时不用
func Send(data []byte, appID interface{}) {
//根据appID生成完整url
fullUrl := GenFullUrl(appID.(float64)) //clientID暂时不用
conn, resp, err := websocket.DefaultDialer.Dial(fullUrl, nil)
if err != nil || resp.StatusCode != 101 {
@@ -83,11 +91,11 @@ func Send(data []byte) {
for {
_, msg, err := conn.ReadMessage()
temp := string(msg)
if err != nil {
break
} else {
temp := string(msg)
if temp != HeartCheckSuccess || temp != "成功" {
if temp != HeartSuccessWord {
ReadMsgFromVendor(VendorIDMT, "", msg)
}
}
@@ -96,6 +104,83 @@ func Send(data []byte) {
return
}
// MtInit 发送心跳
func MtInit() {
data := []byte(HeartCheckMsg)
//生成完整url
url := GenFullUrl2()
//主连接
jxutils.CallMsgHandlerAsync(func() {
conn, resp, err := websocket.DefaultDialer.Dial(url.UrlMain, nil)
if err != nil || resp.StatusCode != 101 {
fmt.Printf("连接失败:%v http响应不成功", err)
}
//关闭
defer func(conn *websocket.Conn) {
err := conn.Close()
if err != nil {
return
}
}(conn)
//client连接事件
client := NewClient(url.ClientIDMain, conn, ClientTypeMt)
Manager.Connect <- client
err = conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
fmt.Println(err)
}
for {
_, msg, err := conn.ReadMessage()
temp := string(msg)
if err != nil || temp != "HB" {
break
}
fmt.Printf("%s receive: %s\n", conn.RemoteAddr(), string(msg))
}
}, url.ClientIDMain)
//副连接
if url.UrlSub != "" {
jxutils.CallMsgHandlerAsync(func() {
connSub, respSub, errSub := websocket.DefaultDialer.Dial(url.UrlSub, nil)
if errSub != nil || respSub.StatusCode != 101 {
fmt.Printf("连接失败:%v http响应不成功", errSub)
}
//关闭
defer func(conn *websocket.Conn) {
err := conn.Close()
if err != nil {
return
}
}(connSub)
//client连接事件
client := NewClient(url.ClientIDSub, connSub, ClientTypeMt)
Manager.Connect <- client
errSub = connSub.WriteMessage(websocket.TextMessage, data)
if errSub != nil {
fmt.Println(errSub)
}
for {
_, msg, err := connSub.ReadMessage()
temp := string(msg)
if err != nil || temp != HeartCheckSuccess {
break
}
fmt.Printf("%s connSub:receive: %s\n", connSub.RemoteAddr(), string(msg))
}
}, url.ClientIDSub)
}
}
// ReadMsgFromClient 存储客户端发送的消息
func ReadMsgFromClient(vendorID int, elmAppID string, msg interface{}) {
var (
@@ -221,13 +306,8 @@ func PushMsgByCid(vendorStoreID string, vendorID int) error {
func SetMessageDetail(req *JXMsg, vendorID int, elmAppID string) error {
//生成京西消息ID detail
msgID := GenMsgDetailID(req, vendorID, elmAppID)
err := rdb.Set("test", "可以插入数据sjdfoiqaj", ExpireTimeDay)
if err != nil {
globals.SugarLogger.Debugf("测试插入err:%v", err)
}
data, _ := json.Marshal(req)
err = rdb.RPush(msgID, string(data))
globals.SugarLogger.Debugf("im SetUserList err=%v", err)
err := rdb.RPush(msgID, string(data))
ok, err := rdb.ExpireResult(msgID, ExpireTimeDay)
if err != nil || !ok {
return err
@@ -251,12 +331,6 @@ func SetUserList(jxMsg *JXMsg, userList *UserMessageList, vendorID int, elmAppID
//存储当前数据
data, _ := json.Marshal(userList)
err = rdb.RPush(msgID, string(data))
globals.SugarLogger.Debugf("im SetUserList msgID=%s", msgID)
globals.SugarLogger.Debugf("im SetUserList err=%v", err)
//test
str := rdb.Get(msgID)
globals.SugarLogger.Debugf("im SetUserList str=%v", str)
//over
ok, err := rdb.ExpireResult(msgID, ExpireTimeDay)
if err != nil || !ok {
return err
@@ -331,6 +405,8 @@ func GetImUserList(req []RelInfo) (map[string][]interface{}, error) {
temp := rdb.LRange(j)
for _, v := range temp {
retVal[j] = append(retVal[j], v)
//暂时写死
//retVal["userList"] = append(retVal["userList"], v)
}
}
return retVal, nil
@@ -351,6 +427,7 @@ func GetImChatDetail(req []UserRelInfo) (map[string][]interface{}, error) {
temp := rdb.LRange(j)
for _, v := range temp {
retVal[j] = append(retVal[j], v)
//retVal["chatDetail"] = append(retVal["chatDetail"], v)
}
}
return retVal, nil
@@ -388,3 +465,18 @@ func SetJxMsgRead(appID, vendorStoreID, vendorID, userID string) error {
}
return nil
}
// DelRedisByKey 清除redis数据
func DelRedisByKey(keys []string) {
var errList errlist.ErrList
for _, key := range keys {
err := rdb.Del(key)
if err != nil {
errList.AddErr(err)
}
}
if errList.GetErrListAsOne() != nil {
globals.SugarLogger.Debugf("DelRedisByKey err=%v", errList.GetErrListAsOne())
}
return
}