template.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package sms
  2. import (
  3. "encoding/json"
  4. "github.com/go-resty/resty/v2"
  5. )
  6. type Template struct {
  7. Template_id string `json:"template_id"`
  8. Sms_title string `json:"sms_title"`
  9. Sms_signature string `json:"sms_signature"`
  10. Sms_content string `json:"sms_content"`
  11. Add_date string `json:"add_date"`
  12. Edit_date string `json:"edit_date"`
  13. Template_status string `json:"template_status"`
  14. Template_status_description string `json:"template_status_description"`
  15. }
  16. type TemplateRes struct {
  17. Status string `json:"status"`
  18. Template_id string `json:"template_id"`
  19. Template Template `json:"template"`
  20. }
  21. // 通过模板id获取模板
  22. func (t *SMS) SmsTemplate_Get(template_id string) (TemplateRes, error) {
  23. client := resty.New()
  24. resp, err := client.R().
  25. SetQueryParams(map[string]string{
  26. "appid": t.Appid,
  27. "signature": t.Signature,
  28. "template_id": template_id,
  29. }).
  30. Get("https://api-v4.mysubmail.com/sms/template.json")
  31. if err != nil {
  32. return TemplateRes{}, err
  33. }
  34. temp := TemplateRes{}
  35. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  36. return TemplateRes{}, err
  37. }
  38. return temp, nil
  39. }
  40. // 创建模板
  41. func (t *SMS) SmsTemplate_Post(sms_title, sms_signature, sms_content string) (TemplateRes, error) {
  42. client := resty.New()
  43. resp, err := client.R().
  44. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  45. SetFormData(map[string]string{
  46. "appid": t.Appid,
  47. "signature": t.Signature,
  48. "sms_title": sms_title,
  49. "sms_signature": sms_signature,
  50. "sms_content": sms_content,
  51. }).
  52. Post("http://api.mysubmail.com/sms/template.json")
  53. if err != nil {
  54. return TemplateRes{}, err
  55. }
  56. temp := TemplateRes{}
  57. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  58. return TemplateRes{}, err
  59. }
  60. return temp, nil
  61. }
  62. // 修改模板
  63. func (t *SMS) SmsTemplate_Put(template_id, sms_title, sms_signature, sms_content string) (TemplateRes, error) {
  64. client := resty.New()
  65. resp, err := client.R().
  66. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  67. SetFormData(map[string]string{
  68. "appid": t.Appid,
  69. "signature": t.Signature,
  70. "template_id": template_id,
  71. "sms_title": sms_title,
  72. "sms_signature": sms_signature,
  73. "sms_content": sms_content,
  74. }).
  75. SetResult(&TemplateRes{}).
  76. Put("http://api.mysubmail.com/sms/template.json")
  77. if err != nil {
  78. return TemplateRes{}, err
  79. }
  80. temp := TemplateRes{}
  81. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  82. return TemplateRes{}, err
  83. }
  84. return temp, nil
  85. }
  86. // 删除模板
  87. func (t *SMS) SmsTemplate_Delete(template_id string) (TemplateRes, error) {
  88. client := resty.New()
  89. resp, err := client.R().
  90. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  91. SetFormData(map[string]string{
  92. "appid": t.Appid,
  93. "signature": t.Signature,
  94. "template_id": template_id,
  95. }).
  96. SetResult(&TemplateRes{}).
  97. Delete("http://api.mysubmail.com/sms/template.json")
  98. if err != nil {
  99. return TemplateRes{}, err
  100. }
  101. temp := TemplateRes{}
  102. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  103. return TemplateRes{}, err
  104. }
  105. return temp, nil
  106. }
  107. // 短信模板发送
  108. // vars json字符串 格式 {"name1":"value1","name2":"value2"}
  109. func (t *SMS) Template_XSend(template_id, to, vars string) (SendRes, error) {
  110. client := resty.New()
  111. resp, err := client.R().
  112. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  113. SetFormData(map[string]string{
  114. "appid": t.Appid,
  115. "signature": t.Signature,
  116. "to": to,
  117. "project": template_id,
  118. "vars": vars,
  119. }).
  120. SetResult(&SendRes{}).
  121. Post("https://api-v4.mysubmail.com/sms/xsend")
  122. if err != nil {
  123. return SendRes{}, err
  124. }
  125. temp := SendRes{}
  126. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  127. return SendRes{}, err
  128. }
  129. return temp, nil
  130. }