فهرست منبع

增加自定义发送消息,群发消息修改发送id和名称

renjiaojiao 3 سال پیش
والد
کامیت
68f1b495f7

+ 37 - 0
src/customerService/customController.go

@@ -1 +1,38 @@
 package customerService
+
+import "github.com/baiy/Cadmin-server-go/admin"
+
+type customMsg struct {
+	Id int `form:"id"` //消息id
+	//UserGroupId   string `form:"userGroupId"`   //用户分组标识
+	//UserGroupName string `form:"userGroupName"` //用户分组名称
+	MsgType    int    `form:"msgType"`    //消息类型
+	Title      string `form:"title"`      //标题
+	Content    string `form:"content"`    //内容
+	Link       string `form:"link"`       //链接
+	SendMode   int    `form:"sendMode"`   //发送时效 发送模式  1- 定时 2-实时
+	SendTime   string `form:"sendTime"`   // 发送时间
+	AndroidUrl string `form:"androidUrl"` //安卓连接
+	IosUrl     string `form:"iosUrl"`     //ios连接
+	WeChatUrl  string `form:"weChatUrl"`  //微信连接
+	UserIds    string `form:"userIds"`    //发送用户id
+}
+
+func customSendMsg(context *admin.Context) (interface{}, error) {
+	param := &customMsg{}
+	err := context.Form(param)
+	if err != nil {
+		return nil, nil
+	}
+	//发送状态
+	sendStatus := 1 //
+	if param.SendMode == 2 {
+		sendStatus = 4
+	}
+	//拼接link
+	param.Link = param.Link + "," + param.AndroidUrl + "," + param.IosUrl + "," + param.WeChatUrl
+	status, err := CustomSendMsg(param, sendStatus, context.User.Username, context.User.Id)
+	return map[string]interface{}{
+		"status": status,
+	}, err
+}

+ 201 - 0
src/customerService/customService.go

@@ -1 +1,202 @@
 package customerService
+
+import (
+	"config"
+	"errors"
+	"log"
+	qutil "qfw/util"
+	"strconv"
+	"strings"
+	"sync"
+	"task"
+	"time"
+	"util"
+)
+
+func CustomSendMsg(param *customMsg, sendStatus int, loginUserName string, loginUserId int) (int, error) {
+	var msgLogId string
+	if param.SendMode == 2 {
+		param.SendTime = time.Now().Format(qutil.Date_Full_Layout)
+	}
+	// 更新消息
+	if param.Id != 0 {
+		msgLogId = strconv.Itoa(param.Id)
+		ok := util.JysqlDB.Update("message_send_log", map[string]interface{}{"id": param.Id}, map[string]interface{}{
+			"send_usergroup_id":   "",
+			"send_usergroup_name": "",
+			"receive_user_id":     param.UserIds,
+			"msg_type":            param.MsgType,
+			"title":               param.Title,
+			"content":             param.Content,
+			"send_mode":           param.SendMode,
+			"send_time":           param.SendTime,
+			"send_status":         sendStatus,
+			"update_time":         time.Now().Format(qutil.Date_Full_Layout),
+			"link":                param.Link,
+			"isdel":               1,
+			"send_userid":         loginUserId,
+		})
+		if !ok {
+			return 0, errors.New("发送消息出错")
+		}
+		if param.SendMode == 1 {
+			now := time.Now()
+			sendTime := param.SendTime
+			_, err := time.ParseInLocation(qutil.Date_Full_Layout, sendTime, now.Location())
+			if err != nil {
+				sendTime = param.SendTime + ":00"
+			}
+			key := strconv.Itoa(param.Id)
+			if _, t := util.OnTimeSendMap.Load(key + sendTime); !t {
+				sendTime2, _ := time.ParseInLocation(qutil.Date_Full_Layout, sendTime, now.Location())
+				execTime := sendTime2.Unix() - now.Unix()
+				log.Println(execTime, sendTime, now.Format(qutil.Date_Full_Layout), sendTime2.Unix(), now.Unix())
+				util.OnTimeSendMap.Store(key+sendTime, true)
+				time.AfterFunc(time.Duration(execTime)*time.Second, func() {
+					// 执行任务
+					log.Println("创建、修改添加定时任务")
+					log.Println(param.Id, sendTime)
+					task.Task(param.Id, sendTime, param.AndroidUrl, param.IosUrl)
+				})
+			}
+		}
+		return 1, nil
+	} else {
+		//保存发送的消息
+		log.Println("发送消息发送时间:", param.SendTime)
+		msgId := util.JysqlDB.Insert("message_send_log", map[string]interface{}{
+			"send_usergroup_id":   "",
+			"send_usergroup_name": "",
+			// "receive_user_id":     userIdStr,
+			"msg_type":    param.MsgType,
+			"title":       param.Title,
+			"content":     param.Content,
+			"send_mode":   param.SendMode,
+			"send_time":   param.SendTime,
+			"send_status": sendStatus,
+			"update_time": time.Now().Format(qutil.Date_Full_Layout),
+			"createtime":  time.Now().Format(qutil.Date_Full_Layout),
+			"link":        param.Link,
+			"isdel":       1,
+			"send_userid": loginUserId,
+		})
+		msgLogId = strconv.FormatInt(msgId, 10)
+		//如果是定时发送
+		if param.SendMode == 1 {
+			if msgId > 0 {
+				msgId := qutil.IntAll(msgId)
+				sendTime, _ := time.ParseInLocation(qutil.Date_Full_Layout, param.SendTime+":00", time.Now().Location())
+				now := time.Now()
+				days := sendTime.Sub(now)
+				log.Println("时间:", days)
+				util.OnTimeSendMap.Store(strconv.Itoa(msgId)+param.SendTime+":00", true)
+				time.AfterFunc(days, func() {
+					// 执行任务
+					//log.Println("创建、修改添加定时任务")
+					log.Println(msgId, sendTime)
+					task.Task(msgId, sendTime.Format("2006-01-02 15:04:05"), param.AndroidUrl, param.IosUrl)
+				})
+				return 1, nil
+			}
+		}
+	}
+	log.Println("发送消息记录id*********", msgLogId)
+	//立即发送
+	if param.SendMode == 2 {
+		projectIdMap := sync.Map{}
+		orm := util.Tidb.NewSession()
+		err := orm.Begin()
+		if err != nil {
+			log.Println("简历数据库连接出错:", err)
+		}
+		log.Println("--------", sendStatus)
+		userNames := ""
+		userIds := ""
+		i := 0
+		msgType := strconv.Itoa(param.MsgType)
+		msg := map[string]interface{}{
+			"sendUserId": loginUserId,
+			"sendName":   loginUserName,
+			"title":      param.Title,
+			"content":    param.Content,
+			"msgType":    msgType,
+			"link":       param.Link,
+			"appid":      util.AppId,
+			"msgLogId":   msgLogId,
+		}
+		userIdArr := strings.Split(param.UserIds, ",")
+		for _, val := range userIdArr {
+			i++
+			userId := val
+			if config.SysConfigs.UserIdMap[userId] != "" {
+				userId = config.SysConfigs.UserIdMap[userId]
+			}
+			if _, ok := projectIdMap.Load(userId); ok {
+				log.Println("########################已发送,本次跳过。。。。。。。。。。。。。", userId)
+				continue
+			} else {
+				projectIdMap.Store(userId, true)
+			}
+			userIds += userId + ","
+
+			//查询mongo库用户信息
+			userData := &map[string]interface{}{}
+			ok := false
+			var otherPushId, jgPushId, phoneType, name = "", "", "", ""
+			appVersion := ""
+			userData, ok = util.MQFW.FindById("user", userId, `"s_name":1,"s_opushid":1,"s_jpushid":1,"s_appponetype":1,"s_appversion":1`)
+			if userData != nil && len(*userData) > 0 && ok {
+				otherPushId = qutil.ObjToString((*userData)["s_opushid"])
+				jgPushId = qutil.ObjToString((*userData)["s_jpushid"])
+				phoneType = qutil.ObjToString((*userData)["s_appponetype"])
+				name = qutil.ObjToString((*userData)["s_name"])
+				appVersion = qutil.ObjToString((*userData)["s_appversion"])
+			}
+			userNames += name + ","
+			appPushUrl := "/jyapp/frontPage/messageCenter/sess/index"
+			if strings.Contains(phoneType, "iPhone") {
+				if param.IosUrl != "" {
+					appPushUrl = param.IosUrl
+				}
+			} else {
+				if param.AndroidUrl != "" {
+					appPushUrl = param.AndroidUrl
+				}
+			}
+			//实时发送发送时间为当前时间
+			param.SendTime = time.Now().Format(qutil.Date_Full_Layout)
+			dt := map[string]interface{}{
+				"receiveUserId": userId,
+				"receiveName":   name,
+				"sendUserId":    loginUserId,
+				"sendName":      loginUserName,
+				"title":         param.Title,
+				"content":       param.Content,
+				"msgType":       msgType,
+				"link":          param.Link,
+				"appid":         util.AppId,
+			}
+			//推送消息
+			if appVersion > "3.0.3" {
+				go util.AppGrpcPush(dt, otherPushId, jgPushId, phoneType, appPushUrl)
+			}
+			if i == 100 {
+				//调用消息中台
+				util.MultipleSaveMessage(msg, userIds, userNames)
+				userNames = ""
+				userIds = ""
+				i = 0
+			}
+
+		}
+		if i > 0 {
+			//调用中台接口
+			util.MultipleSaveMessage(msg, userIds, userNames)
+			userNames = ""
+			userIds = ""
+			i = 0
+		}
+		return 1, nil
+	}
+	return 0, errors.New("发送消息出错")
+}

+ 1 - 1
src/customerService/newsController.go

@@ -88,7 +88,7 @@ func MessageSave(context *admin.Context) (interface{}, error) {
 	}
 	//拼接link
 	param.Link = param.Link + "," + param.AndroidUrl + "," + param.IosUrl + "," + param.WeChatUrl
-	status, err := SendMsg(param, sendStatus)
+	status, err := SendMsg(param, sendStatus, context.User.Username, context.User.Id)
 	return map[string]interface{}{
 		"status": status,
 	}, err

+ 7 - 7
src/customerService/newsService.go

@@ -527,7 +527,7 @@ func GetGroupUser(groupId string) ([]int, error) {
 	return 0, errors.New("发送消息出错")
 }*/
 
-func SendMsg(param *Message, sendStatus int) (int, error) {
+func SendMsg(param *Message, sendStatus int, loginUserName string, loginUserId int) (int, error) {
 	var msgLogId string
 	if param.SendMode == 2 {
 		param.SendTime = time.Now().Format(qutil.Date_Full_Layout)
@@ -548,7 +548,7 @@ func SendMsg(param *Message, sendStatus int) (int, error) {
 			"update_time": time.Now().Format(qutil.Date_Full_Layout),
 			"link":        param.Link,
 			"isdel":       1,
-			"send_userid": "qmx",
+			"send_userid": loginUserId,
 		})
 		if !ok {
 			return 0, errors.New("发送消息出错")
@@ -592,7 +592,7 @@ func SendMsg(param *Message, sendStatus int) (int, error) {
 			"createtime":  time.Now().Format(qutil.Date_Full_Layout),
 			"link":        param.Link,
 			"isdel":       1,
-			"send_userid": "qmx",
+			"send_userid": loginUserId,
 		})
 		msgLogId = strconv.FormatInt(msgId, 10)
 		//如果是定时发送
@@ -631,8 +631,8 @@ func SendMsg(param *Message, sendStatus int) (int, error) {
 		i := 0
 		msgType := strconv.Itoa(param.MsgType)
 		msg := map[string]interface{}{
-			"sendUserId": "qmx",
-			"sendName":   "剑鱼后台",
+			"sendUserId": loginUserId,
+			"sendName":   loginUserName,
 			"title":      param.Title,
 			"content":    param.Content,
 			"msgType":    msgType,
@@ -700,8 +700,8 @@ func SendMsg(param *Message, sendStatus int) (int, error) {
 			dt := map[string]interface{}{
 				"receiveUserId": userId,
 				"receiveName":   name,
-				"sendUserId":    "qmx",
-				"sendName":      "剑鱼后台",
+				"sendUserId":    loginUserId,
+				"sendName":      loginUserName,
 				"title":         param.Title,
 				"content":       param.Content,
 				"msgType":       msgType,