sms_send.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strconv"
  7. "github.com/blinkbean/dingtalk"
  8. "hongze/hongze_robot/cache"
  9. "hongze/hongze_robot/models"
  10. "hongze/hongze_robot/utils"
  11. "strings"
  12. "time"
  13. )
  14. func SendSms() {
  15. time.Sleep(1 * time.Second)
  16. var err error
  17. defer func() {
  18. if err != nil {
  19. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendSms Err:"+err.Error(), utils.EmailSendToUsers)
  20. }
  21. }()
  22. configItem, err := cache.GetSmsConfigByCache("mobile")
  23. if err != nil {
  24. return
  25. }
  26. if configItem.ConfigValue == "" {
  27. err = errors.New("mobile-val is empty")
  28. return
  29. }
  30. items, err := models.GetSmsRecord()
  31. if err != nil {
  32. return
  33. }
  34. var dingToken = []string{}
  35. if utils.RunMode == "release" {
  36. dingToken = []string{
  37. "5f9ae772343b20af806e1f8a6c27a3d8c1b57d8c36f468ab0a12d7eca1cf423c",
  38. "a3125f68a3b627443aa6756e2e973547d0bf3a346668f0f2794fb6d845e2ce68",
  39. "c13598ccb03fc6a2bfa2a7673237c9b375ba0e778a0f0db56ba358b01a9a8d89",
  40. }
  41. } else {
  42. dingToken = []string{
  43. "d2506ab774a3978dac6e69f59814b6f3f669cdde6ceefad67c109e34038de87e",
  44. }
  45. }
  46. for _, v := range items {
  47. if strings.Contains(configItem.ConfigValue, v.Mobile) || strings.Contains(v.Body, "【华瑞资讯】") {
  48. smsDateTime := v.SmsDateTime.Format(utils.FormatDateTime)
  49. msg := []string{
  50. "### " + v.Mobile,
  51. "---",
  52. "- <font>" + v.Body + "</font>",
  53. "- " + smsDateTime,
  54. }
  55. cli := dingtalk.InitDingTalk(dingToken, "hongze")
  56. //cli.SendTextMessage("content", dingtalk.WithAtAll())
  57. err = cli.SendMarkDownMessageBySlice("华瑞信息", msg)
  58. if err != nil {
  59. return
  60. }
  61. models.ModifySmsRecordIsSend(v.Mobile, v.SmsDate)
  62. time.Sleep(1 * time.Second)
  63. } else if _, ok := blackMobile[v.Mobile]; !ok {
  64. msgBody := "聚酯短信有: 第三方更换了手机号:" + v.Mobile
  65. go SendAlarmMsgByDingTalkRelease(utils.APPNAME, utils.RunMode, msgBody, 3)
  66. go SendAlarmMsgByEmail(utils.APPNAME, utils.RunMode, msgBody, utils.EmailSendToUsers, 3)
  67. }
  68. }
  69. }
  70. func AddUnionCode() {
  71. list, err := models.GetSmsRecordList()
  72. if err != nil {
  73. fmt.Println("GetSmsRecordList Err:" + err.Error())
  74. return
  75. }
  76. for _, v := range list {
  77. fmt.Println(v.Id, v.Mobile)
  78. unionCode := utils.MD5(v.Mobile + v.Body + v.SmsDate)
  79. err = models.ModifySmsRecordUnicode(unionCode, v.Id)
  80. if err != nil {
  81. fmt.Println("ModifySmsRecordUnicode Err:" + err.Error())
  82. return
  83. }
  84. }
  85. }
  86. func CheckSmsSend(cont context.Context) (err error) {
  87. //未推送短信记录
  88. list, err := models.GetSmsRecord()
  89. if err != nil && err.Error() != utils.ErrNoRow() {
  90. utils.FileLog.Info("GetSmsRecord Err:" + err.Error())
  91. return err
  92. }
  93. listLen := len(list)
  94. if listLen > 5 {
  95. msgBody := "聚酯短信有: " + strconv.Itoa(listLen) + " 条未推送"
  96. go SendAlarmMsgByDingTalkRelease(utils.APPNAME, utils.RunMode, msgBody, 3)
  97. //发送短信
  98. SendSms()
  99. }
  100. return err
  101. }