reset_password.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-system/consts"
  15. "property-system/errors"
  16. dbmodel "property-system/model"
  17. "property-system/pb"
  18. pb_v1 "property-system/pb/v1"
  19. "time"
  20. )
  21. func checkResetPasswordParam(req *pb_v1.ResetPasswordRequest) error {
  22. switch {
  23. case req.Phone == "":
  24. return status.Error(10003, "联系人电话不能为空")
  25. case req.Vcode < 1:
  26. return status.Error(10003, "短信验证码不能为空")
  27. case req.Password == "":
  28. return status.Error(10003, "密码不能为空")
  29. }
  30. return nil
  31. }
  32. func checkVcode(phone string, vcode uint32) error {
  33. mreq := &pb_v1.CheckVcodeRequest{Vcode:vcode, PhoneNumber:phone}
  34. _, err := pb.Thirdparty.CheckVcode(context.Background(), mreq)
  35. return err
  36. }
  37. func resetPasswordWithUid(req *pb_v1.ResetPasswordRequest) (reply *pb_v1.ResetPasswordReply, err error) {
  38. reply = &pb_v1.ResetPasswordReply{}
  39. user := &dbmodel.TUser{}
  40. where := map[string]interface{}{
  41. "id":req.Uid,
  42. "phone":req.Phone,
  43. }
  44. err = user.Find(database.DB(), where)
  45. if err != nil && err != gorm.ErrRecordNotFound {
  46. return nil, errors.DataBaseError
  47. }
  48. if user.ID == 0 {
  49. return nil, errors.UserNotExist
  50. }
  51. if err = checkVcode(req.Phone, req.Vcode); err != nil {
  52. return nil, err
  53. }
  54. item := &pb_v1.ResetPasswordData{
  55. Uid:req.Uid,
  56. Username:user.User,
  57. GardenName:user.GardenName,
  58. }
  59. reply.List = make([]*pb_v1.ResetPasswordData, 1)
  60. reply.List[0] = item
  61. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  62. now := time.Now()
  63. values := map[string]interface{}{
  64. "password":passwd,
  65. "updated_at":now,
  66. }
  67. err = user.Update(database.DB(), where, values)
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. return reply, nil
  72. }
  73. //
  74. func ResetPassword(ctx context.Context, req *pb_v1.ResetPasswordRequest) (reply *pb_v1.ResetPasswordReply, err error) {
  75. reply = &pb_v1.ResetPasswordReply{}
  76. // 捕获各个task中的异常并返回给调用者
  77. defer func() {
  78. if r := recover(); r != nil {
  79. err = fmt.Errorf("%+v", r)
  80. e := &status.Status{}
  81. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  82. logger.Error("err",
  83. zap.String("system_err", err.Error()),
  84. zap.Stack("stacktrace"))
  85. }
  86. }
  87. }()
  88. err = checkResetPasswordParam(req)
  89. if err != nil {
  90. return nil, err
  91. }
  92. if req.Uid > 0 {
  93. return resetPasswordWithUid(req)
  94. }
  95. user := &dbmodel.TUser{}
  96. where := map[string]interface{}{
  97. "phone":req.Phone,
  98. }
  99. list, err := user.List(database.DB(), where, nil, -1, -1)
  100. if err != nil {
  101. return nil, errors.DataBaseError
  102. }
  103. if len(list) == 0 {
  104. return nil, errors.ErrRecordNotFound
  105. }
  106. if err = checkVcode(req.Phone, req.Vcode); err != nil {
  107. return nil, err
  108. }
  109. reply.List = make([]*pb_v1.ResetPasswordData, len(list))
  110. for i, v := range list {
  111. item := &pb_v1.ResetPasswordData{
  112. Uid:v.ID,
  113. Username:v.User,
  114. GardenName:v.GardenName,
  115. }
  116. reply.List[i] = item
  117. }
  118. if len(list) == 1 {
  119. user := &dbmodel.TUser{}
  120. where := map[string]interface{}{
  121. "id":list[0].ID,
  122. }
  123. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  124. now := time.Now()
  125. values := map[string]interface{}{
  126. "password":passwd,
  127. "updated_at":now,
  128. }
  129. err = user.Update(database.DB(), where, values)
  130. if err != nil {
  131. return nil, errors.DataBaseError
  132. }
  133. }
  134. return reply, nil
  135. }