sms.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package controllers
  2. import (
  3. "hongze/hongze_robot/models"
  4. "hongze/hongze_robot/utils"
  5. "strconv"
  6. "time"
  7. "github.com/blinkbean/dingtalk"
  8. )
  9. //短信管理
  10. type SmsController struct {
  11. BaseCommonController
  12. }
  13. // @Title 短信转发接口
  14. // @Description 短信转发接口
  15. // @Param request body company.CompanyApplyApproveReq true "type json string"
  16. // @Success Ret=200 转发成功
  17. // @router /send [post]
  18. func (this *SmsController) SmsSend() {
  19. br := new(models.BaseResponse).Init()
  20. var err error
  21. defer func() {
  22. if err != nil {
  23. utils.FileLog.Info("send msg err: %s", err.Error())
  24. }
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sign := this.GetString("Sign")
  29. address := this.GetString("Address")
  30. body := this.GetString("Body")
  31. name := this.GetString("Name")
  32. date := this.GetString("Date")
  33. utils.FileLog.Info("send msg: %s; %s; %s; %s; %s", sign, address, body, name, date)
  34. count, err := models.GetSmsRecordCount(address, date)
  35. if err != nil && err.Error() != utils.ErrNoRow() {
  36. br.Msg = "失败,请联系技术"
  37. br.ErrMsg = "失败;Err:" + err.Error()
  38. return
  39. }
  40. //判断记录是否存在
  41. if count <= 0 {
  42. var dingToken = []string{"5f9ae772343b20af806e1f8a6c27a3d8c1b57d8c36f468ab0a12d7eca1cf423c"}
  43. var smsDateTime string
  44. if date != "" {
  45. smsDateInt64, _ := strconv.ParseInt(date, 10, 64)
  46. tm := time.Unix(smsDateInt64/1e3, 0)
  47. smsDateTime = tm.Format(utils.FormatDateTime)
  48. }
  49. if smsDateTime == "" {
  50. smsDateTime = date
  51. }
  52. msg := []string{
  53. "### " + address,
  54. "---",
  55. "- <font color=#00ff00 size=6>" + body + "</font>",
  56. "- " + smsDateTime,
  57. }
  58. cli := dingtalk.InitDingTalk(dingToken, "hongze")
  59. //cli.SendTextMessage("content", dingtalk.WithAtAll())
  60. err = cli.SendMarkDownMessageBySlice("华瑞信息", msg)
  61. if err != nil {
  62. br.Msg = "失败,请联系技术"
  63. br.ErrMsg = "发送钉钉消息失败;Err:" + err.Error()
  64. return
  65. }
  66. record := new(models.SmsRecord)
  67. record.CreateTime = time.Now()
  68. record.Mobile = address
  69. record.SmsDate = date
  70. record.Body = body
  71. _, err = models.AddSmsRecord(record)
  72. if err != nil {
  73. br.Msg = "失败,请联系技术"
  74. br.ErrMsg = "新增记录失败;Err:" + err.Error()
  75. return
  76. }
  77. }
  78. br.Ret = 200
  79. br.Success = true
  80. br.Msg = "发送成功"
  81. }
  82. //func init() {
  83. // fmt.Println("start")
  84. // smsDate:=`1620290685894`
  85. // int64, err := strconv.ParseInt(smsDate, 10, 64)
  86. // fmt.Println(err)
  87. // tm:=time.Unix(int64/1e3,0)
  88. // fmt.Println(tm)
  89. // fmt.Println(tm.Format(utils.FormatDateTime))
  90. // fmt.Println("end")
  91. //}