作者 yangfu

短信验证码

syntax = "v1"
import "core/common.api"
import "core/comment.api"
import "core/message.api"
import "core/article_tag.api"
... ...
syntax = "v1"
info(
title: "天联鹰蜓"
desc: "天联鹰蜓"
author: "email"
email: "email"
version: "v1"
)
// 通用接口
@server(
prefix: v1/common
group: common
)
service Core {
@doc "短信验证码"
@handler commonSmsCode
post /sms/code (CommonSmsCodeRequest) returns (CommonSmsCodeResposne)
}
// 短信验证码
type(
CommonSmsCodeRequest{
Phone string `json:"phone"`
}
CommonSmsCodeResposne{
}
)
\ No newline at end of file
... ...
package common
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/common"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func CommonSmsCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.CommonSmsCodeRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := common.NewCommonSmsCodeLogic(r.Context(), svcCtx)
resp, err := l.CommonSmsCode(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
... ... @@ -6,6 +6,7 @@ import (
article "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/article"
comment "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/comment"
common "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/common"
company "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/company"
department "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/department"
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) {
[]rest.Route{
{
Method: http.MethodPost,
Path: "/sms/code",
Handler: common.CommonSmsCodeHandler(serverCtx),
},
},
rest.WithPrefix("/v1/common"),
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/article_comment",
Handler: comment.MiniCreateArticleCommentHandler(serverCtx),
},
... ...
package common
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CommonSmsCodeLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCommonSmsCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommonSmsCodeLogic {
return &CommonSmsCodeLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CommonSmsCodeLogic) CommonSmsCode(req *types.CommonSmsCodeRequest) (resp *types.CommonSmsCodeResposne, err error) {
_, err = l.svcCtx.SmsService.SendSmsCode(l.ctx, smslib.RequestSendSmsCode{
Phone: req.Phone,
})
if err != nil {
return nil, xerr.NewErrMsgErr(err.Error(), err)
}
resp = &types.CommonSmsCodeResposne{}
return
}
... ...
... ... @@ -87,7 +87,9 @@ func (l *MiniUserInfoLogic) MiniUserInfo(req *types.MiniUserInfoRequest) (resp *
authSet := collection.NewSet()
for _, role := range roles {
for _, auth := range role.Auths {
if !authSet.Contains(auth) {
if authSet.Contains(auth) {
continue
}
authSet.Add(auth)
if item := role.GetAuth(auth); item != nil {
resp.Auths = append(resp.Auths, types.Auth{
... ... @@ -98,6 +100,5 @@ func (l *MiniUserInfoLogic) MiniUserInfo(req *types.MiniUserInfoRequest) (resp *
}
}
}
}
return
}
... ...
... ... @@ -7,6 +7,7 @@ import (
"github.com/silenceper/wechat/v2/cache"
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
... ... @@ -121,6 +122,9 @@ func (c WxClientLogin) PhoneSmsCodeLogin(phone string, code string) (*domain.Log
users []*domain.User
err error
)
if _, err = c.l.svcCtx.SmsService.CheckSmsCode(c.l.ctx, smslib.RequestCheckSmsCode{Phone: phone, Code: code}); err != nil {
return nil, xerr.NewErrMsgErr(err.Error(), err)
}
conn := c.l.svcCtx.DefaultDBConn()
_, users, err = c.l.svcCtx.UserRepository.Find(c.l.ctx, conn, domain.NewQueryOptions().
MustWithKV("phone", phone).
... ...
... ... @@ -10,9 +10,11 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/database"
"gorm.io/gorm"
"time"
)
type ServiceContext struct {
... ... @@ -39,6 +41,7 @@ type ServiceContext struct {
UserRepository domain.UserRepository
ApiAuthService authlib.ApiAuthService
SmsService smslib.SMSService
LoginStatusCheck rest.Middleware
}
... ... @@ -57,6 +60,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
DB: db,
Redis: redis,
ApiAuthService: apiAuth,
SmsService: smslib.SMSService{Service: gateway.NewService("短信服务", "https://sms.fjmaimaimai.com:9897", time.Second*5)},
LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
... ...
// Code generated by goctl. DO NOT EDIT.
package types
type CommonSmsCodeRequest struct {
Phone string `json:"phone"`
}
type CommonSmsCodeResposne struct {
}
type CommentAuthor struct {
Id int64 `json:"id"` // 人员id
Name string `json:"name"` // 人员的名字
... ...
... ... @@ -24,5 +24,8 @@ type HttpError struct {
}
func (e HttpError) Error() string {
if e.Base.Code > 0 && e.Base.Msg != "" {
return e.Base.Msg
}
return fmt.Sprintf("HttpError code:%d msg:%s", e.Base.Code, e.Base.Msg)
}
... ...
package smslib
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
"net/http"
)
type SMSService struct {
gateway.Service
}
func (svc *SMSService) SendSmsCode(ctx context.Context, request RequestSendSmsCode) (*DataSendSmsCode, error) {
var result DataSendSmsCode
if err := svc.Do(ctx, "/service/sendSms", http.MethodPost, request, &result); err != nil {
return nil, err
}
return &result, nil
}
func (svc *SMSService) CheckSmsCode(ctx context.Context, request RequestCheckSmsCode) (*DataCheckSmsCode, error) {
var result DataCheckSmsCode
if err := svc.Do(ctx, "/service/checkSmsCode", http.MethodPost, request, &result); err != nil {
return nil, err
}
return &result, nil
}
... ...
package smslib
type (
RequestSendSmsCode struct {
Phone string `json:"phone"`
}
DataSendSmsCode struct {
}
)
type (
RequestCheckSmsCode struct {
Phone string `json:"phone"`
Code string `json:"code"`
}
DataCheckSmsCode struct {
}
)
... ...