SmsTemplate.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package submail
  2. import (
  3. "FollowUp_Notice/conf"
  4. "FollowUp_Notice/lib"
  5. "FollowUp_Notice/logs"
  6. "encoding/json"
  7. "github.com/go-resty/resty/v2"
  8. )
  9. type Template struct {
  10. Template_id string `json:"template_id"`
  11. Sms_title string `json:"sms_title"`
  12. Sms_signature string `json:"sms_signature"`
  13. Sms_content string `json:"sms_content"`
  14. Add_date int64 `json:"add_date"`
  15. Edit_date int64 `json:"edit_date"`
  16. Template_status string `json:"template_status"`
  17. Template_status_description string `json:"template_status_description"`
  18. Template_reject_reson string `json:"template_reject_reson"`
  19. }
  20. type TemplateRes struct {
  21. Status string `json:"status"`
  22. Template_id string `json:"template_id"`
  23. Template Template `json:"template"`
  24. }
  25. type XSendRes struct {
  26. Status string `json:"status"`
  27. Send_id string `json:"send_id"`
  28. Fee float64 `json:"fee"`
  29. Msg string `json:"msg"`
  30. Code string `json:"code"`
  31. }
  32. func SmsTemplate_Get(template_id string) (TemplateRes, error) {
  33. // Create a Resty Client
  34. client := resty.New()
  35. resp, err := client.R().
  36. SetQueryParams(map[string]string{
  37. "appid": conf.SUBMAIL_Sms_Appid,
  38. "signature": conf.SUBMAIL_Sms_Signature,
  39. "template_id": template_id,
  40. }).
  41. Get("https://api-v4.mysubmail.com/sms/template.json")
  42. if err != nil {
  43. return TemplateRes{}, err
  44. }
  45. temp := TemplateRes{}
  46. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  47. logs.Error(lib.FuncName(), err)
  48. return TemplateRes{}, err
  49. }
  50. return temp, nil
  51. }
  52. // 创建模板
  53. func SmsTemplate_Post(sms_signature, sms_content string) (TemplateRes, error) {
  54. client := resty.New()
  55. resp, err := client.R().
  56. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  57. SetFormData(map[string]string{
  58. "appid": conf.SUBMAIL_Sms_Appid,
  59. "signature": conf.SUBMAIL_Sms_Signature,
  60. "sms_title": "复诊通知",
  61. "sms_signature": sms_signature,
  62. "sms_content": sms_content,
  63. }).
  64. Post("http://api.mysubmail.com/sms/template.json")
  65. if err != nil {
  66. return TemplateRes{}, err
  67. }
  68. temp := TemplateRes{}
  69. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  70. logs.Error(lib.FuncName(), err)
  71. return TemplateRes{}, err
  72. }
  73. return temp, nil
  74. }
  75. // 修改模板
  76. func SmsTemplate_Put(template_id, sms_signature, sms_content string) (TemplateRes, error) {
  77. client := resty.New()
  78. resp, err := client.R().
  79. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  80. SetFormData(map[string]string{
  81. "appid": conf.SUBMAIL_Sms_Appid,
  82. "signature": conf.SUBMAIL_Sms_Signature,
  83. "template_id": template_id,
  84. "sms_title": "复诊通知",
  85. "sms_signature": sms_signature,
  86. "sms_content": sms_content,
  87. }).
  88. SetResult(&TemplateRes{}).
  89. Put("http://api.mysubmail.com/sms/template.json")
  90. if err != nil {
  91. return TemplateRes{}, err
  92. }
  93. temp := TemplateRes{}
  94. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  95. logs.Error(lib.FuncName(), err)
  96. return TemplateRes{}, err
  97. }
  98. return temp, nil
  99. }
  100. func SmsTemplate_Delete(template_id string) (TemplateRes, error) {
  101. client := resty.New()
  102. resp, err := client.R().
  103. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  104. SetFormData(map[string]string{
  105. "appid": conf.SUBMAIL_Sms_Appid,
  106. "signature": conf.SUBMAIL_Sms_Signature,
  107. "template_id": template_id,
  108. }).
  109. SetResult(&TemplateRes{}).
  110. Delete("http://api.mysubmail.com/sms/template.json")
  111. if err != nil {
  112. return TemplateRes{}, err
  113. }
  114. temp := TemplateRes{}
  115. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  116. logs.Error(lib.FuncName(), err)
  117. return TemplateRes{}, err
  118. }
  119. return temp, nil
  120. }
  121. // 批量发送短信
  122. func SmsMultiSend(template_id, content string) (TemplateRes, error) {
  123. client := resty.New()
  124. resp, err := client.R().
  125. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  126. SetFormData(map[string]string{
  127. "appid": conf.SUBMAIL_Sms_Appid,
  128. "signature": conf.SUBMAIL_Sms_Signature,
  129. "content": content,
  130. }).
  131. SetResult(&TemplateRes{}).
  132. Delete("https://api-v4.mysubmail.com/sms/multisend")
  133. if err != nil {
  134. return TemplateRes{}, err
  135. }
  136. temp := TemplateRes{}
  137. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  138. logs.Error(lib.FuncName(), err)
  139. return TemplateRes{}, err
  140. }
  141. return temp, nil
  142. }
  143. // 短信模板发送
  144. func SmsXSend(template_id, to, name, time string) (XSendRes, error) {
  145. type Vars struct {
  146. Name string `json:"name"`
  147. Time string `json:"time"`
  148. }
  149. vars := Vars{Name: name, Time: time}
  150. b, _ := json.Marshal(vars)
  151. client := resty.New()
  152. resp, err := client.R().
  153. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  154. SetFormData(map[string]string{
  155. "appid": conf.SUBMAIL_Sms_Appid,
  156. "signature": conf.SUBMAIL_Sms_Signature,
  157. "to": to,
  158. "project": template_id,
  159. "vars": string(b),
  160. }).
  161. SetResult(&XSendRes{}).
  162. Post("https://api-v4.mysubmail.com/sms/xsend")
  163. if err != nil {
  164. return XSendRes{}, err
  165. }
  166. temp := XSendRes{}
  167. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  168. logs.Error(lib.FuncName(), err)
  169. return XSendRes{}, err
  170. }
  171. return temp, nil
  172. }
  173. func SmsXSendBill(to, name, moneyFlag string) (XSendRes, error) {
  174. type Vars struct {
  175. Name string `json:"name"`
  176. Msg string `json:"msg"`
  177. }
  178. vars := Vars{Name: name, Msg: moneyFlag}
  179. b, _ := json.Marshal(vars)
  180. client := resty.New()
  181. resp, err := client.R().
  182. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  183. SetFormData(map[string]string{
  184. "appid": conf.SUBMAIL_Sms_Appid,
  185. "signature": conf.SUBMAIL_Sms_Signature,
  186. "to": to,
  187. "project": "LPdPa4",
  188. "vars": string(b),
  189. }).
  190. SetResult(&XSendRes{}).
  191. Post("https://api-v4.mysubmail.com/sms/xsend")
  192. if err != nil {
  193. return XSendRes{}, err
  194. }
  195. temp := XSendRes{}
  196. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  197. logs.Error(lib.FuncName(), err)
  198. return XSendRes{}, err
  199. }
  200. return temp, nil
  201. }