change_password.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "git.getensh.com/common/gopkgs/util"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. "property-company/consts"
  15. "property-company/errors"
  16. dbmodel "property-company/model"
  17. pb_v1 "property-company/pb/v1"
  18. "time"
  19. )
  20. func checkChangePasswordParam(req *pb_v1.ChangePasswordRequest) error {
  21. switch {
  22. case req.Uid == 0:
  23. return status.Error(10003, "账号不能为空")
  24. case req.OldPassword == "":
  25. return status.Error(10003, "原密码不能为空")
  26. case req.NewPassword == "":
  27. return status.Error(10003, "密码不能为空")
  28. }
  29. return nil
  30. }
  31. //
  32. func ChangePassword(ctx context.Context, req *pb_v1.ChangePasswordRequest) (reply *pb_v1.ChangePasswordReply, err error) {
  33. reply = &pb_v1.ChangePasswordReply{}
  34. // 捕获各个task中的异常并返回给调用者
  35. defer func() {
  36. if r := recover(); r != nil {
  37. err = fmt.Errorf("%+v", r)
  38. e := &status.Status{}
  39. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  40. logger.Error("err",
  41. zap.String("system_err", err.Error()),
  42. zap.Stack("stacktrace"))
  43. }
  44. }
  45. }()
  46. err = checkChangePasswordParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. user := &dbmodel.TUser{}
  51. where := map[string]interface{}{
  52. "id":req.Uid,
  53. }
  54. err = user.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound{
  56. return nil, errors.DataBaseError
  57. }
  58. if user.ID == 0 {
  59. return nil, errors.UserNotExist
  60. }
  61. cid := user.Cid
  62. oldUser := user.User
  63. oldpasswd, _ := util.GetMd5Pass(req.OldPassword, consts.CRYPTO_KEY)
  64. if oldpasswd != user.Password {
  65. return nil, errors.PasswordError
  66. }
  67. passwd, _ := util.GetMd5Pass(req.NewPassword, consts.CRYPTO_KEY)
  68. now := time.Now()
  69. values := map[string]interface{}{
  70. "password":passwd,
  71. "updated_at":now,
  72. }
  73. db := database.DB().Begin()
  74. err = user.Update(db, where, values)
  75. if err != nil {
  76. db.Rollback()
  77. return nil, errors.DataBaseError
  78. }
  79. company := &dbmodel.TCompany{}
  80. where = map[string]interface{}{
  81. "id":cid,
  82. "user":oldUser,
  83. "approve_status":consts.ApproveStatusSuccess,
  84. }
  85. err = company.Update(db, where, values)
  86. if err != nil {
  87. db.Rollback()
  88. return nil, errors.DataBaseError
  89. }
  90. db.Commit()
  91. return reply, nil
  92. }