正在回答 回答被采纳积分+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 |
相似问题
登录后可查看更多问答,登录/注册
Go开发工程师全新版
- 参与学习 508 人
- 解答问题 589 个
风口上的技术,薪资水平遥遥领先,现在学习正值红利期! 未来3-5年,Go语言势必成为企业高性能项目中不可替代的语言 从基础到项目实战再到重构,对转行人员友好,真正从入门到精通!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧