sms_send.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/blinkbean/dingtalk"
  6. "hongze/hongze_robot/cache"
  7. "hongze/hongze_robot/models"
  8. "hongze/hongze_robot/utils"
  9. "strings"
  10. "time"
  11. )
  12. func SendSms() {
  13. time.Sleep(time.Second)
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendSms Err:"+err.Error(), utils.EmailSendToUsers)
  18. }
  19. }()
  20. configItem, err := cache.GetSmsConfigByCache("mobile")
  21. if err != nil {
  22. return
  23. }
  24. if configItem.ConfigValue == "" {
  25. err = errors.New("mobile-val is empty")
  26. return
  27. }
  28. items, err := models.GetSmsRecord()
  29. if err != nil {
  30. return
  31. }
  32. var dingToken = []string{}
  33. if utils.RunMode == "release" {
  34. dingToken = []string{
  35. "5f9ae772343b20af806e1f8a6c27a3d8c1b57d8c36f468ab0a12d7eca1cf423c",
  36. "a3125f68a3b627443aa6756e2e973547d0bf3a346668f0f2794fb6d845e2ce68",
  37. "c13598ccb03fc6a2bfa2a7673237c9b375ba0e778a0f0db56ba358b01a9a8d89",
  38. }
  39. } else {
  40. dingToken = []string{
  41. "d2506ab774a3978dac6e69f59814b6f3f669cdde6ceefad67c109e34038de87e",
  42. }
  43. }
  44. for _, v := range items {
  45. if strings.Contains(configItem.ConfigValue, v.Mobile) {
  46. smsDateTime := v.SmsDateTime.Format(utils.FormatDateTime)
  47. msg := []string{
  48. "### " + v.Mobile,
  49. "---",
  50. "- <font>" + v.Body + "</font>",
  51. "- " + smsDateTime,
  52. }
  53. cli := dingtalk.InitDingTalk(dingToken, "hongze")
  54. //cli.SendTextMessage("content", dingtalk.WithAtAll())
  55. err = cli.SendMarkDownMessageBySlice("华瑞信息", msg)
  56. if err != nil {
  57. return
  58. }
  59. models.ModifySmsRecordIsSend(v.Mobile, v.SmsDate)
  60. time.Sleep(1 * time.Second)
  61. }
  62. }
  63. }
  64. func AddUnionCode() {
  65. list, err := models.GetSmsRecordList()
  66. if err != nil {
  67. fmt.Println("GetSmsRecordList Err:" + err.Error())
  68. return
  69. }
  70. for _, v := range list {
  71. fmt.Println(v.Id, v.Mobile)
  72. unionCode := utils.MD5(v.Mobile + v.Body + v.SmsDate)
  73. err = models.ModifySmsRecordUnicode(unionCode, v.Id)
  74. if err != nil {
  75. fmt.Println("ModifySmsRecordUnicode Err:" + err.Error())
  76. return
  77. }
  78. }
  79. }
  80. //func init() {
  81. // fmt.Println("start 111")
  82. // AddUnionCode()
  83. // fmt.Println("end 111")
  84. //}