审查视图

pkg/infrastructure/serviceGateway/httplib_sms_service.go 1.3 KB
tangxvhui authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
package serviceGateway

import (
	"bytes"
	"encoding/json"
	"errors"
	"net/http"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/serviceGateway/reply"
)

// 调用接口 发送短信消息
type SmsService struct {
}

const (
	sendNoticeSms string = "https://sms.fjmaimaimai.com:9897/service/sendNoticeSms"
)

// SendNoticeSms 发送短信消息
// phone 手机号
// tplId 短信模板id
// tplValues 短信模板中带的参数 具体根据定义的模板参数
func (ssrv SmsService) SendNoticeSms(phone string, tplId int, tplValues map[string]string) error {
	param := map[string]interface{}{
		"phone":     phone,
		"tplId":     tplId,
		"tplValues": tplValues,
	}

	buf := bytes.NewBuffer(nil)
	jEncode := json.NewEncoder(buf)
	err := jEncode.Encode(param)
	if err != nil {
		return err
	}
	req, err := http.NewRequest(http.MethodPost, sendNoticeSms, buf)
	if err != nil {
		return err
	}
	req.Header = map[string][]string{
		"Content-Type": {"application/json"},
	}
	httpclient := http.Client{
		Timeout: 30 * time.Second,
	}

	resp, err := httpclient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	var result reply.BaseReply
	jDecoder := json.NewDecoder(resp.Body)
	err = jDecoder.Decode(&result)
	if err != nil {
		return err
	}
	if result.Code != 0 {
		return errors.New(result.Msg)
	}
	return nil

}