Bladeren bron

修复user接口
增加同步微信标签功能

boweniac 6 maanden geleden
bovenliggende
commit
bbc6603e55

+ 1 - 1
desc/wechat/user.api

@@ -116,7 +116,7 @@ service Wechat {
 }
 
 @server(
-	jwt: Auth
+    jwt: Auth
 	group: User
 	middleware: Authority
 )

+ 5 - 0
etc/wechat.yaml

@@ -66,3 +66,8 @@ CasbinConf:
     e = some(where (p.eft == allow))
     [matchers]
     m = r.sub == p.sub && keyMatch2(r.obj,p.obj) && r.act == p.act
+
+Miniprogram:
+  Appid: wx1452f34bba8fe718
+  Secret: 171fdab212fdde0d51b59fa59c9ee070
+  redisaddr: redis-server:6379

+ 13 - 0
hook/contact.go

@@ -5,6 +5,19 @@ import (
 	"strings"
 )
 
+// 获取标签信息
+func (h *Hook) GetContactLabelList() (result GetContactLabelListReap, err error) {
+	resp, err := h.Client.R().SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/GetContactLabelList")
+	if err != nil {
+		return
+	}
+	if !resp.IsSuccessState() {
+		err = fmt.Errorf("GetContactLabelList failed with status code %d", resp.StatusCode)
+		return
+	}
+	return
+}
+
 // 获取好友和群信息
 func (h *Hook) GetFriendAndChatRoomList(typeStr string) (result GetFriendAndChatRoomListReap, err error) {
 	resp, err := h.Client.R().SetBody(&GetFriendAndChatRoomListReq{

+ 7 - 0
hook/type.go

@@ -90,6 +90,13 @@ type GetBatchGetChatRoomMemberWxidReap struct {
 	} `json:"data"`
 }
 
+type GetContactLabelListReap struct {
+	Label []struct {
+		Id   string `json:"id"`
+		Name string `json:"name"`
+	}
+}
+
 type GetFriendAndChatRoomListReq struct {
 	Type string `json:"type"`
 }

+ 4 - 1
internal/logic/User/get_user_info_logic.go

@@ -25,13 +25,16 @@ func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
 }
 
 func (l *GetUserInfoLogic) GetUserInfo() (resp *types.UserBaseIDInfoResp, err error) {
+	l.Logger.Errorf("------------------------GetUserInfo---------------------------")
+	l.Logger.Errorf("------------------------userId--------------------------- %+v", l.ctx.Value("userId").(string))
 	user, err := l.svcCtx.CoreRpc.GetUserById(l.ctx,
 		&core.UUIDReq{Id: l.ctx.Value("userId").(string)})
 	if err != nil {
 		return nil, err
 	}
-
+	l.Logger.Errorf("------------------------user--------------------------- %+v", user)
 	departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: user.GetDepartmentId()})
+	l.Logger.Errorf("------------------------departmentInfo--------------------------- %+v", departmentInfo)
 	if err != nil {
 		return nil, err
 	}

+ 73 - 2
internal/logic/Wxhook/get_friends_and_groups_logic.go

@@ -5,8 +5,9 @@ import (
 	"github.com/spf13/cast"
 	"github.com/suyuan32/simple-admin-common/enum/errorcode"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"strings"
+	"wechat-api/ent/label"
 	"wechat-api/hook"
-
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
 
@@ -49,10 +50,12 @@ func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *
 		return
 	}
 
+	newFriendIds := make(map[uint64][]string)
+	newLagIds := make(map[string]struct{})
 	friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
 	if friendCnt > 0 {
 		for _, friend := range friendAndChatRoomList.Friend {
-			l.svcCtx.DB.Contact.Create().
+			id, err := l.svcCtx.DB.Contact.Create().
 				SetWxWxid(wxInfo.Wxid).
 				SetType(1).
 				SetWxid(friend.Wxid).
@@ -70,6 +73,65 @@ func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *
 				UpdateNewValues().
 				SetOrganizationID(organizationId).
 				ID(l.ctx)
+			if err == nil {
+				lags := splitStringToIntArray(friend.Lag)
+				newFriendIds[id] = lags
+				for _, lag := range lags {
+					if _, exists := newLagIds[lag]; !exists {
+						newLagIds[lag] = struct{}{}
+					}
+				}
+			}
+		}
+	}
+	// 平台和微信标签映射
+	if len(newLagIds) > 0 {
+		wxLagIdsSet := make(map[string]string)
+		wxLagIds, err := hookClient.GetContactLabelList()
+		if err == nil {
+			wxSysSet := make(map[string]uint64)
+			for _, wxLagId := range wxLagIds.Label {
+				wxLagIdsSet[wxLagId.Id] = wxLagId.Name
+			}
+			for lagId := range newLagIds {
+				name := wxLagIdsSet[lagId]
+				label, err := l.svcCtx.DB.Label.Query().
+					Where(
+						label.NameEQ(name), // Filter by ID
+						label.TypeEQ(1),
+						label.OrganizationID(organizationId), // Additional filter by organizationId
+					).
+					Only(l.ctx)
+				if err != nil {
+					// 如果标签不存在则创建
+					newLabel, err := l.svcCtx.DB.Label.Create().
+						SetType(1).
+						SetName(name).
+						SetFrom(2).
+						SetMode(2).
+						SetConditions("{}").
+						SetOrganizationID(organizationId).
+						Save(l.ctx)
+					if err == nil {
+						wxSysSet[lagId] = newLabel.ID
+					}
+				} else {
+					// 如果标签存在
+					wxSysSet[lagId] = label.ID
+				}
+			}
+			// 为新联系人打标签
+			if len(newFriendIds) > 0 && len(newLagIds) > 0 {
+				for id, lags := range newFriendIds {
+					for _, lag := range lags {
+						l.svcCtx.DB.LabelRelationship.Create().
+							SetLabelID(wxSysSet[lag]).
+							SetContactID(id).
+							SetOrganizationID(organizationId).
+							Save(l.ctx)
+					}
+				}
+			}
 		}
 	}
 	chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
@@ -114,3 +176,12 @@ func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *
 
 	return
 }
+
+func splitStringToIntArray(input string) []string {
+	strArray := strings.Split(input, ",")
+	intArray := make([]string, len(strArray))
+	for i, str := range strArray {
+		intArray[i] = str
+	}
+	return intArray
+}

+ 2 - 1
internal/middleware/authority_middleware.go

@@ -55,9 +55,10 @@ func (m *AuthorityMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
 		}
 
 		result := batchCheck(m.Cbn, roleIds, act, obj)
-
+		logx.Errorw("------------------------result--------------------------- ", logx.Field("detail", result))
 		if result {
 			userIdStr := r.Context().Value("userId").(string)
+			logx.Errorw("------------------------userIdStr--------------------------- ", logx.Field("detail", userIdStr))
 			logx.Infow("HTTP/HTTPS Request", logx.Field("UUID", userIdStr),
 				logx.Field("path", obj), logx.Field("method", act))
 			data, err := m.CoreRpc.GetUserById(r.Context(), &core.UUIDReq{Id: userIdStr})