This commit is contained in:
richboo111
2023-04-25 10:31:54 +08:00
parent 352bb15636
commit 0901cfc573
12 changed files with 784 additions and 90 deletions

View File

@@ -3,11 +3,10 @@ package ebaiapi
import (
"errors"
"fmt"
"net/http"
"net/url"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"net/http"
"net/url"
)
const (
@@ -20,6 +19,11 @@ const (
CmdShopMsgPush = "shop.msg.push"
CmdShopBindMsg = "shop.bind.msg"
CmdShopUnbindMsg = "shop.unbind.msg"
//IM消息回调通知
CmdImMessageSendEvent = "im.message.send.event" //用户/骑手消息通知
CmdImMessageReadEvent = "im.message.read.event" //用户/骑手已读通知
)
type CallbackResponse struct {
@@ -85,6 +89,55 @@ type CBUserCancelInfo struct {
CancelType int `json:"cancel_type"`
}
//temp IM 用户/骑手消息通知
type TempSent struct {
SubBizType string `json:"subBizType"` //业务子类型枚举值SEND_MESSAGE-发送消息
BizType string `json:"bizType"` //业务类型枚举值IM-消息
PayLoad struct {
SenderID string `json:"senderId"` //角色+随机数字串角色10(用户)、20(骑手)、30(商家)、32连锁账号登录
ReceiverIDs []string `json:"receiverIds"` //角色+随机数字串角色10(用户)、20(骑手)、30(商家)、32连锁账号登录
CreateTime int64 `json:"createTime"` //时间戳
GroupID string `json:"groupId"` //会话id
MsgID string `json:"msgId"` //消息ID
ContentType int `json:"contentType"` //消息类型枚举值1-普通文本
} `json:"payload"`
PlatformShopID string `json:"platformShopId"` //平台门店ID
}
//IM 用户/骑手消息通知
type ImMessageSend struct {
SubBizType string `json:"subBizType"` //业务子类型枚举值SEND_MESSAGE-发送消息
BizType string `json:"bizType"` //业务类型枚举值IM-消息
PayLoad PayLoad `json:"payLoad"`
PlatformShopID string `json:"platformShopId"` //平台门店ID
}
type PayLoad struct {
SenderID string `json:"senderId"` //角色+随机数字串角色10(用户)、20(骑手)、30(商家)、32连锁账号登录
ReceiverIDs []string `json:"receiverIds"` //角色+随机数字串角色10(用户)、20(骑手)、30(商家)、32连锁账号登录
CreateTime int64 `json:"createTime"` //时间戳
GroupID string `json:"groupId"` //会话id
MsgID string `json:"msgId"` //消息ID
ContentType int `json:"contentType"` //消息类型枚举值1-普通文本
Content string `json:"content"`
}
//ContentType =1-普通文本,8-@消息
type Content struct {
Text string `json:"text"`
}
//IM 用户/骑手已读通知
type ImMessageRead struct {
PlatformShopID string `json:"platformShopId"` //平台门店ID
BizType string `json:"bizType"` //业务类型枚举值IM-消息
SubBizType string `json:"subBizType"` //业务子类型枚举值READ_MESSAGE-读取消息
PayLoad struct {
MsgIDs string `json:"msgIds"` //消息ID 列表
CID string `json:"cid"` //会话id
UID string `json:"uid"` //已读操作人
} `json:"payLoad"`
}
func (a *API) Err2CallbackResponse(cmd string, err error, data interface{}) *CallbackResponse {
response := &CallbackResponse{
Cmd: "resp." + cmd,
@@ -168,6 +221,10 @@ func (a *API) GetCallbackMsg(request *http.Request) (msg *CallbackMsg, callbackR
case CmdOrderUserCancel:
var userCancelData CBUserCancelInfo
tmpObj = &userCancelData
case CmdImMessageSendEvent:
tmpObj = &ImMessageSend{}
case CmdImMessageReadEvent:
tmpObj = &ImMessageRead{}
}
if tmpObj != nil {
if utils.Map2StructByJson(msg.Body, tmpObj, true) == nil {

View File

@@ -1,6 +1,10 @@
package ebaiapi
import "testing"
import (
"encoding/json"
"fmt"
"testing"
)
func TestGetStoreImStatus(t *testing.T) {
data, err := api.GetStoreIMStatus("1139781155")
@@ -9,3 +13,36 @@ func TestGetStoreImStatus(t *testing.T) {
}
t.Log(data)
}
//获取门店线上IM状态
func TestAPI_GetImOnlineStatus(t *testing.T) {
}
func TestParseMultilayerJson(t *testing.T) {
//var data1 = `{"subBizType": "SEND_MESSAGE","bizType": "IM","payload": {"senderId":"20235760123","receiverIds":["105872382789","30506545123","20235760456"],"createTime":1642647893901,"groupId":"$2$10514249123$PNM","msgId": "1654907240123.PNM","contentType": "1","content":"{"text":"测试消息"}"},"platformShopId": "32267818868"}`
var data8 = `{
"subBizType": "SEND_MESSAGE",
"bizType": "IM",
"payload": {
"senderId": "102000022769889",
"receiverIds": ["102000022769889", "30507511668"],
"createTime": 1680579669946,
"groupId": "$2$10996707119$PNM",
"msgId": "1734454964456.PNM",
"contentType": 8,
"content": "{\"elements\":[{\"elementContent\":\"{\\\"atAll\\\":false,\\\"defaultNick\\\":\\\"\\\",\\\"uid\\\":{\\\"appUid\\\":\\\"30507511668\\\",\\\"domain\\\":\\\"eleme\\\"}}\",\"elementType\":3},{\"elementContent\":\"{\\\"extensions\\\":{},\\\"text\\\":\\\"@商家 我选的就是退一个杯子呀\\\"}\",\"elementType\":1}]}"
},
"platformShopId": "507511668"
}`
//retVal1 := ParseMultilayerJson(data1)
//fmt.Println(utils.Format4Output(retVal1, false))
//retVal8 := ParseMultilayerJson(data8)
//fmt.Println(utils.Format4Output(retVal8, false))
temp := ImMessageSent{}
err := json.Unmarshal([]byte(data8), &temp)
if err != nil {
fmt.Println(err)
}
fmt.Println(temp)
}

View File

@@ -9,10 +9,60 @@ const (
IMStoreStatusOnLine = "ONLINE" // 门店im在线状态
IMStoreStatusBusy = "BUSY" // 忙碌状态
IMType = "IM" // 业务类型,消息默认IM
SubIMType = "SEND_MESSAGE" // 子业务类型发送消息。默认值SEND_MESSAGE
SubTypeDef = "SEND_MESSAGE" // 子业务类型发送消息。默认值SEND_MESSAGE
ReadIMType = "READ_MESSAGE"
//消息类型
ContentTypeNormal = 1 //普通文本信息
ContentTypeAt = 8 //@ 消息
)
// BusinessSendMsgReq im发送消息
type BusinessSendMsgReq struct {
PlatformShopId string `json:"platformShopId"` // 平台门店id
BizType string `json:"bizType"` // 业务类型IM消息。默认值IM
SubBizType string `json:"subBizType"` // 子业务类型发送消息。默认值SEND_MESSAGE
Payload BusinessMsgPayload `json:"payload"`
}
type BusinessMsgPayload struct {
GroupId string `json:"groupId"` // 会话ID
MsgId string `json:"msgId"` // 消息ID
ReceiverIds []string `json:"receiverIds"` // 接收人列表
Content string `json:"content"` // 发送内容格式JSON {"text":"msg"}
ContentType string `json:"contentType"` // 内容类型,目前只支持文本消息。枚举值: 1-普通文本
}
// BusinessSendMsg 门店老板发送消息 主要用这个
func (a *API) BusinessSendMsg(param *BusinessSendMsgReq) error {
result, err := a.AccessAPI("im.message.send", utils.Struct2MapByJson(param))
if err != nil {
return err
}
if result.ErrNo != 0 {
return errors.New(result.Error)
}
return nil
}
// SettingStoreMsgRead 设置消息已读
func (a *API) SettingStoreMsgRead(platformShopId string, msgId string) error {
result, err := a.AccessAPI("im.message.read", map[string]interface{}{
"platformShopId": platformShopId,
"bizType": IMType,
"subBizType": ReadIMType,
"payload": map[string]string{"msgId": msgId},
})
if err != nil {
return err
}
if result.ErrNo != 0 {
return errors.New(result.Error)
}
return nil
}
// GetStoreIMStatus 获取门店的im状态(这个应该不怎么用)
func (a *API) GetStoreIMStatus(platformShopId string) (int, error) {
result, err := a.AccessAPI("im.getIMStatus", map[string]interface{}{"platformShopId": platformShopId})
@@ -51,50 +101,3 @@ func (a *API) SetImOnlineStatus(platformShopId string, status string) error {
return nil
}
// BusinessSendMsg 门店老板发送消息
func (a *API) BusinessSendMsg(param *BusinessSendMsgReq) error {
result, err := a.AccessAPI("im.message", utils.Struct2MapByJson(param))
if err != nil {
return err
}
if result.ErrNo != 0 {
return errors.New(result.Error)
}
return nil
}
// BusinessSendMsgReq im发送消息
type BusinessSendMsgReq struct {
PlatformShopId string `json:"platformShopId"` // 平台门店id
BizType string `json:"bizType"` // 业务类型IM消息。默认值IM
SubBizType string `json:"subBizType"` // 子业务类型发送消息。默认值SEND_MESSAGE
Payload BusinessMsgPayload `json:"payload"`
}
type BusinessMsgPayload struct {
GroupId string `json:"groupId"` // 会话ID
MsgId string `json:"msgId"` // 消息ID
ReceiverIds []string `json:"receiverIds"` // 接收人列表
Content string `json:"content"` // 发送内容格式JSON {"text":"msg"}
ContentType string `json:"contentType"` // 内容类型,目前只支持文本消息。枚举值: 1-普通文本
}
// SettingStoreMsgRead 设置消息已读
func (a *API) SettingStoreMsgRead(platformShopId string, msgId string) error {
result, err := a.AccessAPI("im.message.read", map[string]interface{}{
"platformShopId": platformShopId,
"bizType": IMType,
"subBizType": ReadIMType,
"payload": map[string]string{"msgId": msgId},
})
if err != nil {
return err
}
if result.ErrNo != 0 {
return errors.New(result.Error)
}
return nil
}