package sms import ( "encoding/json" "github.com/go-resty/resty/v2" ) type Template struct { Template_id string `json:"template_id"` Sms_title string `json:"sms_title"` Sms_signature string `json:"sms_signature"` Sms_content string `json:"sms_content"` Add_date string `json:"add_date"` Edit_date string `json:"edit_date"` Template_status string `json:"template_status"` Template_status_description string `json:"template_status_description"` } type TemplateRes struct { Status string `json:"status"` Template_id string `json:"template_id"` Template Template `json:"template"` } // 通过模板id获取模板 func (t *SMS) SmsTemplate_Get(template_id string) (TemplateRes, error) { client := resty.New() resp, err := client.R(). SetQueryParams(map[string]string{ "appid": t.Appid, "signature": t.Signature, "template_id": template_id, }). Get("https://api-v4.mysubmail.com/sms/template.json") if err != nil { return TemplateRes{}, err } temp := TemplateRes{} if err = json.Unmarshal(resp.Body(), &temp); err != nil { return TemplateRes{}, err } return temp, nil } // 创建模板 func (t *SMS) SmsTemplate_Post(sms_title, sms_signature, sms_content string) (TemplateRes, error) { client := resty.New() resp, err := client.R(). SetHeader("Content-Type", "application/x-www-form-urlencoded"). SetFormData(map[string]string{ "appid": t.Appid, "signature": t.Signature, "sms_title": sms_title, "sms_signature": sms_signature, "sms_content": sms_content, }). Post("http://api.mysubmail.com/sms/template.json") if err != nil { return TemplateRes{}, err } temp := TemplateRes{} if err = json.Unmarshal(resp.Body(), &temp); err != nil { return TemplateRes{}, err } return temp, nil } // 修改模板 func (t *SMS) SmsTemplate_Put(template_id, sms_title, sms_signature, sms_content string) (TemplateRes, error) { client := resty.New() resp, err := client.R(). SetHeader("Content-Type", "application/x-www-form-urlencoded"). SetFormData(map[string]string{ "appid": t.Appid, "signature": t.Signature, "template_id": template_id, "sms_title": sms_title, "sms_signature": sms_signature, "sms_content": sms_content, }). SetResult(&TemplateRes{}). Put("http://api.mysubmail.com/sms/template.json") if err != nil { return TemplateRes{}, err } temp := TemplateRes{} if err = json.Unmarshal(resp.Body(), &temp); err != nil { return TemplateRes{}, err } return temp, nil } // 删除模板 func (t *SMS) SmsTemplate_Delete(template_id string) (TemplateRes, error) { client := resty.New() resp, err := client.R(). SetHeader("Content-Type", "application/x-www-form-urlencoded"). SetFormData(map[string]string{ "appid": t.Appid, "signature": t.Signature, "template_id": template_id, }). SetResult(&TemplateRes{}). Delete("http://api.mysubmail.com/sms/template.json") if err != nil { return TemplateRes{}, err } temp := TemplateRes{} if err = json.Unmarshal(resp.Body(), &temp); err != nil { return TemplateRes{}, err } return temp, nil } // 短信模板发送 // vars json字符串 格式 {"name1":"value1","name2":"value2"} func (t *SMS) Template_XSend(template_id, to, vars string) (SendRes, error) { client := resty.New() resp, err := client.R(). SetHeader("Content-Type", "application/x-www-form-urlencoded"). SetFormData(map[string]string{ "appid": t.Appid, "signature": t.Signature, "to": to, "project": template_id, "vars": vars, }). SetResult(&SendRes{}). Post("https://api-v4.mysubmail.com/sms/xsend") if err != nil { return SendRes{}, err } temp := SendRes{} if err = json.Unmarshal(resp.Body(), &temp); err != nil { return SendRes{}, err } return temp, nil }