real_name_auth.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "property-household/utils"
  13. "git.getensh.com/common/gopkgs/database"
  14. "git.getensh.com/common/gopkgs/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. const (
  19. HouseholdApproveStatusWait = 1
  20. HouseholdApproveStatusSuccess = 2
  21. HouseholdApproveStatusFail = 3
  22. )
  23. // 实名认证
  24. func RealNameAuth(ctx context.Context, req *pb_v1.RealNameAuthRequest) (reply *pb_v1.RealNameAuthReply, err error) {
  25. reply = &pb_v1.RealNameAuthReply{}
  26. // 捕获各个task中的异常并返回给调用者
  27. defer func() {
  28. if r := recover(); r != nil {
  29. err = fmt.Errorf("%+v", r)
  30. e := &status.Status{}
  31. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  32. logger.Error("err",
  33. zap.String("system_err", err.Error()),
  34. zap.Stack("stacktrace"))
  35. }
  36. }
  37. }()
  38. if req.IdNumber == "" || req.IdType < 1 || req.Uid < 1 || req.RealName == "" {
  39. return nil, errors.ParamsError
  40. }
  41. if req.IdType == 1 && !utils.CheckIDCert(req.IdNumber) {
  42. return nil, status.Error(10003, "身份证格式错误")
  43. }
  44. user := &dbmodel.TUser{}
  45. where := map[string]interface{}{
  46. "id": req.Uid,
  47. }
  48. err = user.Find(database.DB(), where)
  49. if err != nil && err != gorm.ErrRecordNotFound {
  50. return nil, errors.DataBaseError
  51. }
  52. if user.ID == 0 {
  53. return nil, errors.UserNotExist
  54. }
  55. idNumber := ""
  56. if req.IdNumber != "" {
  57. idNumber = utils.CertEncrypt(req.IdNumber)
  58. }
  59. uinfo := dbmodel.TUser{IdType: req.IdType, IdNumber: idNumber, RealName: req.RealName}
  60. if err = userChange(req.Uid, uinfo); err != nil {
  61. return nil, err
  62. }
  63. return reply, nil
  64. }