1
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/partner"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
@@ -450,57 +452,6 @@ 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
|
||||
}
|
||||
|
||||
var rel = map[int]string{
|
||||
589: "a81eb3df418d83d6a1a4b7c572156d2f",
|
||||
5873: "41c479790a76f86326f89e8048964739",
|
||||
4123: "df2c88338b85f830cebce2a9eab56628",
|
||||
}
|
||||
|
||||
// DecryptIm 解密操作
|
||||
func DecryptIm(appID int, msg string) (string, error) {
|
||||
data, _ := base64.StdEncoding.DecodeString(msg)
|
||||
key := utils.LimitUTF8StringLen2(rel[appID], 16)
|
||||
res, err := utils.AESCBCDecpryt(data, []byte(key), []byte(key))
|
||||
if len(string(res)) > 0 && err == nil {
|
||||
return string(res), nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
//加密操作
|
||||
|
||||
// FilterIm 过滤操作
|
||||
func FilterIm(appID int, msg string) bool {
|
||||
var check = "[自动回复]"
|
||||
data, _ := DecryptIm(appID, msg)
|
||||
if len(data) > 0 && strings.Contains(data, check) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RandTimeNumber 生成int类型 随机数+时间戳
|
||||
func RandTimeNumber() int {
|
||||
num, _ := rand.Int(rand.Reader, big.NewInt(100000))
|
||||
str := utils.Int64ToStr(num.Int64()) + utils.Int2Str(int(time.Now().Unix()))
|
||||
return utils.Str2Int(str)
|
||||
}
|
||||
|
||||
// AutoReply 是否已回复
|
||||
//key=AppID+:+UserID+:+msgID(mt)
|
||||
//key=AppID+:+UserID+:+msgID(elm)
|
||||
@@ -514,26 +465,23 @@ type AutoReply struct {
|
||||
// CheckAndReply 判断并回复
|
||||
func CheckAndReply(req *JXMsg, elmAppID string) (err error) {
|
||||
var (
|
||||
flag = false
|
||||
errList errlist.ErrList
|
||||
key string
|
||||
flag = false
|
||||
vendorID int
|
||||
errList errlist.ErrList
|
||||
apply AutoReply
|
||||
jxMsg = &JXMsg{}
|
||||
userList = &UserMessageList{}
|
||||
)
|
||||
if req.SendType == SendTypeMt {
|
||||
vendorID = VendorIDMT
|
||||
mt := req.MsgContent.(mtwmapi.PushContentReq)
|
||||
key := utils.Int2Str(mt.AppID) + ":" + utils.Int2Str(mt.OpenUserID) + ":autoReply"
|
||||
key = utils.Int2Str(mt.AppID) + ":" + utils.Int2Str(mt.OpenUserID) + ":autoReply"
|
||||
//1 检测是否已自动回复
|
||||
if n, err := rdb.Exists(key); n > 0 && err == nil {
|
||||
str := rdb.LRange(key)
|
||||
for i := 0; i < len(str); i++ {
|
||||
v := AutoReply{}
|
||||
if err = json.Unmarshal([]byte(str[i]), &v); err == nil {
|
||||
flag = v.IsApply
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flag = GetIfReply(key)
|
||||
//判断flag状态
|
||||
if !flag {
|
||||
apply := AutoReply{
|
||||
apply = AutoReply{
|
||||
UserID: mt.AppID,
|
||||
MsgID: mt.MsgID,
|
||||
IsApply: true,
|
||||
@@ -542,10 +490,13 @@ func CheckAndReply(req *JXMsg, elmAppID string) (err error) {
|
||||
|
||||
temp := mt
|
||||
//获取自定义回复模板
|
||||
if template := GetCustomTemplate(utils.Int2Str(mt.AppID), mt.AppPoiCode); len(template) != 0 {
|
||||
if template := GetCustomTemplate(utils.Int2Str(mt.AppID), mt.AppPoiCode); len(template) > 0 {
|
||||
temp.MsgContent = template
|
||||
} else {
|
||||
temp.MsgContent = AutoReplyByAppID[mt.AppID]
|
||||
temp.MsgContent, err = GetDefaultTemplate(utils.Int2Str(mt.AppID), mt.AppPoiCode, VendorIDMT)
|
||||
if err != nil {
|
||||
temp.MsgContent = AutoReplyByAppID[mt.AppID]
|
||||
}
|
||||
}
|
||||
temp.MsgSource = mtwmapi.MsgSourceStore
|
||||
temp.Cts = int(time.Now().Unix())
|
||||
@@ -556,47 +507,120 @@ func CheckAndReply(req *JXMsg, elmAppID string) (err error) {
|
||||
apply.IsApply = false
|
||||
globals.SugarLogger.Debugf("CheckAndReply mtSend err:%v", err)
|
||||
} else {
|
||||
jxMsg := &JXMsg{
|
||||
jxMsg = &JXMsg{
|
||||
SendType: SendTypeJx,
|
||||
MsgContent: temp,
|
||||
}
|
||||
userList := &UserMessageList{
|
||||
userList = &UserMessageList{
|
||||
VendorID: VendorIDMT,
|
||||
UserID: utils.Int2Str(mt.OpenUserID),
|
||||
LatestMsg: mt.MsgContent,
|
||||
LatestTime: mt.Cts,
|
||||
OrderID: "",
|
||||
}
|
||||
//1 存储详细聊天记录list
|
||||
if err = SetMessageDetail(jxMsg, VendorIDMT, elmAppID); err != nil {
|
||||
errList.AddErr(fmt.Errorf("存储详细聊天记录错误:%v", err))
|
||||
}
|
||||
//2 存储展示列表时单条数据
|
||||
if err = SetUserList(jxMsg, userList, VendorIDMT, elmAppID); err != nil {
|
||||
errList.AddErr(fmt.Errorf("存储STU聊天记录错误:%v", err))
|
||||
}
|
||||
}
|
||||
|
||||
//记录自动回复状态
|
||||
data, _ := json.Marshal(apply)
|
||||
err := rdb.RPush(key, string(data))
|
||||
ok, err := rdb.ExpireResult(key, ExpireTimeDay)
|
||||
if err != nil || !ok {
|
||||
globals.SugarLogger.Debugf("CheckAndReply apply err:%v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if req.SendType == SendTypeElm {
|
||||
//
|
||||
//}
|
||||
if req.SendType == SendTypeElm {
|
||||
vendorID = VendorIDELM
|
||||
elm := req.MsgContent.(ebaiapi.ImMessageSend)
|
||||
key = elmAppID + ":" + elm.PayLoad.SenderID + ":autoReply"
|
||||
|
||||
//1 检测是否已自动回复
|
||||
flag = GetIfReply(key)
|
||||
if !flag {
|
||||
apply = AutoReply{
|
||||
UserID: utils.Str2Int(elm.PayLoad.SenderID),
|
||||
MsgID: utils.Str2Int(elm.PayLoad.MsgID),
|
||||
IsApply: true,
|
||||
Timestamp: elm.PayLoad.CreateTime,
|
||||
}
|
||||
|
||||
temp := &ebaiapi.BusinessSendMsgReq{
|
||||
PlatformShopId: elm.PlatformShopID,
|
||||
BizType: ebaiapi.IMType,
|
||||
SubBizType: ebaiapi.IMTypeSendMsg,
|
||||
Payload: ebaiapi.BusinessMsgPayload{
|
||||
GroupId: elm.PayLoad.GroupID,
|
||||
MsgId: utils.Int2Str(RandTimeNumber()),
|
||||
ReceiverIds: elm.PayLoad.ReceiverIDs,
|
||||
ContentType: utils.Int2Str(ebaiapi.ContentTypeNormal),
|
||||
},
|
||||
}
|
||||
if template := GetCustomTemplate(elmAppID, elm.PlatformShopID); len(template) > 0 {
|
||||
temp.Payload.Content = template
|
||||
} else {
|
||||
temp.Payload.Content, err = GetDefaultTemplate(elmAppID, elm.PlatformShopID, VendorIDELM)
|
||||
if err != nil {
|
||||
temp.Payload.Content = LastTemplate
|
||||
}
|
||||
}
|
||||
|
||||
if err = partner.CurAPIManager.GetAPI(model.VendorIDEBAI, elmAppID).(*ebaiapi.API).BusinessSendMsg(temp); err != nil {
|
||||
apply.IsApply = false
|
||||
globals.SugarLogger.Debugf("CheckAndReply mtSend err:%v", err)
|
||||
} else {
|
||||
jxMsg = &JXMsg{
|
||||
SendType: SendTypeJx,
|
||||
MsgContent: temp,
|
||||
}
|
||||
userList = &UserMessageList{
|
||||
VendorID: VendorIDMT,
|
||||
UserID: elm.PayLoad.SenderID,
|
||||
LatestMsg: elm.PayLoad.Content,
|
||||
LatestTime: elm.PayLoad.CreateTime,
|
||||
OrderID: "",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//1 存储详细聊天记录list
|
||||
if err = SetMessageDetail(jxMsg, vendorID, elmAppID); err != nil {
|
||||
errList.AddErr(fmt.Errorf("自动回复:存储详细聊天记录错误:%v", err))
|
||||
}
|
||||
//2 存储展示列表时单条数据
|
||||
if err = SetUserList(jxMsg, userList, vendorID, elmAppID); err != nil {
|
||||
errList.AddErr(fmt.Errorf("自动回复:存储STU聊天记录错误:%v", err))
|
||||
}
|
||||
//3 记录自动回复状态
|
||||
if err = RecordAutoStatus(key, apply); err != nil {
|
||||
errList.AddErr(fmt.Errorf("自动回复:记录自动回复状态错误:%v", err))
|
||||
}
|
||||
if errList.GetErrListAsOne() != nil {
|
||||
return fmt.Errorf("CheckAndReply err=%v", errList.GetErrListAsOne())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetIfReply 检查是否已回复
|
||||
func GetIfReply(key string) (flag bool) {
|
||||
flag = false
|
||||
if n, err := rdb.Exists(key); n > 0 && err == nil {
|
||||
str := rdb.LRange(key)
|
||||
for i := 0; i < len(str); i++ {
|
||||
v := AutoReply{}
|
||||
if err = json.Unmarshal([]byte(str[i]), &v); err == nil {
|
||||
flag = v.IsApply
|
||||
}
|
||||
}
|
||||
}
|
||||
return flag
|
||||
}
|
||||
|
||||
// RecordAutoStatus 记录是否已自动回复状态
|
||||
func RecordAutoStatus(key string, apply AutoReply) error {
|
||||
data, _ := json.Marshal(apply)
|
||||
err := rdb.RPush(key, string(data))
|
||||
ok, err := rdb.ExpireResult(key, ExpireTimeDay)
|
||||
if err != nil || !ok {
|
||||
globals.SugarLogger.Debugf("CheckAndReply apply err:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenCustomReplyID 生成门店自动回复模板ID
|
||||
func GenCustomReplyID(appID, vendorStoreID string) string {
|
||||
return BaseCusKey + ":" + appID + ":" + vendorStoreID
|
||||
@@ -625,5 +649,97 @@ func AddCustomReply(appID, vendorStoreID, replyTemplate string) (storeTemplate s
|
||||
err = rdb.Set(key, replyTemplate, 0)
|
||||
}
|
||||
return "", err
|
||||
|
||||
}
|
||||
|
||||
// GetDefaultTemplate 获取门店默认回复模板
|
||||
func GetDefaultTemplate(appID, vendorStoreID string, vendorID int) (string, error) {
|
||||
var (
|
||||
phoneNum = " "
|
||||
t string
|
||||
)
|
||||
store, err := dao.GetStoreDetail2(dao.GetDB(), 0, vendorStoreID, vendorID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(store.Tel1) > 0 {
|
||||
t = store.Tel1
|
||||
}
|
||||
if len(store.Tel1) == 0 && len(store.Tel2) == 0 {
|
||||
t = BasePhoneNum
|
||||
}
|
||||
phoneNum = t[:3] + "-" + t[3:7] + "-" + t[7:]
|
||||
temp := BaseTemplate + phoneNum
|
||||
|
||||
if vendorID == VendorIDMT {
|
||||
data, err := EncryptIm(utils.Str2Int(appID), temp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
if vendorID == VendorIDELM {
|
||||
return temp, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var rel = map[int]string{
|
||||
589: "a81eb3df418d83d6a1a4b7c572156d2f",
|
||||
5873: "41c479790a76f86326f89e8048964739",
|
||||
4123: "df2c88338b85f830cebce2a9eab56628",
|
||||
}
|
||||
|
||||
// DecryptIm 解密操作
|
||||
func DecryptIm(appID int, msg string) (string, error) {
|
||||
data, _ := base64.StdEncoding.DecodeString(msg)
|
||||
key := utils.LimitUTF8StringLen2(rel[appID], 16)
|
||||
res, err := utils.AESCBCDecpryt(data, []byte(key), []byte(key))
|
||||
if len(string(res)) > 0 && err == nil {
|
||||
return string(res), nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
// EncryptIm 加密操作
|
||||
func EncryptIm(appID int, msg string) (retVal string, err error) {
|
||||
key := utils.LimitUTF8StringLen2(rel[appID], 16)
|
||||
if data, err := utils.AESCBCEncpryt([]byte(msg), []byte(key), []byte(key)); err == nil {
|
||||
retVal = base64.StdEncoding.EncodeToString(data)
|
||||
}
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
// FilterIm 过滤操作
|
||||
func FilterIm(appID int, msg string) bool {
|
||||
var check = "[自动回复]"
|
||||
data, _ := DecryptIm(appID, msg)
|
||||
if len(data) > 0 && strings.Contains(data, check) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RandTimeNumber 生成int类型 随机数+时间戳
|
||||
func RandTimeNumber() int {
|
||||
num, _ := rand.Int(rand.Reader, big.NewInt(100000))
|
||||
str := utils.Int64ToStr(num.Int64()) + utils.Int2Str(int(time.Now().Unix()))
|
||||
return utils.Str2Int(str)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user