作者 唐旭辉

添加sms服务接口

1 package sms 1 package sms
2 2
3 -import (  
4 - "ability/protocol"  
5 -  
6 - "github.com/astaxie/beego"  
7 - "github.com/astaxie/beego/httplib"  
8 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"  
9 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"  
10 -)  
11 -  
12 -type ISmsService interface {  
13 - Send(request *protocol.SmsCodeRequest) (err error) 3 +type ISmsServe interface {
  4 + Send() error //调用远端接口发送短信
  5 + TextContent() (string, error) //构建短信文本内容
  6 + ValidReturn() (string, error) //返回调用远端接口后的返回内容,可用于记日志
14 } 7 }
15 8
16 -type YunPianSmsService struct{}  
17 -  
18 -func assertImplement() {  
19 - var _ ISmsService = (*YunPianSmsService)(nil) 9 +type SmsServe struct {
  10 + ToPhones []string
  11 + Content string
  12 + ServeReturn interface{}
20 } 13 }
21 14
22 -//发送  
23 -  
24 -func (s *YunPianSmsService) Send(request *protocol.SmsCodeRequest) (err error) {  
25 - var (  
26 - resp *YunPianResponse  
27 - )  
28 - log.Debug("[sms] mobile:", request.Phone, " content:", request.Content)  
29 - if beego.BConfig.RunMode != "prod" {  
30 - return  
31 - }  
32 - post := httplib.Post(beego.AppConfig.String("yunpian_sms_sdk_url"))  
33 - post.Param("apikey", beego.AppConfig.String("yunpian_app_key"))  
34 - post.Param("mobile", request.Phone)  
35 - post.Param("text", request.Content)  
36 - if err = post.ToJSON(&resp); err != nil {  
37 - return  
38 - }  
39 - if resp.Code != 0 || resp.Mobile != request.Phone {  
40 - log.Error("yunpian send sms code:", resp.Code, " error msg:", resp.Msg)  
41 - err = common.NewErrorWithMsg(1, resp.Msg)  
42 - } 15 +func (s *SmsServe) Send() error {
43 return nil 16 return nil
44 } 17 }
45 18
46 -type YunPianResponse struct {  
47 - Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面  
48 - Msg string `json:"msg"` //例如""发送成功"",或者相应错误信息  
49 - Count int `json:"count"` //发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费)  
50 - Mobile string `json:"string"` //发送手机号  
51 - Fee float64 `json:"fee"` //扣费金额,单位:元,类型:双精度浮点型/double  
52 - Sid int64 `json:"sid"` //短信id,64位整型 19 +func (s *SmsServe) TextContent() (string, error) {
  20 + return "", nil
  21 +}
  22 +
  23 +func (s *SmsServe) ValidReturn() (string, error) {
  24 + return "", nil
53 } 25 }
  1 +package sms
  2 +
  3 +import (
  4 + "errors"
  5 +)
  6 +
  7 +var yunPianSmsTpl = map[string]string{
  8 + "10001": "【云片网】您的验证码是%s",
  9 +}
  10 +
  11 +const (
  12 + TplNotExit string = "tpl not exit"
  13 +)
  14 +
  15 +func getSmsTpl(code string) (string, error) {
  16 + if tpl, ok := yunPianSmsTpl[code]; ok {
  17 + return tpl, nil
  18 + }
  19 + return "", errors.New(TplNotExit)
  20 +}
  1 +package sms
  2 +
  3 +import (
  4 + "ability/protocol"
  5 + "errors"
  6 + "fmt"
  7 +
  8 + "github.com/astaxie/beego"
  9 + "github.com/astaxie/beego/httplib"
  10 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
  11 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  12 +)
  13 +
  14 +type ISmsService interface {
  15 + Send(request *protocol.SmsCodeRequest) (err error)
  16 + GetSmsTpl(code string, content ...interface{}) (string, error)
  17 +}
  18 +
  19 +type YunPianSmsService struct{}
  20 +
  21 +func assertImplement() {
  22 + var _ ISmsService = (*YunPianSmsService)(nil)
  23 +}
  24 +
  25 +//发送
  26 +
  27 +func (s *YunPianSmsService) Send(request *protocol.SmsCodeRequest) (err error) {
  28 + var (
  29 + resp *YunPianResponse
  30 + )
  31 + log.Debug("[sms] mobile:", request.Phone, " content:", request.Content)
  32 + if beego.BConfig.RunMode != "prod" {
  33 + return
  34 + }
  35 + post := httplib.Post(beego.AppConfig.String("yunpian_sms_sdk_url"))
  36 + post.Param("apikey", beego.AppConfig.String("yunpian_app_key"))
  37 + post.Param("mobile", request.Phone)
  38 + post.Param("text", request.Content)
  39 + if err = post.ToJSON(&resp); err != nil {
  40 + return
  41 + }
  42 + if resp.Code != 0 || resp.Mobile != request.Phone {
  43 + log.Error("yunpian send sms code:", resp.Code, " error msg:", resp.Msg)
  44 + err = common.NewErrorWithMsg(1, resp.Msg)
  45 + }
  46 + return nil
  47 +}
  48 +
  49 +func (s *YunPianSmsService) GetSmsTpl(code string, content ...interface{}) (string, error) {
  50 + if data, ok := yunPianSmsTpl[code]; !ok {
  51 + return fmt.Sprintf(data, content...), nil
  52 + }
  53 +
  54 + return "", errors.New("GetSmsTpl err,code=" + code)
  55 +}
  56 +
  57 +type YunPianResponse struct {
  58 + Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面
  59 + Msg string `json:"msg"` //例如""发送成功"",或者相应错误信息
  60 + Count int `json:"count"` //发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费)
  61 + Mobile string `json:"string"` //发送手机号
  62 + Fee float64 `json:"fee"` //扣费金额,单位:元,类型:双精度浮点型/double
  63 + Sid int64 `json:"sid"` //短信id,64位整型
  64 +}