sms.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_robot/cache"
  6. "hongze/hongze_robot/models"
  7. "hongze/hongze_robot/services"
  8. "hongze/hongze_robot/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. //短信管理
  14. type SmsController struct {
  15. BaseCommonController
  16. }
  17. // @Title 短信转发接口
  18. // @Description 短信转发接口
  19. // @Param request body company.CompanyApplyApproveReq true "type json string"
  20. // @Success Ret=200 转发成功
  21. // @router /send [post]
  22. func (this *SmsController) SmsSend() {
  23. br := new(models.BaseResponse).Init()
  24. var err error
  25. defer func() {
  26. if err != nil {
  27. utils.FileLog.Info("send msg err: %s", err.Error())
  28. }
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. reqBody := this.Ctx.Input.RequestBody
  33. utils.FileLog.Info("%s", string(reqBody))
  34. req := make([]*models.SmsReq, 0)
  35. err = json.Unmarshal(reqBody, &req)
  36. if err != nil {
  37. br.Msg = "参数解析异常!"
  38. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  39. return
  40. }
  41. addList := make([]*models.SmsRecord, 0)
  42. for _, v := range req {
  43. v.Address = strings.Trim(v.Address, " ")
  44. if v.Sign != utils.Sign {
  45. br.Msg = "签名错误"
  46. br.ErrMsg = "签名错误;get sign:" + v.Sign + ";check sign:" + utils.Sign
  47. return
  48. }
  49. unionCode := utils.MD5(v.Address + v.Body + v.Date)
  50. count, err := models.GetSmsRecordCount(unionCode)
  51. if err != nil && err.Error() != utils.ErrNoRow() {
  52. br.Msg = "失败,请联系技术"
  53. br.ErrMsg = "失败;Err:" + err.Error()
  54. return
  55. }
  56. fmt.Println("count:", count)
  57. item, err := cache.GetSmsConfigByCache("mobile")
  58. if err != nil {
  59. br.Msg = "失败,请联系技术"
  60. br.ErrMsg = "获取配置信息失败;Err:" + err.Error()
  61. return
  62. }
  63. if item.ConfigValue == "" {
  64. br.Msg = "失败,请联系技术"
  65. br.ErrMsg = "配置信息为空"
  66. return
  67. }
  68. fmt.Println(item.ConfigValue)
  69. fmt.Println(v.Address)
  70. fmt.Println(strings.Contains(item.ConfigValue, v.Address))
  71. //判断记录是否存在
  72. if count <= 0 && strings.Contains(item.ConfigValue, v.Address) {
  73. var tm time.Time
  74. if v.Date != "" {
  75. smsDateInt64, _ := strconv.ParseInt(v.Date, 10, 64)
  76. tm = time.Unix(smsDateInt64/1e3, 0)
  77. }
  78. record := new(models.SmsRecord)
  79. record.CreateTime = time.Now()
  80. record.ModifyTime = time.Now()
  81. record.IsSend = 0
  82. record.SmsDateTime = tm
  83. record.Mobile = v.Address
  84. record.SmsDate = v.Date
  85. record.Body = v.Body
  86. record.UnionCode = unionCode
  87. addList = append(addList, record)
  88. }
  89. }
  90. if len(addList) > 0 {
  91. _, err = models.AddSmsRecordMulti(addList)
  92. if err != nil {
  93. br.Msg = "失败,请联系技术"
  94. br.ErrMsg = "新增记录失败;Err:" + err.Error()
  95. return
  96. }
  97. time.Sleep(1 * time.Second)
  98. go services.SendSms()
  99. }
  100. br.Ret = 200
  101. br.Success = true
  102. br.Msg = "发送成功"
  103. }
  104. //func init() {
  105. // fmt.Println("start")
  106. // smsDate:=`1620290685894`
  107. // int64, err := strconv.ParseInt(smsDate, 10, 64)
  108. // fmt.Println(err)
  109. // tm:=time.Unix(int64/1e3,0)
  110. // fmt.Println(tm)
  111. // fmt.Println(tm.Format(utils.FormatDateTime))
  112. // fmt.Println("end")
  113. //}
  114. //func init() {
  115. // fmt.Println("start")
  116. // services.SendSms()
  117. // fmt.Println("end")
  118. // return
  119. //}