diff --git a/push/getui.go b/push/getui_push.go similarity index 100% rename from push/getui.go rename to push/getui_push.go diff --git a/push/getui_sms.go b/push/getui_sms.go new file mode 100644 index 0000000..9d6a30d --- /dev/null +++ b/push/getui_sms.go @@ -0,0 +1,98 @@ +package push + +import ( + "encoding/json" + "errors" + "fmt" + "git.u8t.cn/open/gosdk/util" + log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" + "github.com/spf13/cast" + "time" +) + +type GetuiSms struct { + config *GetuiConfig + token string +} + +func NewGetuiSms(c *GetuiConfig) *GetuiSms { + return &GetuiSms{ + config: c, + } +} + +func (g *GetuiSms) getResult(rspBody []byte) (map[string]interface{}, error) { + result := make(map[string]interface{}) + + if err := json.Unmarshal(rspBody, &result); err != nil { + log.Errorf("json[%s] error :%s", string(rspBody), err.Error()) + return nil, err + } + code := cast.ToInt(result["result"]) + data := cast.ToStringMap(result["data"]) + + if code == 20000 { + return data, nil + } + + if code != 0 { + log.Errorf("json[%s] rsp error", string(rspBody)) + return nil, errors.New(string(rspBody)) + } + return data, nil +} + +func (g *GetuiSms) Token() string { + + timestamp := cast.ToString(time.Now().UnixMilli()) + signStr := fmt.Sprintf("%s%s%s", g.config.AppKey, timestamp, g.config.MasterSecret) + reqUrl := "https://openapi-smsp.getui.com/v1/sps/auth_sign" + params := make(map[string]string) + params["timestamp"] = timestamp + params["appId"] = g.config.AppId + params["sign"] = util.Sha256(signStr) + reqBody, _ := json.Marshal(params) + rspBody, err := HttpPostJson(reqUrl, nil, reqBody) + if err != nil { + log.Errorf("http post [%s] error :%s", string(reqBody), err.Error()) + return "" + } + + result, err := g.getResult(rspBody) + if err != nil { + log.Errorf("get Result error :%s", err.Error()) + return "" + } + + token := cast.ToString(result["authToken"]) + + g.token = token + + return g.token +} + +func (g *GetuiSms) Send(tmpId, phone string, data map[string]string) error { + + reqUrl := "https://openapi-smsp.getui.com/v1/sps/push_sms_list" + params := make(map[string]interface{}) + params["authToken"] = g.Token() + params["appId"] = g.config.AppId + params["smsTemplateId"] = tmpId + params["smsParam"] = data + params["recNum"] = []string{goutil.Md5(phone)} + reqBody, _ := json.Marshal(params) + rspBody, err := HttpPostJson(reqUrl, nil, reqBody) + if err != nil { + log.Errorf("http post [%s] error :%s", string(reqBody), err.Error()) + return err + } + fmt.Println(string(reqBody), string(rspBody)) + _, err = g.getResult(rspBody) + if err != nil { + log.Errorf("get Result error :%s", err.Error()) + return err + } + + return nil +}