| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- 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, 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: "notice",
- 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
- }
|