go-zero-looklook-2

思考并回答以下问题:

app

app/usercenter/cmd/api/internal/handler/routes.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Code generated by goctl. DO NOT EDIT.
package handler

import (
"net/http"

user "looklook/app/usercenter/cmd/api/internal/handler/user"
"looklook/app/usercenter/cmd/api/internal/svc"

"github.com/zeromicro/go-zero/rest"
)

func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/user/register",
Handler: user.RegisterHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/user/login",
Handler: user.LoginHandler(serverCtx),
},
},
rest.WithPrefix("/usercenter/v1"),
)

server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/user/detail",
Handler: user.DetailHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/user/wxMiniAuth",
Handler: user.WxMiniAuthHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.JwtAuth.AccessSecret),
rest.WithPrefix("/usercenter/v1"),
)
}

app/usercenter/cmd/api/internal/handler/user/registerHandler.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package user

import (
"net/http"

"looklook/app/usercenter/cmd/api/internal/logic/user"
"looklook/app/usercenter/cmd/api/internal/svc"
"looklook/app/usercenter/cmd/api/internal/types"
"looklook/common/result"

"github.com/zeromicro/go-zero/rest/httpx"
)

func RegisterHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RegisterReq
if err := httpx.Parse(r, &req); err != nil {
result.ParamErrorResult(r, w, err)
return
}

l := user.NewRegisterLogic(r.Context(), ctx)
resp, err := l.Register(req)
result.HttpResult(r, w, resp, err)
}
}

app/usercenter/cmd/api/internal/types/types.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Code generated by goctl. DO NOT EDIT.
package types

type User struct {
Id int64 `json:"id"`
Mobile string `json:"mobile"`
Nickname string `json:"nickname"`
Sex int64 `json:"sex"`
Avatar string `json:"avatar"`
Info string `json:"info"`
}

type RegisterReq struct {
Mobile string `json:"mobile"`
Password string `json:"password"`
}

type RegisterResp struct {
AccessToken string `json:"accessToken"`
AccessExpire int64 `json:"accessExpire"`
RefreshAfter int64 `json:"refreshAfter"`
}

type LoginReq struct {
Mobile string `json:"mobile"`
Password string `json:"password"`
}

type LoginResp struct {
AccessToken string `json:"accessToken"`
AccessExpire int64 `json:"accessExpire"`
RefreshAfter int64 `json:"refreshAfter"`
}

type WXMiniAuthReq struct {
Code string `json:"code"`
IV string `json:"iv"`
EncryptedData string `json:"encryptedData"`
}

type WXMiniAuthResp struct {
AccessToken string `json:"accessToken"`
AccessExpire int64 `json:"accessExpire"`
RefreshAfter int64 `json:"refreshAfter"`
}

type UserInfoReq struct {
}

type UserInfoResp struct {
UserInfo User `json:"userInfo"`
}

app/usercenter/cmd/api/internal/logic/user/registerLogic.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package user

import (
"context"
"looklook/app/usercenter/cmd/api/internal/svc"
"looklook/app/usercenter/cmd/api/internal/types"
"looklook/app/usercenter/cmd/rpc/usercenter"
"looklook/app/usercenter/model"

"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) RegisterLogic {
return RegisterLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *RegisterLogic) Register(req types.RegisterReq) (*types.RegisterResp, error) {

registerResp, err := l.svcCtx.UsercenterRpc.Register(l.ctx, &usercenter.RegisterReq{
Mobile: req.Mobile,
Password: req.Password,
AuthKey: req.Mobile,
AuthType: model.UserAuthTypeSystem,
})
if err != nil {
return nil, errors.Wrapf(err, "req: %+v", req)
}

var resp types.RegisterResp
_ = copier.Copy(&resp, registerResp)

return &resp, nil
}

0%