diff --git a/bot_test.go b/bot_test.go index df3fa91..5c141e9 100644 --- a/bot_test.go +++ b/bot_test.go @@ -1,6 +1,8 @@ package openwechat -import "testing" +import ( + "testing" +) func defaultBot(modes ...mode) *Bot { bot := DefaultBot(modes...) @@ -43,3 +45,17 @@ func TestFriend(t *testing.T) { } t.Log(friends) } + +func TestMps(t *testing.T) { + self, err := getSelf() + if err != nil { + t.Error(err) + return + } + mps, err := self.Mps() + if err != nil { + t.Error(err) + return + } + t.Log(mps) +} diff --git a/relations.go b/relations.go index 500d366..460bffd 100644 --- a/relations.go +++ b/relations.go @@ -207,3 +207,57 @@ func isFriend(user User) bool { func isGroup(user User) bool { return strings.HasPrefix(user.UserName, "@@") && user.VerifyFlag == 0 } + +func isMP(user User) bool { + return user.VerifyFlag == 8 || user.VerifyFlag == 24 || user.VerifyFlag == 136 +} + +type Mp struct{ *User } + +func (m Mp) String() string { + return fmt.Sprintf("", m.NickName) +} + +type Mps []*Mp + +func (m Mps) Count() int { + return len(m) +} + +func (m Mps) First() *Mp { + if m.Count() > 0 { + return m[0] + } + return nil +} + +func (m Mps) Last() *Mp { + if m.Count() > 0 { + return m[m.Count()-1] + } + return nil +} + +func (m Mps) Search(limit int, condFuncList ...func(group *Mp) bool) (results Mps) { + if condFuncList == nil { + return m + } + if limit <= 0 { + limit = m.Count() + } + for _, member := range m { + if results.Count() == limit { + break + } + var passCount int + for _, condFunc := range condFuncList { + if condFunc(member) { + passCount++ + } + } + if passCount == len(condFuncList) { + results = append(results, member) + } + } + return +} diff --git a/user.go b/user.go index 84d659c..e936dd8 100644 --- a/user.go +++ b/user.go @@ -119,6 +119,7 @@ type Self struct { members Members friends Friends groups Groups + mps Mps } // 获取所有的好友、群组、公众号信息 @@ -194,6 +195,16 @@ func (s *Self) Groups(update ...bool) (Groups, error) { return s.groups, nil } +// 获取所有的公众号 +func (s *Self) Mps(update ...bool) (Mps, error) { + if s.mps == nil { + if err := s.updateMps(update...); err != nil { + return nil, err + } + } + return s.mps, nil +} + // 更新好友处理 func (s *Self) updateFriends(update ...bool) error { var isUpdate bool @@ -239,6 +250,27 @@ func (s *Self) updateGroups(update ...bool) error { return nil } +func (s *Self) updateMps(update ...bool) error { + var isUpdate bool + if len(update) > 0 { + isUpdate = update[len(update)-1] + } + if isUpdate || s.members == nil { + if err := s.updateMembers(); err != nil { + return err + } + } + var mps Mps + for _, member := range s.members { + if isMP(*member) { + mp := &Mp{member} + mps = append(mps, mp) + } + } + s.mps = mps + return nil +} + // 更新所有的联系人信息 func (s *Self) UpdateMembersDetail() error { // 先获取所有的联系人