nick_name.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-household/errors"
  10. dbmodel "property-household/model"
  11. pb_v1 "property-household/pb/v1"
  12. "git.getensh.com/common/gopkgs/database"
  13. "git.getensh.com/common/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. // 修改昵称
  18. func UserUpdateNickName(ctx context.Context, req *pb_v1.UserUpdateNickNameRequest) (reply *pb_v1.UserUpdateNickNameReply, err error) {
  19. reply = &pb_v1.UserUpdateNickNameReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.NickName == "" || req.Uid < 1 {
  33. return nil, errors.ParamsError
  34. }
  35. user := &dbmodel.TUser{}
  36. where := map[string]interface{}{
  37. "id": req.Uid,
  38. }
  39. err = user.Find(database.DB(), where)
  40. if err != nil && err != gorm.ErrRecordNotFound {
  41. return nil, errors.DataBaseError
  42. }
  43. if user.ID == 0 {
  44. return nil, errors.UserNotExist
  45. }
  46. uinfo := dbmodel.TUser{NickName: req.NickName}
  47. if err = userChange(req.Uid, uinfo); err != nil {
  48. return nil, err
  49. }
  50. return reply, nil
  51. }