package service import ( "dashoo.cn/micro/app/dao" "dashoo.cn/opms_libary/myerrors" "dashoo.cn/opms_libary/plugin/dingtalk" "dashoo.cn/opms_libary/plugin/dingtalk/message/corpconversation" "dashoo.cn/opms_libary/plugin/email" "github.com/gogf/gf/frame/g" "strings" ) // 发送邮箱 func (c *contextService) SendUserEmailMsg(userId, msgTitle, msgContent string, zipurl string) error { userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.Email).WherePri(userId).One() if err != nil { g.Log().Error(err) return err } if userInfo.Email == "" { errMsg := "发送用户 " + userInfo.NickName + " 邮箱邮件失败:用户邮箱为空" g.Log().Error(errMsg) return myerrors.TipsError(errMsg) } m := email.NewMessage() m.SetHeader("From", g.Config().GetString("email.From")) m.SetHeader("To", userInfo.Email) m.SetHeader("Subject", msgTitle) m.SetBody("text/html", msgContent) m.Attach(zipurl) // 附件 err = email.Client.DialAndSend(m) if err != nil { g.Log().Error("发送用户 "+userInfo.NickName+" 邮箱邮件失败:"+userInfo.Email, err) return err } return nil } // 批量发送邮箱 func (c *contextService) BatchSendUserEmailMsg(userIds []string, msgTitle, msgContent string) error { userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.Email).WherePri(userIds).All() if err != nil { g.Log().Error(err) return err } for _, userInfo := range userInfos { if userInfo.Email == "" { g.Log().Error("发送用户 " + userInfo.NickName + " 邮箱邮件失败:用户邮箱为空") continue } m := email.NewMessage() m.SetHeader("From", g.Config().GetString("email.From")) m.SetHeader("To", userInfo.Email) m.SetHeader("Subject", msgTitle) m.SetBody("text/html", msgContent) err = email.Client.DialAndSend(m) if err != nil { g.Log().Error("发送用户邮箱邮件失败:"+userInfo.Email, err) continue } } return nil } // 发送钉钉 func (c *contextService) SendUserDingTalkTextMsg(userId, msgTitle, msgContent string) error { userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.DingtalkUid).WherePri(userId).One() if err != nil { g.Log().Error(err) return err } if userInfo.DingtalkUid == "" { errMsg := "发送用户 " + userInfo.NickName + " 钉钉失败:钉钉账号信息为空" g.Log().Error(errMsg) return myerrors.TipsError(errMsg) } dingtalkSendMsgReq := &corpconversation.SendCorpConversationRequest{ UseridList: userInfo.DingtalkUid, Msg: corpconversation.Msg{ Msgtype: "text", Text: corpconversation.TextMsg{ Content: msgContent, }, }, } err = c.BaseSendUserDingTalkMsg(dingtalkSendMsgReq, userInfo.NickName) if err != nil { return err } return nil } // 批量发送钉钉 func (c *contextService) BatchSendUserDingTalkTextMsg(userIds []string, msgType, msgTitle, msgContent string) error { userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.DingtalkUid).WherePri(userIds).All() if err != nil { g.Log().Error(err) return err } var dingtalkUids []string var userNickNameList []string for _, userInfo := range userInfos { if userInfo.DingtalkUid == "" { errMsg := "发送用户 " + userInfo.NickName + " 钉钉消息失败:钉钉账号信息为空" g.Log().Error(errMsg) continue } dingtalkUids = append(dingtalkUids, userInfo.DingtalkUid) userNickNameList = append(userNickNameList, userInfo.NickName) } dingtalkSendMsgReq := new(corpconversation.SendCorpConversationRequest) // 文件处理 if msgType != "40" { //dingtalkSendMsgReq = &corpconversation.SendCorpConversationRequest{ // UseridList: strings.Join(dingtalkUids, ","), // Msg: corpconversation.Msg{ // Msgtype: "text", // Text: corpconversation.TextMsg{ // Content: msgContent, // }, // }, //} dingtalkSendMsgReq = &corpconversation.SendCorpConversationRequest{ UseridList: strings.Join(dingtalkUids, ","), Msg: corpconversation.Msg{ Msgtype: "markdown", Markdown: corpconversation.MarkdownMsg{ Title: msgTitle, Text: msgContent, }, }, } } else { dingtalkSendMsgReq = &corpconversation.SendCorpConversationRequest{ UseridList: strings.Join(dingtalkUids, ","), Msg: corpconversation.Msg{ Msgtype: "file", File: corpconversation.FileMsg{ MediaId: msgContent, }, }, } } err = c.BaseSendUserDingTalkMsg(dingtalkSendMsgReq, userNickNameList) if err != nil { return err } return nil } // 钉钉基础调用 func (c *contextService) BaseSendUserDingTalkMsg(dingtalkSendMsgReq *corpconversation.SendCorpConversationRequest, remark interface{}) error { err := c.CreateDingTalkLog("10", dingtalkSendMsgReq, remark) if err != nil { g.Log().Error("创建发送钉钉消息请求log失败:", err) } resp, err := dingtalk.Client.GetCorpConversation().SendCorpConversation(dingtalkSendMsgReq) if err != nil { g.Log().Error("发送用户钉钉消息失败:", err) return err } err = c.CreateDingTalkLog("20", resp, remark) if err != nil { g.Log().Error("创建钉钉消息返回log失败:", err) } getSendResultRequest := &corpconversation.GetSendResultRequest{ TaskId: resp.TaskId, } err = c.CreateDingTalkLog("10", getSendResultRequest, nil) if err != nil { g.Log().Error("创建获取发送钉钉消息结果请求log失败:", err) } response, err := dingtalk.Client.GetCorpConversation().GetSendResult(getSendResultRequest) if err != nil { g.Log().Error("获取发送用户钉钉消息结果失败:", err) return err } err = c.CreateDingTalkLog("20", response, nil) if err != nil { g.Log().Error("创建获取发送钉钉消息结果返回log失败:", err) } return err } // 创建钉钉调用log func (c *contextService) CreateDingTalkLog(reqType string, content, remark interface{}) error { data := g.Map{ "type": reqType, "content": content, "remark": remark, } SetCreatedInfo(data, 0, "系统发送消息创建") _, err := g.DB(c.Tenant).Model("dingtalk_log").Safe().Data(data).Insert() if err != nil { g.Log().Error("创建钉钉log失败:", err) } return err } // 发送微信小程序 func (c *contextService) SendUserMiniWechatMsg(userId, msgTitle, msgContent string) error { userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userId).One() if err != nil { g.Log().Error(err) return err } if userInfo.WechatId == "" { errMsg := "发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空" g.Log().Error(errMsg) return myerrors.TipsError(errMsg) } if err != nil { g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err) return err } return nil } // 批量发送微信小程序 func (c *contextService) BatchSendUserMiniWechatMsg(userIds []string, msgTitle, msgContent string) error { userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userIds).All() if err != nil { g.Log().Error(err) return err } for _, userInfo := range userInfos { if userInfo.WechatId == "" { g.Log().Error("发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空") continue } if err != nil { g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err) continue } } return nil }