sys_send_message.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package service
  2. import (
  3. "dashoo.cn/micro/app/dao"
  4. "dashoo.cn/opms_libary/myerrors"
  5. "dashoo.cn/opms_libary/plugin/dingtalk"
  6. "dashoo.cn/opms_libary/plugin/dingtalk/message/corpconversation"
  7. "dashoo.cn/opms_libary/plugin/email"
  8. "github.com/gogf/gf/frame/g"
  9. "strings"
  10. )
  11. // 发送邮箱
  12. func (c *contextService) SendUserEmailMsg(userId, msgTitle, msgContent string, zipurl string) error {
  13. userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.Email).WherePri(userId).One()
  14. if err != nil {
  15. g.Log().Error(err)
  16. return err
  17. }
  18. if userInfo.Email == "" {
  19. errMsg := "发送用户 " + userInfo.NickName + " 邮箱邮件失败:用户邮箱为空"
  20. g.Log().Error(errMsg)
  21. return myerrors.TipsError(errMsg)
  22. }
  23. m := email.NewMessage()
  24. m.SetHeader("From", g.Config().GetString("email.From"))
  25. m.SetHeader("To", userInfo.Email)
  26. m.SetHeader("Subject", msgTitle)
  27. m.SetBody("text/html", msgContent)
  28. m.Attach(zipurl) // 附件
  29. err = email.Client.DialAndSend(m)
  30. if err != nil {
  31. g.Log().Error("发送用户 "+userInfo.NickName+" 邮箱邮件失败:"+userInfo.Email, err)
  32. return err
  33. }
  34. return nil
  35. }
  36. // 批量发送邮箱
  37. func (c *contextService) BatchSendUserEmailMsg(userIds []string, msgTitle, msgContent string) error {
  38. userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.Email).WherePri(userIds).All()
  39. if err != nil {
  40. g.Log().Error(err)
  41. return err
  42. }
  43. for _, userInfo := range userInfos {
  44. if userInfo.Email == "" {
  45. g.Log().Error("发送用户 " + userInfo.NickName + " 邮箱邮件失败:用户邮箱为空")
  46. continue
  47. }
  48. m := email.NewMessage()
  49. m.SetHeader("From", g.Config().GetString("email.From"))
  50. m.SetHeader("To", userInfo.Email)
  51. m.SetHeader("Subject", msgTitle)
  52. m.SetBody("text/html", msgContent)
  53. err = email.Client.DialAndSend(m)
  54. if err != nil {
  55. g.Log().Error("发送用户邮箱邮件失败:"+userInfo.Email, err)
  56. continue
  57. }
  58. }
  59. return nil
  60. }
  61. // 发送钉钉
  62. func (c *contextService) SendUserDingTalkTextMsg(userId, msgTitle, msgContent string) error {
  63. userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.DingtalkUid).WherePri(userId).One()
  64. if err != nil {
  65. g.Log().Error(err)
  66. return err
  67. }
  68. if userInfo.DingtalkUid == "" {
  69. errMsg := "发送用户 " + userInfo.NickName + " 钉钉失败:钉钉账号信息为空"
  70. g.Log().Error(errMsg)
  71. return myerrors.TipsError(errMsg)
  72. }
  73. dingtalkSendMsgReq := &corpconversation.SendCorpConversationRequest{
  74. UseridList: userInfo.DingtalkUid,
  75. Msg: corpconversation.Msg{
  76. Msgtype: "text",
  77. Text: corpconversation.TextMsg{
  78. Content: msgContent,
  79. },
  80. },
  81. }
  82. err = c.BaseSendUserDingTalkMsg(dingtalkSendMsgReq, userInfo.NickName)
  83. if err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. // 批量发送钉钉
  89. func (c *contextService) BatchSendUserDingTalkTextMsg(userIds []string, msgType, msgTitle, msgContent string) error {
  90. userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.DingtalkUid).WherePri(userIds).All()
  91. if err != nil {
  92. g.Log().Error(err)
  93. return err
  94. }
  95. var dingtalkUids []string
  96. var userNickNameList []string
  97. for _, userInfo := range userInfos {
  98. if userInfo.DingtalkUid == "" {
  99. errMsg := "发送用户 " + userInfo.NickName + " 钉钉消息失败:钉钉账号信息为空"
  100. g.Log().Error(errMsg)
  101. continue
  102. }
  103. dingtalkUids = append(dingtalkUids, userInfo.DingtalkUid)
  104. userNickNameList = append(userNickNameList, userInfo.NickName)
  105. }
  106. dingtalkSendMsgReq := new(corpconversation.SendCorpConversationRequest)
  107. // 文件处理
  108. if msgType != "40" {
  109. //dingtalkSendMsgReq = &corpconversation.SendCorpConversationRequest{
  110. // UseridList: strings.Join(dingtalkUids, ","),
  111. // Msg: corpconversation.Msg{
  112. // Msgtype: "text",
  113. // Text: corpconversation.TextMsg{
  114. // Content: msgContent,
  115. // },
  116. // },
  117. //}
  118. dingtalkSendMsgReq = &corpconversation.SendCorpConversationRequest{
  119. UseridList: strings.Join(dingtalkUids, ","),
  120. Msg: corpconversation.Msg{
  121. Msgtype: "markdown",
  122. Markdown: corpconversation.MarkdownMsg{
  123. Title: msgTitle,
  124. Text: msgContent,
  125. },
  126. },
  127. }
  128. } else {
  129. dingtalkSendMsgReq = &corpconversation.SendCorpConversationRequest{
  130. UseridList: strings.Join(dingtalkUids, ","),
  131. Msg: corpconversation.Msg{
  132. Msgtype: "file",
  133. File: corpconversation.FileMsg{
  134. MediaId: msgContent,
  135. },
  136. },
  137. }
  138. }
  139. err = c.BaseSendUserDingTalkMsg(dingtalkSendMsgReq, userNickNameList)
  140. if err != nil {
  141. return err
  142. }
  143. return nil
  144. }
  145. // 钉钉基础调用
  146. func (c *contextService) BaseSendUserDingTalkMsg(dingtalkSendMsgReq *corpconversation.SendCorpConversationRequest, remark interface{}) error {
  147. err := c.CreateDingTalkLog("10", dingtalkSendMsgReq, remark)
  148. if err != nil {
  149. g.Log().Error("创建发送钉钉消息请求log失败:", err)
  150. }
  151. resp, err := dingtalk.Client.GetCorpConversation().SendCorpConversation(dingtalkSendMsgReq)
  152. if err != nil {
  153. g.Log().Error("发送用户钉钉消息失败:", err)
  154. return err
  155. }
  156. err = c.CreateDingTalkLog("20", resp, remark)
  157. if err != nil {
  158. g.Log().Error("创建钉钉消息返回log失败:", err)
  159. }
  160. getSendResultRequest := &corpconversation.GetSendResultRequest{
  161. TaskId: resp.TaskId,
  162. }
  163. err = c.CreateDingTalkLog("10", getSendResultRequest, nil)
  164. if err != nil {
  165. g.Log().Error("创建获取发送钉钉消息结果请求log失败:", err)
  166. }
  167. response, err := dingtalk.Client.GetCorpConversation().GetSendResult(getSendResultRequest)
  168. if err != nil {
  169. g.Log().Error("获取发送用户钉钉消息结果失败:", err)
  170. return err
  171. }
  172. err = c.CreateDingTalkLog("20", response, nil)
  173. if err != nil {
  174. g.Log().Error("创建获取发送钉钉消息结果返回log失败:", err)
  175. }
  176. return err
  177. }
  178. // 创建钉钉调用log
  179. func (c *contextService) CreateDingTalkLog(reqType string, content, remark interface{}) error {
  180. data := g.Map{
  181. "type": reqType,
  182. "content": content,
  183. "remark": remark,
  184. }
  185. SetCreatedInfo(data, 0, "系统发送消息创建")
  186. _, err := g.DB(c.Tenant).Model("dingtalk_log").Safe().Data(data).Insert()
  187. if err != nil {
  188. g.Log().Error("创建钉钉log失败:", err)
  189. }
  190. return err
  191. }
  192. // 发送微信小程序
  193. func (c *contextService) SendUserMiniWechatMsg(userId, msgTitle, msgContent string) error {
  194. userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userId).One()
  195. if err != nil {
  196. g.Log().Error(err)
  197. return err
  198. }
  199. if userInfo.WechatId == "" {
  200. errMsg := "发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空"
  201. g.Log().Error(errMsg)
  202. return myerrors.TipsError(errMsg)
  203. }
  204. if err != nil {
  205. g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err)
  206. return err
  207. }
  208. return nil
  209. }
  210. // 批量发送微信小程序
  211. func (c *contextService) BatchSendUserMiniWechatMsg(userIds []string, msgTitle, msgContent string) error {
  212. userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userIds).All()
  213. if err != nil {
  214. g.Log().Error(err)
  215. return err
  216. }
  217. for _, userInfo := range userInfos {
  218. if userInfo.WechatId == "" {
  219. g.Log().Error("发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空")
  220. continue
  221. }
  222. if err != nil {
  223. g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err)
  224. continue
  225. }
  226. }
  227. return nil
  228. }