Merge remote-tracking branch 'origin/dev' into test
正在显示
21 个修改的文件
包含
463 行增加
和
13 行删除
1 | -FROM 192.168.0.243:5000/mmm/mmmopp:20200422 as builder | ||
2 | -#FROM golang:1.13 as builder | 1 | +#FROM 192.168.0.243:5000/mmm/mmmopp:20200110 as builder |
2 | +FROM golang:1.13 as builder | ||
3 | ENV GOPROXY https://goproxy.cn | 3 | ENV GOPROXY https://goproxy.cn |
4 | ENV GO111MODULE on | 4 | ENV GO111MODULE on |
5 | ENV GOPATH /go | 5 | ENV GOPATH /go |
6 | -#RUN git clone http://gitlab.fjmaimaimai.com/mmm-go/gocomm.git /go/src/gocomm | 6 | +RUN git clone http://gitlab.fjmaimaimai.com/mmm-go/gocomm.git /go/src/gocomm |
7 | RUN cd /go/src/gocomm \ | 7 | RUN cd /go/src/gocomm \ |
8 | && git pull | 8 | && git pull |
9 | WORKDIR /go/src/openapi | 9 | WORKDIR /go/src/openapi |
@@ -13,8 +13,8 @@ COPY go.mod . | @@ -13,8 +13,8 @@ COPY go.mod . | ||
13 | COPY . . | 13 | COPY . . |
14 | RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -o openapi main.go | 14 | RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -o openapi main.go |
15 | 15 | ||
16 | -#FROM alpine:latest | ||
17 | -FROM 192.168.0.243:5000/mmm/mmmopp:20200110 | 16 | +FROM alpine:latest |
17 | +#FROM 192.168.0.243:5000/mmm/mmmopp:20200110 | ||
18 | RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ | 18 | RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ |
19 | && apk add --no-cache tzdata \ | 19 | && apk add --no-cache tzdata \ |
20 | && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ | 20 | && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ |
@@ -8,6 +8,7 @@ require ( | @@ -8,6 +8,7 @@ require ( | ||
8 | github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible | 8 | github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible |
9 | github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309 | 9 | github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309 |
10 | github.com/astaxie/beego v1.10.0 | 10 | github.com/astaxie/beego v1.10.0 |
11 | + github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect | ||
11 | github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect | 12 | github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect |
12 | github.com/fatih/structs v1.1.0 // indirect | 13 | github.com/fatih/structs v1.1.0 // indirect |
13 | github.com/gavv/httpexpect v2.0.0+incompatible | 14 | github.com/gavv/httpexpect v2.0.0+incompatible |
1 | package main | 1 | package main |
2 | 2 | ||
3 | -import _ "openapi/pkg/log" | ||
4 | - | ||
5 | import ( | 3 | import ( |
4 | + "fmt" | ||
5 | + | ||
6 | "github.com/astaxie/beego" | 6 | "github.com/astaxie/beego" |
7 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/config" | ||
7 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 8 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
8 | - | 9 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis" |
10 | + "openapi/pkg/constant" | ||
9 | _ "openapi/pkg/infrastructure/bgorm" | 11 | _ "openapi/pkg/infrastructure/bgorm" |
12 | + _ "openapi/pkg/log" | ||
10 | _ "openapi/pkg/port/beego" | 13 | _ "openapi/pkg/port/beego" |
11 | ) | 14 | ) |
12 | 15 | ||
@@ -18,11 +21,20 @@ func main() { | @@ -18,11 +21,20 @@ func main() { | ||
18 | //constant.DebugConfig() | 21 | //constant.DebugConfig() |
19 | 22 | ||
20 | //https | 23 | //https |
21 | - beego.BConfig.Listen.EnableHTTPS = true | ||
22 | - beego.BConfig.Listen.Graceful = true | ||
23 | - beego.BConfig.Listen.HTTPSPort = 443 | ||
24 | - beego.BConfig.Listen.HTTPSCertFile = "conf/_.fjmaimaimai.com_bundle.crt" | ||
25 | - beego.BConfig.Listen.HTTPSKeyFile = "conf/fjmaimaimai.com_RSA.fjmaimaimai.com_RSA.key" | 24 | + //beego.BConfig.Listen.EnableHTTPS = true |
25 | + //beego.BConfig.Listen.Graceful = true | ||
26 | + //beego.BConfig.Listen.HTTPSPort = 443 | ||
27 | + //beego.BConfig.Listen.HTTPSCertFile = "conf/_.fjmaimaimai.com_bundle.crt" | ||
28 | + //beego.BConfig.Listen.HTTPSKeyFile = "conf/fjmaimaimai.com_RSA.fjmaimaimai.com_RSA.key" | ||
29 | + | ||
30 | + err := redis.Init(config.Redis{ | ||
31 | + Addr: fmt.Sprintf("%v:%v", constant.REDIS_HOST, constant.REDIS_PORT), | ||
32 | + Password: constant.REDIS_AUTH, | ||
33 | + MaxIdle: 50, | ||
34 | + }) | ||
35 | + if err != nil { | ||
36 | + log.Error(err) | ||
37 | + } | ||
26 | 38 | ||
27 | beego.Run() | 39 | beego.Run() |
28 | } | 40 | } |
@@ -48,8 +48,13 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | @@ -48,8 +48,13 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | ||
48 | return | 48 | return |
49 | } | 49 | } |
50 | if extInfo, ok := appInfo.GetExtInfo(); ok { | 50 | if extInfo, ok := appInfo.GetExtInfo(); ok { |
51 | + if len(extInfo.Intent) > 0 { | ||
51 | requestOriginal.Ext["intent"] = extInfo.Intent | 52 | requestOriginal.Ext["intent"] = extInfo.Intent |
52 | } | 53 | } |
54 | + if len(extInfo.Sound) > 0 { | ||
55 | + requestOriginal.Ext["sound"] = extInfo.Sound | ||
56 | + } | ||
57 | + } | ||
53 | if len(deviceList) == 0 { | 58 | if len(deviceList) == 0 { |
54 | err = protocol.NewSuccessWithMessage(fmt.Sprintf("接收人:%v 未查询到注册的设备信息!", request.Receivers)) | 59 | err = protocol.NewSuccessWithMessage(fmt.Sprintf("接收人:%v 未查询到注册的设备信息!", request.Receivers)) |
55 | return | 60 | return |
pkg/application/sms/service/sms.go
0 → 100644
1 | +package service | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis" | ||
8 | + protocol "openapi/pkg/domain" | ||
9 | + "openapi/pkg/infrastructure/sms" | ||
10 | + "openapi/pkg/infrastructure/sms/yunpian" | ||
11 | + "strings" | ||
12 | +) | ||
13 | + | ||
14 | +// SmsCode 发送短信验证码 | ||
15 | +func SmsCode(header *protocol.RequestHeader, request *protocol.SmsCodeRequest) (rsp interface{}, err error) { | ||
16 | + code := common.RandomStringWithChars(6, protocol.Nums) | ||
17 | + var smsSvr sms.ISmsService = &yunpian.YPSmsService{} | ||
18 | + content := fmt.Sprintf("【买买买信息科技】%v(手机验证码,请完成验证),如非本人操作,请忽略本短信", code) | ||
19 | + // save log to db | ||
20 | + log.Debug(fmt.Sprintf("【sms】 smscode phone:%v code:%v", request.Phone, code)) | ||
21 | + // save to redis | ||
22 | + if err = redis.Set(smsRedisKey(request.Phone), code, sms.DefaultSmsCodeTimeOut); err != nil { | ||
23 | + logError(err) | ||
24 | + err = protocol.NewCustomMessage(1001, "请求超时,请检查手机号是否正确或网络连接状态") | ||
25 | + return nil, err | ||
26 | + } | ||
27 | + if err = smsSvr.Send(&sms.Options{Phone: request.Phone, SendType: sms.SendSingle, Content: content}); err != nil { | ||
28 | + logError(err) | ||
29 | + err = protocol.NewCustomMessage(1001, "请求超时,请检查手机号是否正确或网络连接状态") | ||
30 | + return nil, err | ||
31 | + } | ||
32 | + return | ||
33 | +} | ||
34 | + | ||
35 | +// CheckSmsCode 检查短信验证码 | ||
36 | +func CheckSmsCode(header *protocol.RequestHeader, request *protocol.CheckSmsCodeRequest) (rsp interface{}, err error) { | ||
37 | + var value string | ||
38 | + if value, err = redis.Get(smsRedisKey(request.Phone)); err != nil || len(value) == 0 { | ||
39 | + logError(err) | ||
40 | + err = protocol.NewCustomMessage(1004, "验证码已失效") | ||
41 | + return nil, err | ||
42 | + } | ||
43 | + if !strings.EqualFold(value, request.Code) { | ||
44 | + err = protocol.NewCustomMessage(1004, "验证码错误,重新输入") | ||
45 | + return nil, err | ||
46 | + } | ||
47 | + redis.Del(smsRedisKey(request.Phone)) | ||
48 | + return | ||
49 | +} | ||
50 | + | ||
51 | +// SendSmsNotice 发送短信通知 | ||
52 | +func SendSmsNotice(header *protocol.RequestHeader, request *protocol.SendSmsNoticeRequest) (rsp interface{}, err error) { | ||
53 | + var smsSvr sms.ISmsService = &yunpian.YPSmsService{} | ||
54 | + options := sms.NewOptions( | ||
55 | + sms.WithPhone(request.Phone), | ||
56 | + sms.WithSendType(sms.SendSingleByTpl), | ||
57 | + sms.WithTpl(request.TplId, request.TplValues), | ||
58 | + ) | ||
59 | + err = smsSvr.Send(options) | ||
60 | + return | ||
61 | +} | ||
62 | + | ||
63 | +func logError(err error) { | ||
64 | + log.Error(fmt.Sprintf("【sms】 %v", err.Error())) | ||
65 | +} | ||
66 | + | ||
67 | +func smsRedisKey(phone string) string { | ||
68 | + return strings.Join([]string{"sms", "smscode", phone}, ":") | ||
69 | +} |
@@ -8,11 +8,13 @@ import ( | @@ -8,11 +8,13 @@ import ( | ||
8 | var ( | 8 | var ( |
9 | LogFilePath string = "F:/log/app.log" | 9 | LogFilePath string = "F:/log/app.log" |
10 | LogLevel string = "error" | 10 | LogLevel string = "error" |
11 | + RunMode string = "dev" | ||
11 | ) | 12 | ) |
12 | 13 | ||
13 | func init() { | 14 | func init() { |
14 | LogLevel = config.StringDefault("log_level", LogLevel) | 15 | LogLevel = config.StringDefault("log_level", LogLevel) |
15 | LogFilePath = config.StringDefault("aliyun_logs_access", LogFilePath) | 16 | LogFilePath = config.StringDefault("aliyun_logs_access", LogFilePath) |
17 | + RunMode = config.StringDefault("RUN_MODE", RunMode) | ||
16 | } | 18 | } |
17 | 19 | ||
18 | func DebugConfig() { | 20 | func DebugConfig() { |
pkg/constant/redis.go
0 → 100644
1 | +package constant | ||
2 | + | ||
3 | +import "os" | ||
4 | + | ||
5 | +var REDIS_HOST = "127.0.0.1" | ||
6 | +var REDIS_PORT = "6379" | ||
7 | +var REDIS_AUTH = "" | ||
8 | + | ||
9 | +func init() { | ||
10 | + if os.Getenv("REDIS_HOST") != "" { | ||
11 | + REDIS_HOST = os.Getenv("REDIS_HOST") | ||
12 | + REDIS_AUTH = os.Getenv("REDIS_AUTH") | ||
13 | + } | ||
14 | + if os.Getenv("REDIS_PORT") != "" { | ||
15 | + REDIS_PORT = os.Getenv("REDIS_PORT") | ||
16 | + } | ||
17 | + if _, ok := os.LookupEnv("REDIS_AUTH"); ok { | ||
18 | + REDIS_AUTH = os.Getenv("REDIS_AUTH") | ||
19 | + } | ||
20 | +} |
pkg/constant/sms.go
0 → 100644
@@ -5,4 +5,8 @@ var errmessge ErrorMap = map[int]string{ | @@ -5,4 +5,8 @@ var errmessge ErrorMap = map[int]string{ | ||
5 | 1: "系统异常", | 5 | 1: "系统异常", |
6 | 2: "参数错误", | 6 | 2: "参数错误", |
7 | 113: "签名验证失败", | 7 | 113: "签名验证失败", |
8 | + 1001: "请求超时,请检查手机号是否正确或网络连接状态", | ||
9 | + 1002: "请输入正确的手机号码", | ||
10 | + 1003: "验证码错误,重新输入", | ||
11 | + 1004: "验证码已失效", | ||
8 | } | 12 | } |
@@ -65,6 +65,7 @@ type AppInfo struct { | @@ -65,6 +65,7 @@ type AppInfo struct { | ||
65 | 65 | ||
66 | type ExtInfo struct { | 66 | type ExtInfo struct { |
67 | Intent string `json:"intent"` | 67 | Intent string `json:"intent"` |
68 | + Sound string `json:"sound,omitempty"` | ||
68 | } | 69 | } |
69 | 70 | ||
70 | func (t *AppInfo) GetExtInfo() (*ExtInfo, bool) { | 71 | func (t *AppInfo) GetExtInfo() (*ExtInfo, bool) { |
pkg/domain/sms.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +var Nums = "0123456789" | ||
4 | + | ||
5 | +/*发送短信验证码*/ | ||
6 | +type SmsCodeRequest struct { | ||
7 | + Project string `json:"project"` | ||
8 | + Phone string `json:"phone" valid:"required"` | ||
9 | +} | ||
10 | + | ||
11 | +type CheckSmsCodeRequest struct { | ||
12 | + Phone string `json:"phone" valid:"required"` | ||
13 | + Code string `json:"code" valid:"required"` | ||
14 | +} | ||
15 | + | ||
16 | +type SendSmsNoticeRequest struct { | ||
17 | + // 手机号 | ||
18 | + Phone string `json:"phone" valid:"required"` | ||
19 | + // 模板id | ||
20 | + TplId int `json:"tplId" valid:"required"` | ||
21 | + // 模板参数 | ||
22 | + TplValues map[string]interface{} `json:"tplValues"` | ||
23 | +} |
@@ -183,6 +183,11 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { | @@ -183,6 +183,11 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { | ||
183 | 183 | ||
184 | authMux.Lock() | 184 | authMux.Lock() |
185 | defer authMux.Unlock() | 185 | defer authMux.Unlock() |
186 | + // recheck | ||
187 | + if authtoken != "" && expire.Unix() > time.Now().Unix() { | ||
188 | + token = authtoken | ||
189 | + return | ||
190 | + } | ||
186 | url := notify.Url(notify.Options.AppId, authSign) | 191 | url := notify.Url(notify.Options.AppId, authSign) |
187 | notify.Request = httplib.Post(strings.TrimSpace(url)) | 192 | notify.Request = httplib.Post(strings.TrimSpace(url)) |
188 | req := &AuthSignRequest{ | 193 | req := &AuthSignRequest{ |
@@ -3,6 +3,7 @@ package getuiV2 | @@ -3,6 +3,7 @@ package getuiV2 | ||
3 | import ( | 3 | import ( |
4 | "openapi/pkg/infrastructure/push" | 4 | "openapi/pkg/infrastructure/push" |
5 | "openapi/pkg/infrastructure/utils" | 5 | "openapi/pkg/infrastructure/utils" |
6 | + "sync" | ||
6 | "testing" | 7 | "testing" |
7 | ) | 8 | ) |
8 | 9 | ||
@@ -77,3 +78,29 @@ func TestGetuiPrd(t *testing.T) { | @@ -77,3 +78,29 @@ func TestGetuiPrd(t *testing.T) { | ||
77 | t.Fatal(err) | 78 | t.Fatal(err) |
78 | } | 79 | } |
79 | } | 80 | } |
81 | + | ||
82 | +func TestGetAuthToken(t *testing.T) { | ||
83 | + var wg sync.WaitGroup | ||
84 | + round := 100 | ||
85 | + notification := &GetuiNotification{} | ||
86 | + var tokenMap = make(map[string]string) | ||
87 | + notification.Init( | ||
88 | + push.DebugModule(true), | ||
89 | + | ||
90 | + push.AppId("WgrbaaStTk7JElrXOCgUg6"), | ||
91 | + push.AppKey("FG5lbqVrHa5rS9NVfxNP7"), | ||
92 | + push.AppMasterSecret("FW3jMNLJrRARYKv2iqA5H5"), | ||
93 | + ) | ||
94 | + for i := 0; i < round; i++ { | ||
95 | + wg.Add(1) | ||
96 | + go func() { | ||
97 | + defer wg.Done() | ||
98 | + token, _ := notification.GetAuthToken() | ||
99 | + tokenMap[token] = token | ||
100 | + }() | ||
101 | + } | ||
102 | + wg.Wait() | ||
103 | + if len(tokenMap) > 1 { | ||
104 | + t.Fatalf("token want 1 get %v", len(tokenMap)) | ||
105 | + } | ||
106 | +} |
@@ -72,6 +72,16 @@ func NewPushMessage(option *push.Options) map[string]interface{} { | @@ -72,6 +72,16 @@ func NewPushMessage(option *push.Options) map[string]interface{} { | ||
72 | 72 | ||
73 | // setting | 73 | // setting |
74 | m.AddFiled("settings.ttl", 3600*24) | 74 | m.AddFiled("settings.ttl", 3600*24) |
75 | + /* | ||
76 | + 默认所有通道的策略选择1-4 | ||
77 | + 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道; | ||
78 | + 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线; | ||
79 | + 3: 表示该消息只通过个推通道下发,不考虑用户是否在线; | ||
80 | + 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。 | ||
81 | + 其中名称可填写: ios、st、hw、xm、vv、mz、op | ||
82 | + */ | ||
83 | + m.AddFiled("settings.strategy.default", 1) | ||
84 | + m.AddFiled("settings.strategy.ios", 4) | ||
75 | 85 | ||
76 | // audience | 86 | // audience |
77 | m.AddFiled("audience.cid", []string{option.ClientId}) | 87 | m.AddFiled("audience.cid", []string{option.ClientId}) |
pkg/infrastructure/sms/sms.go
0 → 100644
1 | +package sms | ||
2 | + | ||
3 | +const ( | ||
4 | + SendSingle SendType = 1 | ||
5 | + SendList SendType = 2 | ||
6 | + SendSingleByTpl SendType = 10 | ||
7 | +) | ||
8 | + | ||
9 | +const DefaultSmsCodeTimeOut int64 = 60 * 2 | ||
10 | + | ||
11 | +type ( | ||
12 | + SendType int | ||
13 | + ISmsService interface { | ||
14 | + Send(option *Options) error | ||
15 | + } | ||
16 | + Options struct { | ||
17 | + Phone string | ||
18 | + Content string | ||
19 | + SendType SendType | ||
20 | + TplId int | ||
21 | + TplValues map[string]interface{} | ||
22 | + } | ||
23 | + option func(o *Options) | ||
24 | +) | ||
25 | + | ||
26 | +func NewOptions(options ...option) *Options { | ||
27 | + o := &Options{} | ||
28 | + for i := range options { | ||
29 | + options[i](o) | ||
30 | + } | ||
31 | + return o | ||
32 | +} | ||
33 | +func WithPhone(phone string) option { | ||
34 | + return func(o *Options) { | ||
35 | + o.Phone = phone | ||
36 | + } | ||
37 | +} | ||
38 | +func WithSendType(sendType SendType) option { | ||
39 | + return func(o *Options) { | ||
40 | + o.SendType = sendType | ||
41 | + } | ||
42 | +} | ||
43 | +func WithTpl(tplId int, tplValues map[string]interface{}) option { | ||
44 | + return func(o *Options) { | ||
45 | + o.TplId = tplId | ||
46 | + o.TplValues = tplValues | ||
47 | + } | ||
48 | +} |
1 | +package yunpian | ||
2 | + | ||
3 | +import ( | ||
4 | + "bytes" | ||
5 | + "fmt" | ||
6 | + "github.com/astaxie/beego/httplib" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
9 | + "net/url" | ||
10 | + "openapi/pkg/constant" | ||
11 | + "openapi/pkg/infrastructure/sms" | ||
12 | + "strconv" | ||
13 | +) | ||
14 | + | ||
15 | +type ( | ||
16 | + YPSmsService struct{} | ||
17 | + YPSmsResponse struct { | ||
18 | + Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面 | ||
19 | + Msg string `json:"msg"` //例如""发送成功"",或者相应错误信息 | ||
20 | + Count int `json:"count"` //发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费) | ||
21 | + Mobile string `json:"string"` //发送手机号 | ||
22 | + Fee float64 `json:"fee"` //扣费金额,单位:元,类型:双精度浮点型/double | ||
23 | + Sid int64 `json:"sid"` //短信id,64位整型 | ||
24 | + } | ||
25 | +) | ||
26 | + | ||
27 | +func (svr *YPSmsService) Send(option *sms.Options) error { | ||
28 | + var err error | ||
29 | + if option.SendType == sms.SendSingle { | ||
30 | + _, err = sendSingle(option) | ||
31 | + } else if option.SendType == sms.SendSingleByTpl { | ||
32 | + _, err = sendSingleByTpl(option) | ||
33 | + } | ||
34 | + return err | ||
35 | +} | ||
36 | + | ||
37 | +func sendSingle(option *sms.Options) (map[string]interface{}, error) { | ||
38 | + var ( | ||
39 | + resp *YPSmsResponse | ||
40 | + err error | ||
41 | + url = constant.YunPianSDKHost + constant.SmsSendSingle | ||
42 | + ) | ||
43 | + post := httplib.Post(url) | ||
44 | + post.Param("apikey", constant.YunPianAppKey) | ||
45 | + post.Param("mobile", option.Phone) | ||
46 | + post.Param("text", option.Content) | ||
47 | + if err = post.ToJSON(&resp); err != nil { | ||
48 | + return nil, err | ||
49 | + } | ||
50 | + err = resolveResponse(resp, err) | ||
51 | + return nil, err | ||
52 | +} | ||
53 | + | ||
54 | +func sendSingleByTpl(option *sms.Options) (map[string]interface{}, error) { | ||
55 | + var ( | ||
56 | + resp *YPSmsResponse | ||
57 | + err error | ||
58 | + url = constant.YunPianSDKHost + constant.SmsSendSingleByTpl | ||
59 | + ) | ||
60 | + post := httplib.Post(url) | ||
61 | + post.Param("apikey", constant.YunPianAppKey) | ||
62 | + post.Param("mobile", option.Phone) | ||
63 | + post.Param("tpl_id", strconv.Itoa(option.TplId)) | ||
64 | + if len(option.TplValues) > 0 { | ||
65 | + post.Param("tpl_value", tplValue(option.TplValues)) | ||
66 | + log.Debug(tplValue(option.TplValues)) | ||
67 | + } | ||
68 | + if err = post.ToJSON(&resp); err != nil { | ||
69 | + return nil, err | ||
70 | + } | ||
71 | + err = resolveResponse(resp, err) | ||
72 | + return nil, err | ||
73 | +} | ||
74 | + | ||
75 | +func resolveResponse(resp *YPSmsResponse, err error) error { | ||
76 | + if resp.Code != 0 { | ||
77 | + log.Error("【sms】 send sms code:", resp.Code, " error msg:", resp.Msg) | ||
78 | + err = common.NewErrorWithMsg(1, resp.Msg) | ||
79 | + return err | ||
80 | + } | ||
81 | + return err | ||
82 | +} | ||
83 | + | ||
84 | +func tplValue(tplValues map[string]interface{}) string { | ||
85 | + var value bytes.Buffer | ||
86 | + for k, v := range tplValues { | ||
87 | + value.WriteString(url.PathEscape(fmt.Sprintf("#%v#=%v&", k, v))) | ||
88 | + } | ||
89 | + return string(value.Bytes()[:value.Len()-1]) | ||
90 | +} |
@@ -167,3 +167,10 @@ func GetPageInfo(pageIndex, pageSize int) (offset, size int) { | @@ -167,3 +167,10 @@ func GetPageInfo(pageIndex, pageSize int) (offset, size int) { | ||
167 | size = pageSize | 167 | size = pageSize |
168 | return | 168 | return |
169 | } | 169 | } |
170 | + | ||
171 | +func IsProductEnv(env string) bool { | ||
172 | + if env == "prod" { | ||
173 | + return true | ||
174 | + } | ||
175 | + return false | ||
176 | +} |
@@ -35,6 +35,7 @@ func (this *PushController) PushInfo() { | @@ -35,6 +35,7 @@ func (this *PushController) PushInfo() { | ||
35 | if len(request.ProjectKey) == 0 || request.ProjectKey == "worth" { | 35 | if len(request.ProjectKey) == 0 || request.ProjectKey == "worth" { |
36 | request.ProjectKey = "mmm.ability.worth" //默认是价值项目 | 36 | request.ProjectKey = "mmm.ability.worth" //默认是价值项目 |
37 | } | 37 | } |
38 | + request.Type = 0 // TODO: 测试需要强制0,后期需要改回来 | ||
38 | header := controllers.GetRequestHeader(this.Ctx) | 39 | header := controllers.GetRequestHeader(this.Ctx) |
39 | msg = protocol.NewReturnResponse(push.Notification(header, request)) | 40 | msg = protocol.NewReturnResponse(push.Notification(header, request)) |
40 | } | 41 | } |
pkg/port/beego/controllers/v1/sms.go
0 → 100644
1 | +package v1 | ||
2 | + | ||
3 | +import ( | ||
4 | + "bytes" | ||
5 | + "encoding/json" | ||
6 | + "github.com/astaxie/beego/validation" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
8 | + sms "openapi/pkg/application/sms/service" | ||
9 | + "openapi/pkg/constant" | ||
10 | + protocol "openapi/pkg/domain" | ||
11 | + "openapi/pkg/infrastructure/utils" | ||
12 | + "openapi/pkg/port/beego/controllers" | ||
13 | +) | ||
14 | + | ||
15 | +type SmsController struct { | ||
16 | + controllers.BaseController | ||
17 | +} | ||
18 | + | ||
19 | +//短信验证码 SmsCode | ||
20 | +// @router /smsCode [post] | ||
21 | +func (this *SmsController) SmsCode() { | ||
22 | + var msg *protocol.ResponseMessage | ||
23 | + defer func() { | ||
24 | + this.Resp(msg) | ||
25 | + }() | ||
26 | + var request *protocol.SmsCodeRequest | ||
27 | + decoder := json.NewDecoder(bytes.NewBuffer(this.ByteBody)) | ||
28 | + decoder.UseNumber() | ||
29 | + if err := decoder.Decode(&request); err != nil { | ||
30 | + log.Error(err) | ||
31 | + msg = protocol.BadRequestParam(1) | ||
32 | + return | ||
33 | + } | ||
34 | + if utils.IsProductEnv(constant.RunMode) { | ||
35 | + valid := validation.Validation{} | ||
36 | + if !valid.Mobile(request.Phone, "mobile").Ok { | ||
37 | + msg = protocol.BadRequestParam(1001) | ||
38 | + return | ||
39 | + } | ||
40 | + } | ||
41 | + header := controllers.GetRequestHeader(this.Ctx) | ||
42 | + msg = protocol.NewReturnResponse(sms.SmsCode(header, request)) | ||
43 | +} | ||
44 | + | ||
45 | +//检查短信验证码 CheckSmsCode | ||
46 | +// @router /checkSmsCode [post] | ||
47 | +func (this *SmsController) CheckSmsCode() { | ||
48 | + var msg *protocol.ResponseMessage | ||
49 | + defer func() { | ||
50 | + this.Resp(msg) | ||
51 | + }() | ||
52 | + var request *protocol.CheckSmsCodeRequest | ||
53 | + decoder := json.NewDecoder(bytes.NewBuffer(this.ByteBody)) | ||
54 | + decoder.UseNumber() | ||
55 | + if err := decoder.Decode(&request); err != nil { | ||
56 | + log.Error(err) | ||
57 | + msg = protocol.BadRequestParam(1) | ||
58 | + return | ||
59 | + } | ||
60 | + if utils.IsProductEnv(constant.RunMode) { | ||
61 | + valid := validation.Validation{} | ||
62 | + if !valid.Mobile(request.Phone, "mobile").Ok { | ||
63 | + msg = protocol.BadRequestParam(1002) | ||
64 | + return | ||
65 | + } | ||
66 | + } | ||
67 | + header := controllers.GetRequestHeader(this.Ctx) | ||
68 | + msg = protocol.NewReturnResponse(sms.CheckSmsCode(header, request)) | ||
69 | +} | ||
70 | + | ||
71 | +//发送短信通知 sendSmsNotice | ||
72 | +// @router /sendSmsNotice [post] | ||
73 | +func (this *SmsController) SendSmsNotice() { | ||
74 | + var msg *protocol.ResponseMessage | ||
75 | + defer func() { | ||
76 | + this.Resp(msg) | ||
77 | + }() | ||
78 | + var request *protocol.SendSmsNoticeRequest | ||
79 | + decoder := json.NewDecoder(bytes.NewBuffer(this.ByteBody)) | ||
80 | + decoder.UseNumber() | ||
81 | + if err := decoder.Decode(&request); err != nil { | ||
82 | + log.Error(err) | ||
83 | + msg = protocol.BadRequestParam(1) | ||
84 | + return | ||
85 | + } | ||
86 | + if utils.IsProductEnv(constant.RunMode) { | ||
87 | + valid := validation.Validation{} | ||
88 | + if !valid.Mobile(request.Phone, "mobile").Ok { | ||
89 | + msg = protocol.BadRequestParam(1002) | ||
90 | + return | ||
91 | + } | ||
92 | + } | ||
93 | + header := controllers.GetRequestHeader(this.Ctx) | ||
94 | + msg = protocol.NewReturnResponse(sms.SendSmsNotice(header, request)) | ||
95 | +} |
@@ -100,4 +100,25 @@ func init() { | @@ -100,4 +100,25 @@ func init() { | ||
100 | AllowHTTPMethods: []string{"post"}, | 100 | AllowHTTPMethods: []string{"post"}, |
101 | MethodParams: param.Make(), | 101 | MethodParams: param.Make(), |
102 | Params: nil}) | 102 | Params: nil}) |
103 | + beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:SmsController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:SmsController"], | ||
104 | + beego.ControllerComments{ | ||
105 | + Method: "SmsCode", | ||
106 | + Router: `/smsCode`, | ||
107 | + AllowHTTPMethods: []string{"post"}, | ||
108 | + MethodParams: param.Make(), | ||
109 | + Params: nil}) | ||
110 | + beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:SmsController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:SmsController"], | ||
111 | + beego.ControllerComments{ | ||
112 | + Method: "CheckSmsCode", | ||
113 | + Router: `/checkSmsCode`, | ||
114 | + AllowHTTPMethods: []string{"post"}, | ||
115 | + MethodParams: param.Make(), | ||
116 | + Params: nil}) | ||
117 | + beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:SmsController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:SmsController"], | ||
118 | + beego.ControllerComments{ | ||
119 | + Method: "SendSmsNotice", | ||
120 | + Router: `/sendSmsNotice`, | ||
121 | + AllowHTTPMethods: []string{"post"}, | ||
122 | + MethodParams: param.Make(), | ||
123 | + Params: nil}) | ||
103 | } | 124 | } |
@@ -13,6 +13,7 @@ func init() { | @@ -13,6 +13,7 @@ func init() { | ||
13 | nsV1 := beego.NewNamespace("v1", | 13 | nsV1 := beego.NewNamespace("v1", |
14 | beego.NSNamespace("vod", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.VodController{})), | 14 | beego.NSNamespace("vod", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.VodController{})), |
15 | beego.NSNamespace("push", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.PushController{})), | 15 | beego.NSNamespace("push", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.PushController{})), |
16 | + beego.NSNamespace("sms", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.SmsController{})), | ||
16 | ) | 17 | ) |
17 | beego.SetStaticPath("/log/NIONkenfieldon", constant.LogFilePath) | 18 | beego.SetStaticPath("/log/NIONkenfieldon", constant.LogFilePath) |
18 | beego.AddNamespace(nsV1) | 19 | beego.AddNamespace(nsV1) |
-
请 注册 或 登录 后发表评论