格式怎么获取?

格式怎么获取?

https://img1.sycdn.imooc.com/climg/654a03bd09414b6f15981044.jpg

在实际开发过程中,我要怎么获取这个传输的格式呢?我要通过什么方法查看这个格式?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
提问者 沉默寡言黄少天丶 2023-12-07 18:00:52


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package handler
 
import (
    "context"
    "crypto/md5"
    "fmt"
    "github.com/anaskhan96/go-password-encoder"
    "github.com/golang/protobuf/ptypes/empty"
    "godemo/3/user_srv/global"
    "godemo/3/user_srv/model"
    "godemo/3/user_srv/proto"
    "google.golang.org/grpc/codes"
    "google.golang.org/grpc/status"
    "gorm.io/gorm"
    "strings"
    "time"
)
 
type UserServer struct {
}
 
func ModelToResponse(user model.User) *proto.UserInfoResponse {
    // 在grpc的messages中字段有默认值,不能随便赋值nil进去
    UserInfoRsp := &proto.UserInfoResponse{
       Id:       user.ID,
       Mobile:   user.Mobile,
       Password: user.Password,
       NickName: user.NickName,
       Gender:   user.Gender,
       Role:     int32(user.Role),
    }
    if user.Birthday != nil {
       UserInfoRsp.Birthday = uint64(user.Birthday.Unix())
    }
    return UserInfoRsp
}
 
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
    return func(db *gorm.DB) *gorm.DB {
       if page <= 0 {
          page = 1
       }
 
       switch {
       case pageSize > 100:
          pageSize = 100
       case pageSize <= 0:
          pageSize = 10
       }
 
       offset := (page - 1) * pageSize
       return db.Offset(offset).Limit(pageSize)
    }
}
 
func (s *UserServer) GetUserList(ctx context.Context, req *proto.PageInfo) (*proto.UserListResponse, error) {
    // 获取用户列表
    var users []model.User
    result := global.DB.Find(&users)
    if result.Error != nil {
       return nil, result.Error
    }
    rsp := &proto.UserListResponse{}
    rsp.Total = int32(result.RowsAffected)
 
    global.DB.Scopes(Paginate(int(req.Page), int(req.PageSize))).Find(&users)
 
    for _, user := range users {
       userInfoRsp := ModelToResponse(user)
       rsp.Data = append(rsp.Data, userInfoRsp)
    }
    return rsp, nil
}
 
func (s *UserServer) GetUserByMobile(ctx context.Context, req *proto.MobileRequest) (*proto.UserInfoResponse, error) {
    // 根据手机号获取用户信息
    var user model.User
    result := global.DB.Where(&model.User{Mobile: req.Mobile}).First(&user)
    if result.RowsAffected == 0 {
       return nil, status.Errorf(codes.NotFound, "用户不存在")
    }
    if result.Error != nil {
       return nil, result.Error
    }
    userInfoRsp := ModelToResponse(user)
    return userInfoRsp, nil
}
 
func (s *UserServer) GetUserById(ctx context.Context, req *proto.IdRequest) (*proto.UserInfoResponse, error) {
    // 根据用户ID获取用户信息
    var user model.User
    result := global.DB.First(&user, req.Id)
    if result.RowsAffected == 0 {
       return nil, status.Errorf(codes.NotFound, "用户不存在")
    }
    if result.Error != nil {
       return nil, result.Error
    }
 
    userInfoRsp := ModelToResponse(user)
    return userInfoRsp, nil
}
 
func (s *UserServer) CreateUser(ctx context.Context, req *proto.CreateUserInfo) (*proto.UserInfoResponse, error) {
    // 创建用户
    var user model.User
    result := global.DB.Where(&model.User{Mobile: req.Mobile}).First(&user)
    if result.RowsAffected == 1 {
       return nil, status.Errorf(codes.AlreadyExists, "用户已存在")
    }
    user.Mobile = req.Mobile
    user.NickName = req.NickName
 
    options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: md5.New}
    salt, encodedPwd := password.Encode("generic password", options)
    user.Password = fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)
    result = global.DB.Create(&user)
    if result.Error != nil {
       return nil, status.Errorf(codes.Internal, result.Error.Error())
    }
    return ModelToResponse(user), nil
}
 
func (s *UserServer) UpdateUser(ctx context.Context, req *proto.UpdateUserInfo) (*empty.Empty, error) {
    // 更新用户
    var user model.User
    result := global.DB.First(&user, req.Id)
    if result.RowsAffected == 0 {
       return nil, status.Errorf(codes.NotFound, "用户不存在")
    }
 
    birthday := time.Unix(int64(req.Birthday), 0)
 
    user.NickName = req.NickName
    user.Gender = req.Gender
    user.Birthday = &birthday
 
    result = global.DB.Save(&user)
    if result.Error != nil {
       return nil, status.Errorf(codes.Internal, result.Error.Error())
    }
 
    return &empty.Empty{}, nil
}
 
func (s *UserServer) CheckPassWord(ctx context.Context, req *proto.PassWordCheckInfo) (*proto.CheckResponse, error) {
    // 校验密码
    options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: md5.New}
    passwordInfo := strings.Split(req.EncryptedPassword, "$")
    check := password.Verify(req.Password, passwordInfo[2], passwordInfo[3], options)
    return &proto.CheckResponse{Success: check}, nil
}


提问者 沉默寡言黄少天丶 2023-12-07 17:58:34
(
    )

(err , c *.) {
    err != {
       e, ok := .(err); ok {
          e.() {
          .:
             c.(., .{
                : e.(),
             })
          .:
             c.(., .{
                : ,
             })
          .:
             c.(., .{
                : ,
             })
          .:
             c.(., .{
                : ,
             })
          :
             c.(., .{
                : ,
             })
          }
          }
    }
}

(ctx *., err ) {
    errs, ok := err.(.)
    !ok {
       ctx.(., .{
          : err.(),
       })
    }
    ctx.(., .{
       : .(errs.(.Trans)),
    })
    }

(ctx *.) {
    opts [].opts = (opts, .(.()))
    userConn, err := .(.(,
       .ServerConfig.UserServerConfig.Host, .ServerConfig.UserServerConfig.Port), opts...)
    err != {
       .().(
          ,
          , err.(),
       )
    }
    userSrvClient := .(userConn)

    page := ctx.(, )
    pageInt, _ := .(page)
    pageSize := ctx.(, )
    pageSizeInt, _ := .(pageSize)
    rsp, err := userSrvClient.(.(), &.{
       Page:     (pageInt),
       PageSize: (pageSizeInt),
    })
    err != {
       .().(
          ,
       )
       (err, ctx)
       }
    .().(
       ,
       , rsp,
    )
    result := ([]{}, )
    _, value := rsp.Data {
       user := .{
          Id:       value.Id,
          Mobile:   value.Mobile,
          Nickname: value.NickName,
          Gender:   value.Gender,
          Birthday: .(.((value.Birthday), )),
       }
       result = (result, user)
    }
    ctx.(., result)
}

(ctx *.) {
    passwordLogin := .{}
    err := ctx.(&passwordLogin); err != {
       (ctx, err)
       }

    opts [].opts = (opts, .(.()))
    userConn, err := .(.(,
       .ServerConfig.UserServerConfig.Host, .ServerConfig.UserServerConfig.Port), opts...)
    err != {
       .().(
          ,
          , err.(),
       )
    }
    userSrvClient := .(userConn)

    userRsp, err := userSrvClient.(.(), &.{
       Mobile: passwordLogin.Mobile,
    })
    err != {
       .().(
          ,
       )
       e, ok := .(err); ok {
          e.() {
          .:
             ctx.(., .{
                : ,
             })
          :
             ctx.(., .{
                : ,
             })
          }
       }
    }
    passwordRsp, err := userSrvClient.(.(), &.{
       Password:          passwordLogin.Password,
       EncryptedPassword: userRsp.Password,
    })
    passwordRsp.Success != {
       .().(
          ,
       )
       ctx.(., .{
          : ,
       })
       }
    ctx.(., .{
       :     ,
       : passwordRsp.Success,
    })
}
1
 


  • 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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    package handler
     
    import (
        "context"
        "crypto/md5"
        "fmt"
        "github.com/anaskhan96/go-password-encoder"
        "github.com/golang/protobuf/ptypes/empty"
        "godemo/3/user_srv/global"
        "godemo/3/user_srv/model"
        "godemo/3/user_srv/proto"
        "google.golang.org/grpc/codes"
        "google.golang.org/grpc/status"
        "gorm.io/gorm"
        "strings"
        "time"
    )
     
    type UserServer struct {
    }
     
    func ModelToResponse(user model.User) *proto.UserInfoResponse {
        // 在grpc的messages中字段有默认值,不能随便赋值nil进去
        UserInfoRsp := &proto.UserInfoResponse{
           Id:       user.ID,
           Mobile:   user.Mobile,
           Password: user.Password,
           NickName: user.NickName,
           Gender:   user.Gender,
           Role:     int32(user.Role),
        }
        if user.Birthday != nil {
           UserInfoRsp.Birthday = uint64(user.Birthday.Unix())
        }
        return UserInfoRsp
    }
     
    func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
        return func(db *gorm.DB) *gorm.DB {
           if page <= 0 {
              page = 1
           }
     
           switch {
           case pageSize > 100:
              pageSize = 100
           case pageSize <= 0:
              pageSize = 10
           }
     
           offset := (page - 1) * pageSize
           return db.Offset(offset).Limit(pageSize)
        }
    }
     
    func (s *UserServer) GetUserList(ctx context.Context, req *proto.PageInfo) (*proto.UserListResponse, error) {
        // 获取用户列表
        var users []model.User
        result := global.DB.Find(&users)
        if result.Error != nil {
           return nil, result.Error
        }
        rsp := &proto.UserListResponse{}
        rsp.Total = int32(result.RowsAffected)
     
        global.DB.Scopes(Paginate(int(req.Page), int(req.PageSize))).Find(&users)
     
        for _, user := range users {
           userInfoRsp := ModelToResponse(user)
           rsp.Data = append(rsp.Data, userInfoRsp)
        }
        return rsp, nil
    }
     
    func (s *UserServer) GetUserByMobile(ctx context.Context, req *proto.MobileRequest) (*proto.UserInfoResponse, error) {
        // 根据手机号获取用户信息
        var user model.User
        result := global.DB.Where(&model.User{Mobile: req.Mobile}).First(&user)
        if result.RowsAffected == 0 {
           return nil, status.Errorf(codes.NotFound, "用户不存在")
        }
        if result.Error != nil {
           return nil, result.Error
        }
        userInfoRsp := ModelToResponse(user)
        return userInfoRsp, nil
    }
     
    func (s *UserServer) GetUserById(ctx context.Context, req *proto.IdRequest) (*proto.UserInfoResponse, error) {
        // 根据用户ID获取用户信息
        var user model.User
        result := global.DB.First(&user, req.Id)
        if result.RowsAffected == 0 {
           return nil, status.Errorf(codes.NotFound, "用户不存在")
        }
        if result.Error != nil {
           return nil, result.Error
        }
     
        userInfoRsp := ModelToResponse(user)
        return userInfoRsp, nil
    }
     
    func (s *UserServer) CreateUser(ctx context.Context, req *proto.CreateUserInfo) (*proto.UserInfoResponse, error) {
        // 创建用户
        var user model.User
        result := global.DB.Where(&model.User{Mobile: req.Mobile}).First(&user)
        if result.RowsAffected == 1 {
           return nil, status.Errorf(codes.AlreadyExists, "用户已存在")
        }
        user.Mobile = req.Mobile
        user.NickName = req.NickName
     
        options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: md5.New}
        salt, encodedPwd := password.Encode("generic password", options)
        user.Password = fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)
        result = global.DB.Create(&user)
        if result.Error != nil {
           return nil, status.Errorf(codes.Internal, result.Error.Error())
        }
        return ModelToResponse(user), nil
    }
     
    func (s *UserServer) UpdateUser(ctx context.Context, req *proto.UpdateUserInfo) (*empty.Empty, error) {
        // 更新用户
        var user model.User
        result := global.DB.First(&user, req.Id)
        if result.RowsAffected == 0 {
           return nil, status.Errorf(codes.NotFound, "用户不存在")
        }
     
        birthday := time.Unix(int64(req.Birthday), 0)
     
        user.NickName = req.NickName
        user.Gender = req.Gender
        user.Birthday = &birthday
     
        result = global.DB.Save(&user)
        if result.Error != nil {
           return nil, status.Errorf(codes.Internal, result.Error.Error())
        }
     
        return &empty.Empty{}, nil
    }
     
    func (s *UserServer) CheckPassWord(ctx context.Context, req *proto.PassWordCheckInfo) (*proto.CheckResponse, error) {
        // 校验密码
        options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: md5.New}
        passwordInfo := strings.Split(req.EncryptedPassword, "$")
        check := password.Verify(req.Password, passwordInfo[2], passwordInfo[3], options)
        return &proto.CheckResponse{Success: check}, nil
    }


    2023-12-07 18:01:38
bobby 2023-11-10 13:59:35

很多工具都可以查看啊 notepad++ https://img1.sycdn.imooc.com/climg/654dc6c409c5d3f508730713.jpg, 再安装一个插件 https://img1.sycdn.imooc.com/climg/654dc6e909c8515a10720321.jpg 这里这个插件是要自己通过notepad++安装的,你也可以goland新建一个py源码文件然后拷贝进去也可以查看 

  • 我表述的有点问题。


    我想问的是,怎么知道传参的字段名和格式的呢?

    method

    params

    id

    这三个是固定写法吗?

    2023-11-10 19:02:17
  • bobby 回复 提问者 沉默寡言黄少天丶 #2

    是的,就是固定的json写法

    2023-11-13 17:16:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Go开发工程师全新版
  • 参与学习       508    人
  • 解答问题       589    个

风口上的技术,薪资水平遥遥领先,现在学习正值红利期! 未来3-5年,Go语言势必成为企业高性能项目中不可替代的语言 从基础到项目实战再到重构,对转行人员友好,真正从入门到精通!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码