作者 yangfu

短信验证码

1 syntax = "v1" 1 syntax = "v1"
2 2
  3 +import "core/common.api"
3 import "core/comment.api" 4 import "core/comment.api"
4 import "core/message.api" 5 import "core/message.api"
5 import "core/article_tag.api" 6 import "core/article_tag.api"
  1 +syntax = "v1"
  2 +
  3 +info(
  4 + title: "天联鹰蜓"
  5 + desc: "天联鹰蜓"
  6 + author: "email"
  7 + email: "email"
  8 + version: "v1"
  9 +)
  10 +
  11 +// 通用接口
  12 +@server(
  13 + prefix: v1/common
  14 + group: common
  15 +)
  16 +service Core {
  17 + @doc "短信验证码"
  18 + @handler commonSmsCode
  19 + post /sms/code (CommonSmsCodeRequest) returns (CommonSmsCodeResposne)
  20 +}
  21 +
  22 +// 短信验证码
  23 +type(
  24 + CommonSmsCodeRequest{
  25 + Phone string `json:"phone"`
  26 + }
  27 + CommonSmsCodeResposne{
  28 +
  29 + }
  30 +)
  1 +package common
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
  5 + "net/http"
  6 +
  7 + "github.com/zeromicro/go-zero/rest/httpx"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/common"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  11 +)
  12 +
  13 +func CommonSmsCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  14 + return func(w http.ResponseWriter, r *http.Request) {
  15 + var req types.CommonSmsCodeRequest
  16 + if err := httpx.Parse(r, &req); err != nil {
  17 + httpx.ErrorCtx(r.Context(), w, err)
  18 + return
  19 + }
  20 +
  21 + l := common.NewCommonSmsCodeLogic(r.Context(), svcCtx)
  22 + resp, err := l.CommonSmsCode(&req)
  23 + result.HttpResult(r, w, resp, err)
  24 + }
  25 +}
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 6
7 article "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/article" 7 article "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/article"
8 comment "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/comment" 8 comment "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/comment"
  9 + common "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/common"
9 company "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/company" 10 company "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/company"
10 department "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/department" 11 department "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/department"
11 message "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/message" 12 message "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/message"
@@ -22,6 +23,17 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -22,6 +23,17 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
22 []rest.Route{ 23 []rest.Route{
23 { 24 {
24 Method: http.MethodPost, 25 Method: http.MethodPost,
  26 + Path: "/sms/code",
  27 + Handler: common.CommonSmsCodeHandler(serverCtx),
  28 + },
  29 + },
  30 + rest.WithPrefix("/v1/common"),
  31 + )
  32 +
  33 + server.AddRoutes(
  34 + []rest.Route{
  35 + {
  36 + Method: http.MethodPost,
25 Path: "/article_comment", 37 Path: "/article_comment",
26 Handler: comment.MiniCreateArticleCommentHandler(serverCtx), 38 Handler: comment.MiniCreateArticleCommentHandler(serverCtx),
27 }, 39 },
  1 +package common
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
  7 +
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  10 +
  11 + "github.com/zeromicro/go-zero/core/logx"
  12 +)
  13 +
  14 +type CommonSmsCodeLogic struct {
  15 + logx.Logger
  16 + ctx context.Context
  17 + svcCtx *svc.ServiceContext
  18 +}
  19 +
  20 +func NewCommonSmsCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommonSmsCodeLogic {
  21 + return &CommonSmsCodeLogic{
  22 + Logger: logx.WithContext(ctx),
  23 + ctx: ctx,
  24 + svcCtx: svcCtx,
  25 + }
  26 +}
  27 +
  28 +func (l *CommonSmsCodeLogic) CommonSmsCode(req *types.CommonSmsCodeRequest) (resp *types.CommonSmsCodeResposne, err error) {
  29 + _, err = l.svcCtx.SmsService.SendSmsCode(l.ctx, smslib.RequestSendSmsCode{
  30 + Phone: req.Phone,
  31 + })
  32 + if err != nil {
  33 + return nil, xerr.NewErrMsgErr(err.Error(), err)
  34 + }
  35 + resp = &types.CommonSmsCodeResposne{}
  36 + return
  37 +}
@@ -87,15 +87,16 @@ func (l *MiniUserInfoLogic) MiniUserInfo(req *types.MiniUserInfoRequest) (resp * @@ -87,15 +87,16 @@ func (l *MiniUserInfoLogic) MiniUserInfo(req *types.MiniUserInfoRequest) (resp *
87 authSet := collection.NewSet() 87 authSet := collection.NewSet()
88 for _, role := range roles { 88 for _, role := range roles {
89 for _, auth := range role.Auths { 89 for _, auth := range role.Auths {
90 - if !authSet.Contains(auth) {  
91 - authSet.Add(auth)  
92 - if item := role.GetAuth(auth); item != nil {  
93 - resp.Auths = append(resp.Auths, types.Auth{  
94 - Id: item.Id,  
95 - Name: item.Name,  
96 - Code: item.Code,  
97 - })  
98 - } 90 + if authSet.Contains(auth) {
  91 + continue
  92 + }
  93 + authSet.Add(auth)
  94 + if item := role.GetAuth(auth); item != nil {
  95 + resp.Auths = append(resp.Auths, types.Auth{
  96 + Id: item.Id,
  97 + Name: item.Name,
  98 + Code: item.Code,
  99 + })
99 } 100 }
100 } 101 }
101 } 102 }
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 "github.com/silenceper/wechat/v2/cache" 7 "github.com/silenceper/wechat/v2/cache"
8 miniConfig "github.com/silenceper/wechat/v2/miniprogram/config" 8 miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool" 11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" 12 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
12 13
@@ -121,6 +122,9 @@ func (c WxClientLogin) PhoneSmsCodeLogin(phone string, code string) (*domain.Log @@ -121,6 +122,9 @@ func (c WxClientLogin) PhoneSmsCodeLogin(phone string, code string) (*domain.Log
121 users []*domain.User 122 users []*domain.User
122 err error 123 err error
123 ) 124 )
  125 + if _, err = c.l.svcCtx.SmsService.CheckSmsCode(c.l.ctx, smslib.RequestCheckSmsCode{Phone: phone, Code: code}); err != nil {
  126 + return nil, xerr.NewErrMsgErr(err.Error(), err)
  127 + }
124 conn := c.l.svcCtx.DefaultDBConn() 128 conn := c.l.svcCtx.DefaultDBConn()
125 _, users, err = c.l.svcCtx.UserRepository.Find(c.l.ctx, conn, domain.NewQueryOptions(). 129 _, users, err = c.l.svcCtx.UserRepository.Find(c.l.ctx, conn, domain.NewQueryOptions().
126 MustWithKV("phone", phone). 130 MustWithKV("phone", phone).
@@ -10,9 +10,11 @@ import ( @@ -10,9 +10,11 @@ import (
10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway" 11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
12 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib" 12 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
  13 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
13 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/cache" 14 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/cache"
14 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/database" 15 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/database"
15 "gorm.io/gorm" 16 "gorm.io/gorm"
  17 + "time"
16 ) 18 )
17 19
18 type ServiceContext struct { 20 type ServiceContext struct {
@@ -39,6 +41,7 @@ type ServiceContext struct { @@ -39,6 +41,7 @@ type ServiceContext struct {
39 UserRepository domain.UserRepository 41 UserRepository domain.UserRepository
40 42
41 ApiAuthService authlib.ApiAuthService 43 ApiAuthService authlib.ApiAuthService
  44 + SmsService smslib.SMSService
42 45
43 LoginStatusCheck rest.Middleware 46 LoginStatusCheck rest.Middleware
44 } 47 }
@@ -57,6 +60,7 @@ func NewServiceContext(c config.Config) *ServiceContext { @@ -57,6 +60,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
57 DB: db, 60 DB: db,
58 Redis: redis, 61 Redis: redis,
59 ApiAuthService: apiAuth, 62 ApiAuthService: apiAuth,
  63 + SmsService: smslib.SMSService{Service: gateway.NewService("短信服务", "https://sms.fjmaimaimai.com:9897", time.Second*5)},
60 LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle, 64 LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
61 65
62 ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)), 66 ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
1 // Code generated by goctl. DO NOT EDIT. 1 // Code generated by goctl. DO NOT EDIT.
2 package types 2 package types
3 3
  4 +type CommonSmsCodeRequest struct {
  5 + Phone string `json:"phone"`
  6 +}
  7 +
  8 +type CommonSmsCodeResposne struct {
  9 +}
  10 +
4 type CommentAuthor struct { 11 type CommentAuthor struct {
5 Id int64 `json:"id"` // 人员id 12 Id int64 `json:"id"` // 人员id
6 Name string `json:"name"` // 人员的名字 13 Name string `json:"name"` // 人员的名字
@@ -24,5 +24,8 @@ type HttpError struct { @@ -24,5 +24,8 @@ type HttpError struct {
24 } 24 }
25 25
26 func (e HttpError) Error() string { 26 func (e HttpError) Error() string {
  27 + if e.Base.Code > 0 && e.Base.Msg != "" {
  28 + return e.Base.Msg
  29 + }
27 return fmt.Sprintf("HttpError code:%d msg:%s", e.Base.Code, e.Base.Msg) 30 return fmt.Sprintf("HttpError code:%d msg:%s", e.Base.Code, e.Base.Msg)
28 } 31 }
  1 +package smslib
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
  6 + "net/http"
  7 +)
  8 +
  9 +type SMSService struct {
  10 + gateway.Service
  11 +}
  12 +
  13 +func (svc *SMSService) SendSmsCode(ctx context.Context, request RequestSendSmsCode) (*DataSendSmsCode, error) {
  14 + var result DataSendSmsCode
  15 + if err := svc.Do(ctx, "/service/sendSms", http.MethodPost, request, &result); err != nil {
  16 + return nil, err
  17 + }
  18 + return &result, nil
  19 +}
  20 +
  21 +func (svc *SMSService) CheckSmsCode(ctx context.Context, request RequestCheckSmsCode) (*DataCheckSmsCode, error) {
  22 + var result DataCheckSmsCode
  23 + if err := svc.Do(ctx, "/service/checkSmsCode", http.MethodPost, request, &result); err != nil {
  24 + return nil, err
  25 + }
  26 + return &result, nil
  27 +}
  1 +package smslib
  2 +
  3 +type (
  4 + RequestSendSmsCode struct {
  5 + Phone string `json:"phone"`
  6 + }
  7 + DataSendSmsCode struct {
  8 + }
  9 +)
  10 +
  11 +type (
  12 + RequestCheckSmsCode struct {
  13 + Phone string `json:"phone"`
  14 + Code string `json:"code"`
  15 + }
  16 + DataCheckSmsCode struct {
  17 + }
  18 +)