imv2
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils/errlist"
|
||||
|
||||
@@ -17,192 +17,53 @@ import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
push "git.rosy.net.cn/jx-callback/business/jxutils/unipush"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// SendToVendor 向平台发消息
|
||||
func SendToVendor(msg []byte) {
|
||||
var (
|
||||
sendData SendData
|
||||
err error
|
||||
elmAppID = api.EbaiAPI.GetSource()
|
||||
)
|
||||
|
||||
//解析数据
|
||||
if err = json.Unmarshal(msg, &sendData); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//存储数据
|
||||
ReadMsgFromClient(sendData.VendorID, elmAppID, sendData.Data)
|
||||
|
||||
//发送信息
|
||||
if sendData.VendorID == VendorIDMT {
|
||||
temp, _ := json.Marshal(sendData.Data)
|
||||
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"])
|
||||
return
|
||||
}
|
||||
if sendData.VendorID == VendorIDELM {
|
||||
param := sendData.Data.(ebaiapi.BusinessSendMsgReq)
|
||||
if err := api.EbaiAPI.BusinessSendMsg(¶m); err != nil {
|
||||
globals.SugarLogger.Debugf("elm发送信息错误:%v", err)
|
||||
return
|
||||
func SendVendorV2(data SendData) (err error) {
|
||||
if data.VendorID == model.VendorIDMTWM {
|
||||
dataStr, _ := json.Marshal(data.Data)
|
||||
temp := string(dataStr)
|
||||
fmt.Println(temp)
|
||||
if _, err = api.MtwmAPI.MsgSend(string(dataStr)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
fmt.Printf("连接失败:%v http响应不成功", err)
|
||||
}
|
||||
//关闭
|
||||
defer func(conn *websocket.Conn) {
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}(conn)
|
||||
|
||||
err = conn.WriteMessage(websocket.TextMessage, data)
|
||||
//if data.VendorID == model.VendorIDEBAI { //todo 后续添加
|
||||
// err = nil
|
||||
//}
|
||||
err = ReadMsgFromClient(data.VendorID, "", data.Data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
globals.SugarLogger.Debugf("SendVendorV2:%v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
_, msg, err := conn.ReadMessage()
|
||||
temp := string(msg)
|
||||
res := JsonCommon(HeartSuccessWord)
|
||||
fmt.Printf("Send %s receive: %s\n", conn.RemoteAddr(), string(msg))
|
||||
if err != nil {
|
||||
break
|
||||
} else if temp == res {
|
||||
continue
|
||||
} else {
|
||||
ReadMsgFromVendor(VendorIDMT, "", msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
res := JsonCommon(HeartCheckSuccess)
|
||||
fmt.Printf("MtInit %s receive: %s\n", conn.RemoteAddr(), string(msg))
|
||||
if err != nil {
|
||||
break
|
||||
} else if temp == res {
|
||||
continue
|
||||
} else {
|
||||
ReadMsgFromVendor(VendorIDMT, "", 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)
|
||||
res := JsonCommon(HeartCheckSuccess)
|
||||
if err != nil || temp == res {
|
||||
break
|
||||
} else {
|
||||
ReadMsgFromVendor(VendorIDMT, "", msg)
|
||||
}
|
||||
fmt.Printf("MtInit %s connSub:receive: %s\n", connSub.RemoteAddr(), string(msg))
|
||||
}
|
||||
|
||||
}, url.ClientIDSub)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadMsgFromClient 存储客户端发送的消息
|
||||
func ReadMsgFromClient(vendorID int, elmAppID string, msg interface{}) {
|
||||
func ReadMsgFromClient(vendorID int, elmAppID string, msg interface{}) error {
|
||||
var (
|
||||
err error
|
||||
jxMsg = &JXMsg{}
|
||||
errList errlist.ErrList
|
||||
userList = &UserMessageList{}
|
||||
)
|
||||
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return
|
||||
errList.AddErr(fmt.Errorf("json处理数据错误:%v", err))
|
||||
}
|
||||
|
||||
if vendorID == VendorIDMT {
|
||||
var MtSingleChat = mtwmapi.SingleChat{}
|
||||
err = json.Unmarshal(data, &MtSingleChat)
|
||||
var pushContent = mtwmapi.PushContentReq{}
|
||||
err = json.Unmarshal(data, &pushContent)
|
||||
jxMsg = &JXMsg{
|
||||
SendType: SendTypeJx,
|
||||
MsgContent: MtSingleChat,
|
||||
MsgContent: pushContent,
|
||||
}
|
||||
userList = &UserMessageList{
|
||||
VendorID: VendorIDMT,
|
||||
UserID: utils.Int2Str(MtSingleChat.OpenUserID),
|
||||
LatestMsg: MtSingleChat.MsgContent,
|
||||
LatestTime: MtSingleChat.Cts,
|
||||
UserID: utils.Int2Str(pushContent.OpenUserID),
|
||||
LatestMsg: pushContent.MsgContent,
|
||||
LatestTime: pushContent.Cts,
|
||||
}
|
||||
}
|
||||
if vendorID == VendorIDELM {
|
||||
@@ -222,41 +83,44 @@ func ReadMsgFromClient(vendorID int, elmAppID string, msg interface{}) {
|
||||
|
||||
//1 存储详细聊天记录list
|
||||
if err = SetMessageDetail(jxMsg, vendorID, elmAppID); err != nil {
|
||||
globals.SugarLogger.Debugf("ReadMsgFromClient SetMessageDetail err:=%v\n", err)
|
||||
//return
|
||||
errList.AddErr(fmt.Errorf("存储详细聊天记录错误:%v", err))
|
||||
}
|
||||
//2 存储展示列表时单条数据
|
||||
if err = SetUserList(jxMsg, userList, vendorID, elmAppID); err != nil {
|
||||
globals.SugarLogger.Debugf("ReadMsgFromClient SetUserList err:=%v\n", err)
|
||||
//return
|
||||
errList.AddErr(fmt.Errorf("存储STU聊天记录错误:%v", err))
|
||||
}
|
||||
if errList.GetErrListAsOne() != nil {
|
||||
return fmt.Errorf("ReadMsgFromClient:%v", errList.GetErrListAsOne())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadMsgFromVendor 读取数据并存储到redis
|
||||
func ReadMsgFromVendor(vendorID int, elmAppID string, msg []byte) {
|
||||
if string(msg) == "" {
|
||||
return
|
||||
}
|
||||
func ReadMsgFromVendor(vendorID int, elmAppID string, msg []byte) error {
|
||||
var (
|
||||
err error
|
||||
//vendorStoreID string
|
||||
jxMsg = &JXMsg{}
|
||||
userList = &UserMessageList{}
|
||||
err error
|
||||
jxMsg = &JXMsg{}
|
||||
vendorStoreID string
|
||||
errList errlist.ErrList
|
||||
userList = &UserMessageList{}
|
||||
)
|
||||
if string(msg) == "" {
|
||||
errList.AddErr(fmt.Errorf("读取平台数据为空,请检查"))
|
||||
}
|
||||
if vendorID == VendorIDMT {
|
||||
var MtSingleChat = mtwmapi.SingleChat{}
|
||||
err = json.Unmarshal(msg, &MtSingleChat)
|
||||
var PushContentReq = mtwmapi.PushContentReq{}
|
||||
err = json.Unmarshal(msg, &PushContentReq)
|
||||
jxMsg = &JXMsg{
|
||||
SendType: SendTypeMt,
|
||||
MsgContent: MtSingleChat,
|
||||
MsgContent: PushContentReq,
|
||||
}
|
||||
userList = &UserMessageList{
|
||||
VendorID: VendorIDMT,
|
||||
UserID: utils.Int2Str(MtSingleChat.OpenUserID),
|
||||
LatestMsg: MtSingleChat.MsgContent,
|
||||
LatestTime: MtSingleChat.Cts,
|
||||
UserID: utils.Int2Str(PushContentReq.OpenUserID),
|
||||
LatestMsg: PushContentReq.MsgContent,
|
||||
LatestTime: PushContentReq.Cts,
|
||||
}
|
||||
//vendorStoreID = MtSingleChat.AppPoiCode
|
||||
vendorStoreID = PushContentReq.AppPoiCode
|
||||
}
|
||||
if vendorID == VendorIDELM {
|
||||
var ElmData = ebaiapi.ImMessageSend{}
|
||||
@@ -275,23 +139,21 @@ func ReadMsgFromVendor(vendorID int, elmAppID string, msg []byte) {
|
||||
|
||||
//1 存储详细聊天记录list
|
||||
if err = SetMessageDetail(jxMsg, vendorID, elmAppID); err != nil {
|
||||
globals.SugarLogger.Debugf("ReadMsgFromVendor SetMessageDetail err:=%v\n", err)
|
||||
//return
|
||||
errList.AddErr(fmt.Errorf("存储详细聊天记录错误:%v", err))
|
||||
}
|
||||
//2 存储展示列表时单条数据
|
||||
if err = SetUserList(jxMsg, userList, vendorID, elmAppID); err != nil {
|
||||
globals.SugarLogger.Debugf("ReadMsgFromVendor SetUserList err:=%v\n", err)
|
||||
//return
|
||||
errList.AddErr(fmt.Errorf("存储STU聊天记录错误:%v", err))
|
||||
}
|
||||
//3 cid推送新消息
|
||||
//err = PushMsgByCid(vendorStoreID, vendorID)
|
||||
//4 长链接通知给客户端
|
||||
if err != nil {
|
||||
ToClientChan <- clientInfo{Code: SuccessCode, Msg: fmt.Sprintf("%v", err), Data: jxMsg}
|
||||
} else {
|
||||
ToClientChan <- clientInfo{Code: SuccessCode, Msg: SuccessMsg, Data: jxMsg}
|
||||
if err = PushMsgByCid(vendorStoreID, vendorID); err != nil {
|
||||
errList.AddErr(fmt.Errorf("向商家cid推送新消息错误:%v", err))
|
||||
}
|
||||
return
|
||||
|
||||
if errList.GetErrListAsOne() != nil {
|
||||
return fmt.Errorf("ReadMsgFromVendor:%v", errList.GetErrListAsOne())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PushMsgByCid 通过cid push用户
|
||||
@@ -303,7 +165,7 @@ func PushMsgByCid(vendorStoreID string, vendorID int) error {
|
||||
}
|
||||
|
||||
// SetMessageDetail 赋值
|
||||
//格式 AppID:AppPoiCode:10:OpenUserID
|
||||
//格式 AppID:AppPoiCode:1:OpenUserID
|
||||
func SetMessageDetail(req *JXMsg, vendorID int, elmAppID string) error {
|
||||
//生成京西消息ID detail
|
||||
msgID := GenMsgDetailID(req, vendorID, elmAppID)
|
||||
@@ -368,7 +230,7 @@ func GetNewAndTrim(key string, flag string) (cnt int, err error) {
|
||||
// GenMsgDetailID 生成查询详细聊天记录ID
|
||||
func GenMsgDetailID(jxMsg *JXMsg, vendorID int, elmAppID string) (msgID string) {
|
||||
if vendorID == VendorIDMT {
|
||||
var d1 = jxMsg.MsgContent.(mtwmapi.SingleChat)
|
||||
var d1 = jxMsg.MsgContent.(mtwmapi.PushContentReq)
|
||||
msgID = utils.Int2Str(d1.AppID) + ":" + d1.AppPoiCode + ":1:" + utils.Int2Str(d1.OpenUserID)
|
||||
}
|
||||
if vendorID == VendorIDELM {
|
||||
@@ -381,7 +243,7 @@ func GenMsgDetailID(jxMsg *JXMsg, vendorID int, elmAppID string) (msgID string)
|
||||
// GenMsgListID 生成展示列表时单条数据ID(部分)
|
||||
func GenMsgListID(jxMsg *JXMsg, vendorID int, elmAppID string) (msgID string) {
|
||||
if vendorID == VendorIDMT {
|
||||
var d1 = jxMsg.MsgContent.(mtwmapi.SingleChat)
|
||||
var d1 = jxMsg.MsgContent.(mtwmapi.PushContentReq)
|
||||
msgID = utils.Int2Str(d1.AppID) + ":" + d1.AppPoiCode + ":1"
|
||||
}
|
||||
if vendorID == VendorIDELM {
|
||||
|
||||
Reference in New Issue
Block a user