finclip-app-manager/infrastructure/client/httpcall/mop_sms_server.go

173 lines
4.7 KiB
Go

package httpcall
import (
"context"
"encoding/json"
"errors"
"finclip-app-manager/infrastructure/config"
)
const (
THIRD_ENV_ZHAOS = "zhaos"
// 运营端-审核管理,同意/驳回小程序审核申请
SMS_APP_PUBLISH_APPROVE = "APP_PUBLISH_APPROVE"
SMS_APP_PUBLISH_REJECT = "APP_PUBLISH_REJECT"
SMS_APP_PUBLISH_IMMED = "APP_PUBLISH_IMMED"
SMS_APP_PUBLISH_SUCC = "APP_PUBLISH_SUCC"
SMS_ZHAOS_APP_PUBLISH_APPROVE = "ZHAOS_APP_PUBLISH_APPROVE"
SMS_ZHAOS_APP_PUBLISH_REJECT = "ZHAOS_APP_PUBLISH_REJECT"
SMS_ZHAOS_APP_PUBLISH_IMMED = "ZHAOS_APP_PUBLISH_IMMED"
SMS_ZHAOS_APP_PUBLISH_SUCC = "ZHAOS_APP_PUBLISH_SUCC"
// 运营端-小程序关联管理,同意/驳回小程序关联审核申请
SMS_LINK_AUDIT_APPLY = "LINK_AUDIT_APPLY"
SMS_LINK_AUDIT_REJECT = "LINK_AUDIT_REJECT"
)
type SendSmsNotifyReq struct {
Type string `json:"type"`
Phone string `json:"phone"`
Params string `json:"params"`
}
type SendSmsNotifyRsp struct {
Errcode string `json:"errcode"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
}
func validateEnvAndType(env, smsType string) bool {
if smsType == SMS_LINK_AUDIT_APPLY && env != config.ENV_FDEP {
return false
}
if smsType == SMS_LINK_AUDIT_REJECT && env != config.ENV_FDEP {
return false
}
if env != config.ENV_FDEP && env != config.ENV_UAT {
return false
}
return true
}
func (c *Client) SendSmsNotify(ctx context.Context, smsType, phone, params string) error {
if !validateEnvAndType(config.GetConfig().PublishEnv, smsType) {
return nil
}
var req SendSmsNotifyReq
req.Type = smsType
req.Phone = phone
req.Params = params
body, _ := json.Marshal(req)
url := config.GetConfig().SmsServerHost + "/api/v1/mop/sms-server/notify"
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("send sms notify err:" + err.Error())
return err
}
if rsp.StatusCode() != 200 {
log.Errorf("send sms notify status err:" + rsp.String())
return errors.New("status err")
}
var smsRsp SendSmsNotifyRsp
err = json.Unmarshal(rsp.Body(), &smsRsp)
if err != nil {
log.Errorf("json.Unmarshal error:" + err.Error())
return err
}
log.Debugf("send sms notify rsp:%+v", rsp.String())
return nil
}
type SendThirdSmsReq struct {
Type string `json:"type"`
Phone string `json:"phone"`
Params string `json:"params"`
}
type SendThirdSmsRsp struct {
Errcode string `json:"errcode"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
}
func (c *Client) SendThirdSms(ctx context.Context, smsType, phone, params string) error {
if !validateEnvAndType(config.GetConfig().PublishEnv, smsType) {
return nil
}
var req SendThirdSmsReq
req.Type = smsType
req.Phone = phone
req.Params = params
body, _ := json.Marshal(req)
url := config.GetConfig().SmsGateWayUrl
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("send third sms err:" + err.Error())
return err
}
if rsp.StatusCode() != 200 {
log.Errorf("send third sms status err:" + rsp.String())
return errors.New("status err")
}
var smsRsp SendThirdSmsRsp
err = json.Unmarshal(rsp.Body(), &smsRsp)
if err != nil {
log.Errorf("send third sms json.Unmarshal error:" + err.Error())
return err
}
log.Debugf("send third sms rsp:%+v", rsp.String())
return nil
}
type SendDelaySmsReq struct {
SmsType string `json:"smsType"`
Phone string `json:"phone"`
AppId string `json:"appId"`
AppName string `json:"appName"`
Sequence int `json:"sequence"`
}
type SendDelaySmsRsp struct {
Errcode string `json:"errcode"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
}
func (c *Client) SendDelaySms(ctx context.Context, smsType, phone string, appId, appName string, sequence int) error {
if !validateEnvAndType(config.GetConfig().PublishEnv, smsType) {
return nil
}
var req SendDelaySmsReq
req.SmsType = smsType
req.Phone = phone
req.AppId = appId
req.AppName = appName
req.Sequence = sequence
body, _ := json.Marshal(req)
url := config.GetConfig().AuditDelaySmsURL
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("send sms notify err:" + err.Error())
return err
}
if rsp.StatusCode() != 200 {
log.Errorf("send delay sms status err:" + rsp.String())
return errors.New("status err")
}
var smsRsp SendDelaySmsRsp
err = json.Unmarshal(rsp.Body(), &smsRsp)
if err != nil {
log.Errorf("json.Unmarshal error:" + err.Error())
return err
}
log.Debugf("send delay sms rsp:%+v", rsp.String())
return nil
}