作者 tangxvhui

更新 短信消息相关

  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +//记录 发送的短信消息
  6 +
  7 +type LogSms struct {
  8 + Id int
  9 + Phone string
  10 + TemplateId string
  11 + Template string
  12 + Value map[string]string
  13 + CreatedAt time.Time
  14 +}
  15 +
  16 +// 周期综合自评 短信消息提醒
  17 +func (sms *LogSms) SummaryEvaluationMessage(phone string, name string) {
  18 + *sms = LogSms{
  19 + Id: 0,
  20 + Phone: phone,
  21 + TemplateId: "5475050",
  22 + Template: "您好,#name#,百忙之中不要忘记填写今天的绩效自评反馈哦",
  23 + Value: map[string]string{
  24 + "name": name,
  25 + },
  26 + CreatedAt: time.Now(),
  27 + }
  28 +}
  1 +package models
  2 +
  3 +import "time"
  4 +
  5 +type LogSms struct {
  6 + tableName struct{} `comment:"记录短信消息" pg:"log_sms"`
  7 + Id int `pg:",pk"`
  8 + Phone string ``
  9 + TemplateId string ``
  10 + Template string ``
  11 + Value map[string]string ``
  12 + CreatedAt time.Time ``
  13 +}
  1 +package serviceGateway
  2 +
  3 +import (
  4 + "bytes"
  5 + "encoding/json"
  6 + "errors"
  7 + "net/http"
  8 + "time"
  9 +
  10 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/serviceGateway/reply"
  11 +)
  12 +
  13 +// 调用接口 发送短信消息
  14 +type SmsService struct {
  15 +}
  16 +
  17 +const (
  18 + sendNoticeSms string = "https://sms.fjmaimaimai.com:9897/service/sendNoticeSms"
  19 +)
  20 +
  21 +// SendNoticeSms 发送短信消息
  22 +// phone 手机号
  23 +// tplId 短信模板id
  24 +// tplValues 短信模板中带的参数 具体根据定义的模板参数
  25 +func (ssrv SmsService) SendNoticeSms(phone string, tplId int, tplValues map[string]string) error {
  26 + param := map[string]interface{}{
  27 + "phone": phone,
  28 + "tplId": tplId,
  29 + "tplValues": tplValues,
  30 + }
  31 +
  32 + buf := bytes.NewBuffer(nil)
  33 + jEncode := json.NewEncoder(buf)
  34 + err := jEncode.Encode(param)
  35 + if err != nil {
  36 + return err
  37 + }
  38 + req, err := http.NewRequest(http.MethodPost, sendNoticeSms, buf)
  39 + if err != nil {
  40 + return err
  41 + }
  42 + req.Header = map[string][]string{
  43 + "Content-Type": {"application/json"},
  44 + }
  45 + httpclient := http.Client{
  46 + Timeout: 30 * time.Second,
  47 + }
  48 +
  49 + resp, err := httpclient.Do(req)
  50 + if err != nil {
  51 + return err
  52 + }
  53 + defer resp.Body.Close()
  54 + var result reply.BaseReply
  55 + jDecoder := json.NewDecoder(resp.Body)
  56 + err = jDecoder.Decode(&result)
  57 + if err != nil {
  58 + return err
  59 + }
  60 + if result.Code != 0 {
  61 + return errors.New(result.Msg)
  62 + }
  63 + return nil
  64 +
  65 +}