email_prepare.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package user
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "github.com/jinzhu/gorm"
  9. "go.uber.org/zap"
  10. "smart-site-management/errors"
  11. dbmodel "smart-site-management/model"
  12. pb_v1 "smart-site-management/pb/v1"
  13. )
  14. func EmailPrepare(ctx context.Context, req *pb_v1.EmailPrepareRequest)(reply *pb_v1.EmailPrepareReply, err error) {
  15. p := &dbmodel.TSiteUser{}
  16. if req.Password == "" || req.Email == "" {
  17. return nil, errors.ParamsError
  18. }
  19. where := map[string]interface{}{
  20. "id":req.Id,
  21. }
  22. // 状态判断
  23. err = p.Find(database.DB(), where, nil)
  24. if err != nil {
  25. logger.Error("EmailPrepare",
  26. zap.String("err", err.Error()))
  27. return nil, errors.DataBaseError
  28. }
  29. if p.Id == 0 {
  30. return nil, errors.NoRecordError
  31. }
  32. pass, _ := getB64Pass(req.Password)
  33. if p.Passwd != pass {
  34. return nil, errors.UserPassError
  35. }
  36. p = &dbmodel.TSiteUser{}
  37. where = map[string]interface{}{
  38. "email":req.Email,
  39. }
  40. // 状态判断
  41. err = p.Find(database.DB(), where, nil)
  42. if err != nil && err != gorm.ErrRecordNotFound{
  43. logger.Error("EmailPrepare",
  44. zap.String("err", err.Error()))
  45. return nil, errors.DataBaseError
  46. }
  47. if p.Id > 0 {
  48. return nil, errors.EmailExist
  49. }
  50. reply = &pb_v1.EmailPrepareReply{}
  51. return reply, nil
  52. }