wx_public_add.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "property-household/pb"
  12. pb_v1 "property-household/pb/v1"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func HandlePublicOpenId(openid string, unionid string) error {
  20. if unionid == "" {
  21. return nil
  22. }
  23. u := dbmodel.TUser{}
  24. where := map[string]interface{}{
  25. "union_id": unionid,
  26. }
  27. err := u.Find(database.DB(), where)
  28. if err != nil && err != gorm.ErrRecordNotFound {
  29. return errors.DataBaseError
  30. }
  31. if u.ID == 0 {
  32. return nil
  33. }
  34. uinfo := dbmodel.TUser{PublicOpenId: openid}
  35. return userChange(u.ID, uinfo)
  36. }
  37. //
  38. func UserWxPublicAdd(ctx context.Context, req *pb_v1.UserWxPublicAddRequest) (reply *pb_v1.UserWxPublicAddReply, err error) {
  39. reply = &pb_v1.UserWxPublicAddReply{}
  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. if req.OpenId == "" {
  53. return nil, errors.ParamsError
  54. }
  55. mreq := pb_v1.WxPublicUnionIdRequest{OpenId: req.OpenId}
  56. mreply, _ := pb.Thirdparty.WxPublicUnionId(ctx, &mreq)
  57. unionId := ""
  58. if mreply != nil {
  59. unionId = mreply.UnionId
  60. }
  61. now := time.Now()
  62. p := &dbmodel.TUserWxPublic{
  63. OpenId: req.OpenId,
  64. UnionId: unionId,
  65. CreatedAt: now,
  66. UpdatedAt: now,
  67. }
  68. err = p.Insert(database.DB())
  69. if err != nil {
  70. return nil, errors.DataBaseError
  71. }
  72. HandlePublicOpenId(req.OpenId, unionId)
  73. return reply, nil
  74. }
  75. func AddPublicTest() {
  76. HandlePublicOpenId("o4o5D6kM-pLBfGYTyxP9yDcqMZWk", "oF22P1kcHAvD2nqAa7Y5JIjLXz0c")
  77. }