sys_send_message.go 6.7 KB

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