sys_send_message.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, 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. } else {
  119. dingtalkSendMsgReq = &corpconversation.SendCorpConversationRequest{
  120. UseridList: strings.Join(dingtalkUids, ","),
  121. Msg: corpconversation.Msg{
  122. Msgtype: "file",
  123. File: corpconversation.FileMsg{
  124. MediaId: msgContent,
  125. },
  126. },
  127. }
  128. }
  129. err = c.BaseSendUserDingTalkMsg(dingtalkSendMsgReq, userNickNameList)
  130. if err != nil {
  131. return err
  132. }
  133. return nil
  134. }
  135. // 钉钉基础调用
  136. func (c *contextService) BaseSendUserDingTalkMsg(dingtalkSendMsgReq *corpconversation.SendCorpConversationRequest, remark interface{}) error {
  137. err := c.CreateDingTalkLog("10", dingtalkSendMsgReq, remark)
  138. if err != nil {
  139. g.Log().Error("创建发送钉钉消息请求log失败:", err)
  140. }
  141. resp, err := dingtalk.Client.GetCorpConversation().SendCorpConversation(dingtalkSendMsgReq)
  142. if err != nil {
  143. g.Log().Error("发送用户钉钉消息失败:", err)
  144. return err
  145. }
  146. err = c.CreateDingTalkLog("20", resp, remark)
  147. if err != nil {
  148. g.Log().Error("创建钉钉消息返回log失败:", err)
  149. }
  150. getSendResultRequest := &corpconversation.GetSendResultRequest{
  151. TaskId: resp.TaskId,
  152. }
  153. err = c.CreateDingTalkLog("10", getSendResultRequest, nil)
  154. if err != nil {
  155. g.Log().Error("创建获取发送钉钉消息结果请求log失败:", err)
  156. }
  157. response, err := dingtalk.Client.GetCorpConversation().GetSendResult(getSendResultRequest)
  158. if err != nil {
  159. g.Log().Error("获取发送用户钉钉消息结果失败:", err)
  160. return err
  161. }
  162. err = c.CreateDingTalkLog("20", response, nil)
  163. if err != nil {
  164. g.Log().Error("创建获取发送钉钉消息结果返回log失败:", err)
  165. }
  166. return err
  167. }
  168. // 创建钉钉调用log
  169. func (c *contextService) CreateDingTalkLog(reqType string, content, remark interface{}) error {
  170. data := g.Map{
  171. "type": reqType,
  172. "content": content,
  173. "remark": remark,
  174. }
  175. SetCreatedInfo(data, 0, "系统发送消息创建")
  176. _, err := g.DB(c.Tenant).Model("dingtalk_log").Safe().Data(data).Insert()
  177. if err != nil {
  178. g.Log().Error("创建钉钉log失败:", err)
  179. }
  180. return err
  181. }
  182. // 发送微信小程序
  183. func (c *contextService) SendUserMiniWechatMsg(userId, msgTitle, msgContent string) error {
  184. userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userId).One()
  185. if err != nil {
  186. g.Log().Error(err)
  187. return err
  188. }
  189. if userInfo.WechatId == "" {
  190. errMsg := "发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空"
  191. g.Log().Error(errMsg)
  192. return myerrors.TipsError(errMsg)
  193. }
  194. if err != nil {
  195. g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err)
  196. return err
  197. }
  198. return nil
  199. }
  200. // 批量发送微信小程序
  201. func (c *contextService) BatchSendUserMiniWechatMsg(userIds []string, msgTitle, msgContent string) error {
  202. userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userIds).All()
  203. if err != nil {
  204. g.Log().Error(err)
  205. return err
  206. }
  207. for _, userInfo := range userInfos {
  208. if userInfo.WechatId == "" {
  209. g.Log().Error("发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空")
  210. continue
  211. }
  212. if err != nil {
  213. g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err)
  214. continue
  215. }
  216. }
  217. return nil
  218. }