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) 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. err = email.Client.DialAndSend(m)
  29. if err != nil {
  30. g.Log().Error("发送用户 "+userInfo.NickName+" 邮箱邮件失败:"+userInfo.Email, err)
  31. return err
  32. }
  33. return nil
  34. }
  35. // 批量发送邮箱
  36. func (c *contextService) BatchSendUserEmailMsg(userIds []string, msgTitle, msgContent string) error {
  37. userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.Email).WherePri(userIds).All()
  38. if err != nil {
  39. g.Log().Error(err)
  40. return err
  41. }
  42. for _, userInfo := range userInfos {
  43. if userInfo.Email == "" {
  44. g.Log().Error("发送用户 " + userInfo.NickName + " 邮箱邮件失败:用户邮箱为空")
  45. continue
  46. }
  47. m := email.NewMessage()
  48. m.SetHeader("From", g.Config().GetString("email.From"))
  49. m.SetHeader("To", userInfo.Email)
  50. m.SetHeader("Subject", msgTitle)
  51. m.SetBody("text/html", msgContent)
  52. err = email.Client.DialAndSend(m)
  53. if err != nil {
  54. g.Log().Error("发送用户邮箱邮件失败:"+userInfo.Email, err)
  55. continue
  56. }
  57. }
  58. return nil
  59. }
  60. // 发送钉钉
  61. func (c *contextService) SendUserDingTalkTextMsg(userId, msgTitle, msgContent string) error {
  62. userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.DingtalkUid).WherePri(userId).One()
  63. if err != nil {
  64. g.Log().Error(err)
  65. return err
  66. }
  67. if userInfo.DingtalkUid == "" {
  68. errMsg := "发送用户 " + userInfo.NickName + " 钉钉失败:钉钉账号信息为空"
  69. g.Log().Error(errMsg)
  70. return myerrors.TipsError(errMsg)
  71. }
  72. dingtalkSendMsgReq := &corpconversation.SendCorpConversationRequest{
  73. UseridList: userInfo.DingtalkUid,
  74. Msg: corpconversation.Msg{
  75. Msgtype: "text",
  76. Text: corpconversation.TextMsg{
  77. Content: msgContent,
  78. },
  79. },
  80. }
  81. err = c.BaseSendUserDingTalkMsg(dingtalkSendMsgReq, userInfo.NickName)
  82. if err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. // 批量发送钉钉
  88. func (c *contextService) BatchSendUserDingTalkTextMsg(userIds []string, msgTitle, msgContent string) error {
  89. userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.DingtalkUid).WherePri(userIds).All()
  90. if err != nil {
  91. g.Log().Error(err)
  92. return err
  93. }
  94. var dingtalkUids []string
  95. var userNickNameList []string
  96. for _, userInfo := range userInfos {
  97. if userInfo.DingtalkUid == "" {
  98. errMsg := "发送用户 " + userInfo.NickName + " 钉钉消息失败:钉钉账号信息为空"
  99. g.Log().Error(errMsg)
  100. continue
  101. }
  102. dingtalkUids = append(dingtalkUids, userInfo.DingtalkUid)
  103. userNickNameList = append(userNickNameList, userInfo.NickName)
  104. }
  105. dingtalkSendMsgReq := &corpconversation.SendCorpConversationRequest{
  106. UseridList: strings.Join(dingtalkUids, ","),
  107. Msg: corpconversation.Msg{
  108. Msgtype: "text",
  109. Text: corpconversation.TextMsg{
  110. Content: msgContent,
  111. },
  112. },
  113. }
  114. err = c.BaseSendUserDingTalkMsg(dingtalkSendMsgReq, userNickNameList)
  115. if err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120. // 钉钉基础调用
  121. func (c *contextService) BaseSendUserDingTalkMsg(dingtalkSendMsgReq *corpconversation.SendCorpConversationRequest, remark interface{}) error {
  122. err := c.CreateDingTalkLog("10", dingtalkSendMsgReq, remark)
  123. if err != nil {
  124. g.Log().Error("创建发送钉钉消息请求log失败:", err)
  125. }
  126. resp, err := dingtalk.Client.GetCorpConversation().SendCorpConversation(dingtalkSendMsgReq)
  127. if err != nil {
  128. g.Log().Error("发送用户钉钉消息失败:", err)
  129. return err
  130. }
  131. err = c.CreateDingTalkLog("20", resp, remark)
  132. if err != nil {
  133. g.Log().Error("创建钉钉消息返回log失败:", err)
  134. }
  135. getSendResultRequest := &corpconversation.GetSendResultRequest{
  136. TaskId: resp.TaskId,
  137. }
  138. err = c.CreateDingTalkLog("10", getSendResultRequest, nil)
  139. if err != nil {
  140. g.Log().Error("创建获取发送钉钉消息结果请求log失败:", err)
  141. }
  142. response, err := dingtalk.Client.GetCorpConversation().GetSendResult(getSendResultRequest)
  143. if err != nil {
  144. g.Log().Error("获取发送用户钉钉消息结果失败:", err)
  145. return err
  146. }
  147. err = c.CreateDingTalkLog("20", response, nil)
  148. if err != nil {
  149. g.Log().Error("创建获取发送钉钉消息结果返回log失败:", err)
  150. }
  151. return err
  152. }
  153. // 创建钉钉调用log
  154. func (c *contextService) CreateDingTalkLog(reqType string, content, remark interface{}) error {
  155. data := g.Map{
  156. "type": reqType,
  157. "content": content,
  158. "remark": remark,
  159. }
  160. SetCreatedInfo(data, 0, "系统发送消息创建")
  161. _, err := g.DB(c.Tenant).Model("dingtalk_log").Safe().Data(data).Insert()
  162. if err != nil {
  163. g.Log().Error("创建钉钉log失败:", err)
  164. }
  165. return err
  166. }
  167. // 发送微信小程序
  168. func (c *contextService) SendUserMiniWechatMsg(userId, msgTitle, msgContent string) error {
  169. userInfo, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userId).One()
  170. if err != nil {
  171. g.Log().Error(err)
  172. return err
  173. }
  174. if userInfo.WechatId == "" {
  175. errMsg := "发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空"
  176. g.Log().Error(errMsg)
  177. return myerrors.TipsError(errMsg)
  178. }
  179. if err != nil {
  180. g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err)
  181. return err
  182. }
  183. return nil
  184. }
  185. // 批量发送微信小程序
  186. func (c *contextService) BatchSendUserMiniWechatMsg(userIds []string, msgTitle, msgContent string) error {
  187. userInfos, err := dao.NewSysUserDao(c.Tenant).Fields(dao.SysUser.C.NickName, dao.SysUser.C.WechatId).WherePri(userIds).All()
  188. if err != nil {
  189. g.Log().Error(err)
  190. return err
  191. }
  192. for _, userInfo := range userInfos {
  193. if userInfo.WechatId == "" {
  194. g.Log().Error("发送用户 " + userInfo.NickName + " 微信小程序消息失败:用户邮箱为空")
  195. continue
  196. }
  197. if err != nil {
  198. g.Log().Error("发送用户 "+userInfo.NickName+" 微信小程序消息失败:"+userInfo.WechatId, err)
  199. continue
  200. }
  201. }
  202. return nil
  203. }