作者 tangxvhui

更新 短信消息相关

package domain
import "time"
//记录 发送的短信消息
type LogSms struct {
Id int
Phone string
TemplateId string
Template string
Value map[string]string
CreatedAt time.Time
}
// 周期综合自评 短信消息提醒
func (sms *LogSms) SummaryEvaluationMessage(phone string, name string) {
*sms = LogSms{
Id: 0,
Phone: phone,
TemplateId: "5475050",
Template: "您好,#name#,百忙之中不要忘记填写今天的绩效自评反馈哦",
Value: map[string]string{
"name": name,
},
CreatedAt: time.Now(),
}
}
... ...
package models
import "time"
type LogSms struct {
tableName struct{} `comment:"记录短信消息" pg:"log_sms"`
Id int `pg:",pk"`
Phone string ``
TemplateId string ``
Template string ``
Value map[string]string ``
CreatedAt time.Time ``
}
... ...
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
}
... ...