sms.go 2.0 KB

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