wx_empty_union_id.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/cache"
  9. "git.getensh.com/common/gopkgs/database"
  10. "git.getensh.com/common/gopkgs/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "property-household/errors"
  14. dbmodel "property-household/model"
  15. "property-household/pb"
  16. pb_v1 "property-household/pb/v1"
  17. )
  18. //
  19. func WxPublicEmptyUnionId(ctx context.Context, req *pb_v1.WxPublicEmptyUnionIdRequest) (reply *pb_v1.WxPublicEmptyUnionIdReply, err error) {
  20. reply = &pb_v1.WxPublicEmptyUnionIdReply{}
  21. // 捕获各个task中的异常并返回给调用者
  22. defer func() {
  23. if r := recover(); r != nil {
  24. err = fmt.Errorf("%+v", r)
  25. e := &status.Status{}
  26. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  27. logger.Error("err",
  28. zap.String("system_err", err.Error()),
  29. zap.Stack("stacktrace"))
  30. }
  31. }
  32. }()
  33. r, err := cache.Redis().SetNxEx("wx_public_task", "1", 3600)
  34. if err != nil {
  35. return reply, errors.RedisError
  36. }
  37. if !r {
  38. return reply, nil
  39. }
  40. p := dbmodel.TUserWxPublic{}
  41. where := map[string]interface{}{
  42. "union_id":"",
  43. }
  44. list, err := p.List(database.DB(), where, nil, -1, -1)
  45. if err != nil {
  46. return
  47. }
  48. mreq := pb_v1.WxPublicUnionIdRequest{}
  49. where = map[string]interface{}{}
  50. values := map[string]interface{}{}
  51. for _, v := range list {
  52. mreq.OpenId = v.OpenId
  53. mreply, err := pb.Thirdparty.WxPublicUnionId(context.Background(), &mreq)
  54. if err != nil {
  55. continue
  56. }
  57. if mreply.UnionId == "" {
  58. continue
  59. }
  60. p := dbmodel.TUserWxPublic{}
  61. where["open_id"] = v.OpenId
  62. values["union_id"] = mreply.UnionId
  63. err = p.Update(database.DB(), where, values)
  64. if err != nil {
  65. continue
  66. }
  67. HandlePublicOpenId(v.OpenId, mreply.UnionId)
  68. }
  69. return reply, nil
  70. }