12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package user
- import (
- "context"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "property-household/errors"
- dbmodel "property-household/model"
- "property-household/pb"
- pb_v1 "property-household/pb/v1"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func HandlePublicOpenId(openid string, unionid string) error {
- if unionid == "" {
- return nil
- }
- u := dbmodel.TUser{}
- where := map[string]interface{}{
- "union_id": unionid,
- }
- err := u.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if u.ID == 0 {
- return nil
- }
- uinfo := dbmodel.TUser{PublicOpenId: openid}
- return userChange(u.ID, uinfo)
- }
- //
- func UserWxPublicAdd(ctx context.Context, req *pb_v1.UserWxPublicAddRequest) (reply *pb_v1.UserWxPublicAddReply, err error) {
- reply = &pb_v1.UserWxPublicAddReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- if req.OpenId == "" {
- return nil, errors.ParamsError
- }
- mreq := pb_v1.WxPublicUnionIdRequest{OpenId: req.OpenId}
- mreply, _ := pb.Thirdparty.WxPublicUnionId(ctx, &mreq)
- unionId := ""
- if mreply != nil {
- unionId = mreply.UnionId
- }
- now := time.Now()
- p := &dbmodel.TUserWxPublic{
- OpenId: req.OpenId,
- UnionId: unionId,
- CreatedAt: now,
- UpdatedAt: now,
- }
- err = p.Insert(database.DB())
- if err != nil {
- return nil, errors.DataBaseError
- }
- HandlePublicOpenId(req.OpenId, unionId)
- return reply, nil
- }
- func AddPublicTest() {
- HandlePublicOpenId("o4o5D6kM-pLBfGYTyxP9yDcqMZWk", "oF22P1kcHAvD2nqAa7Y5JIjLXz0c")
- }
|