reset_password.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "property-company/pb"
  18. pb_v1 "property-company/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. //
  38. func ResetPassword(ctx context.Context, req *pb_v1.ResetPasswordRequest) (reply *pb_v1.ResetPasswordReply, err error) {
  39. reply = &pb_v1.ResetPasswordReply{}
  40. // 捕获各个task中的异常并返回给调用者
  41. defer func() {
  42. if r := recover(); r != nil {
  43. err = fmt.Errorf("%+v", r)
  44. e := &status.Status{}
  45. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  46. logger.Error("err",
  47. zap.String("system_err", err.Error()),
  48. zap.Stack("stacktrace"))
  49. }
  50. }
  51. }()
  52. err = checkResetPasswordParam(req)
  53. if err != nil {
  54. return nil, err
  55. }
  56. user := &dbmodel.TUser{}
  57. where := map[string]interface{}{
  58. "phone": req.Phone,
  59. }
  60. err = user.Find(database.DB(), where)
  61. if err != nil && err != gorm.ErrRecordNotFound {
  62. return nil, errors.DataBaseError
  63. }
  64. if user.ID == 0 {
  65. return nil, errors.ErrRecordNotFound
  66. }
  67. company := &dbmodel.TCompany{}
  68. where = map[string]interface{}{
  69. "id":user.Cid,
  70. }
  71. err = company.Find(database.DB(), where)
  72. if err != nil && err != gorm.ErrRecordNotFound{
  73. return nil, errors.DataBaseError
  74. }
  75. if company.ID == 0 {
  76. return reply, errors.ErrRecordNotFound
  77. }
  78. if company.ApproveStatus != consts.ApproveStatusSuccess {
  79. return nil, errors.UserStatusError
  80. }
  81. cid := user.Cid
  82. oldUser := user.User
  83. if err = checkVcode(req.Phone, req.Vcode); err != nil {
  84. return nil, err
  85. }
  86. p := &dbmodel.TUser{}
  87. where = map[string]interface{}{
  88. "id": user.ID,
  89. }
  90. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  91. now := time.Now()
  92. values := map[string]interface{}{
  93. "password": passwd,
  94. "updated_at": now,
  95. }
  96. db := database.DB().Begin()
  97. err = p.Update(db, where, values)
  98. if err != nil {
  99. db.Rollback()
  100. return nil, errors.DataBaseError
  101. }
  102. company = &dbmodel.TCompany{}
  103. where = map[string]interface{}{
  104. "id":cid,
  105. "user":oldUser,
  106. "approve_status":consts.ApproveStatusSuccess,
  107. }
  108. err = company.Update(db, where, values)
  109. if err != nil {
  110. db.Rollback()
  111. return nil, errors.DataBaseError
  112. }
  113. db.Commit()
  114. return reply, nil
  115. }