Esempio n. 1
0
        private void TreeView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (treeView1.Nodes.Count == 0)
            {
                return;
            }
            var root = treeView1.Nodes[0];

            if (e.Button == MouseButtons.Left)
            {
                var node = treeView1.SelectedNode;
                if (node != null && node.Level > 0)
                {
                    NIM.Team.TeamAPI.QuerySingleMemberInfo(_teamId, node.Name, (info) =>
                    {
                        Action action = () =>
                        {
                            this.flowLayoutPanel1.Controls.Clear();
                            ObjectPropertyInfoForm.CreateFormContent(info, this.flowLayoutPanel1);
                        };
                        this.Invoke(action);
                    });
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                List <TreeNode> treeNodes = new List <TreeNode>();
                foreach (var n in root.Nodes)
                {
                    TreeNode nd = n as TreeNode;
                    if (nd.Checked)
                    {
                        treeNodes.Add(nd);
                    }
                }
                if (treeNodes.Count == 1)
                {
                    var      node = treeNodes[0];
                    MenuItem menu = new MenuItem("移出群", (s, args) =>
                    {
                        NIM.Team.TeamAPI.KickMemberOutFromTeam(_teamId, new string[] { node.Name }, (a) =>
                        {
                            if (a.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
                            {
                                foreach (var id in a.TeamEvent.IdCollection)
                                {
                                    _memberCollection.Remove(id);
                                    _teamMmebers.Remove(id);
                                }
                                UpdateTreeView();
                            }
                            else
                            {
                                MessageBox.Show("操作失败:" + a.TeamEvent.ResponseCode.ToString());
                            }
                        });
                    });
                    ContextMenu m   = new ContextMenu();
                    string      uid = node.Name;
                    if (_teamMmebers.ContainsKey(uid))
                    {
                        var      memInfo      = _teamMmebers[uid];
                        bool     normalMember = memInfo.Type == NIM.Team.NIMTeamUserType.kNIMTeamUserTypeNomal;
                        string   txt          = normalMember ? "设为管理员" : "取消管理员";
                        MenuItem item         = new MenuItem(txt, (s, args) =>
                        {
                            if (normalMember)
                            {
                                NIM.Team.TeamAPI.AddTeamManagers(_teamId, new string[] { uid }, (ret) =>
                                {
                                    DemoTrace.WriteLine(txt, ret.Dump());
                                });
                            }
                            else
                            {
                                NIM.Team.TeamAPI.RemoveTeamManagers(_teamId, new string[] { uid }, (ret) =>
                                {
                                    DemoTrace.WriteLine(txt, ret.Dump());
                                });
                            }
                        });
                        var      text1 = memInfo.IsMuted ? "取消禁言" : "禁言";
                        MenuItem item1 = new MenuItem(text1, (s, args) =>
                        {
                            if (memInfo.IsMuted)
                            {
                                NIM.Team.TeamAPI.SetMemberMuted(_teamId, uid, false, (ret) =>
                                {
                                    DemoTrace.WriteLine(text1, ret.Dump());
                                    if (ret.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
                                    {
                                        memInfo.SetMuted(false);
                                        _teamMmebers[uid] = memInfo;
                                    }
                                });
                            }
                            else
                            {
                                NIM.Team.TeamAPI.SetMemberMuted(_teamId, uid, true, (ret) =>
                                {
                                    DemoTrace.WriteLine(text1, ret.Dump());
                                    if (ret.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
                                    {
                                        memInfo.SetMuted(true);
                                        _teamMmebers[uid] = memInfo;
                                    }
                                });
                            }
                        });
                        MenuItem item2 = new MenuItem("修改群昵称", (s, args) =>
                        {
                            var member      = NIM.Team.TeamAPI.QuerySingleMemberInfo(_teamId, uid);
                            member.NickName = "nickname:" + new Random().Next();
                            NIM.Team.TeamAPI.UpdateMemberNickName(member, null);
                        });
                        m.MenuItems.Add(item);
                        var myInfo = _teamMmebers[FriendsListForm.SelfId];
                        if (myInfo.Type == NIM.Team.NIMTeamUserType.kNIMTeamUserTypeCreator || myInfo.Type == NIM.Team.NIMTeamUserType.kNIMTeamUserTypeManager)
                        {
                            m.MenuItems.Add(item1);
                            m.MenuItems.Add(item2);
                        }
                    }
                    m.MenuItems.Add(menu);
                    m.Show(treeView1, e.Location);
                }
                else if (treeNodes.Count > 1)
                {
                    MenuItem menu = new MenuItem("群推送测试", (s, args) =>
                    {
                        NIM.TeamForecePushMessage forceMsg = new NIM.TeamForecePushMessage();
                        forceMsg.Content = string.Format("群推送测试:" + DateTime.Now.ToLongTimeString());
                        if (treeNodes.Count < root.Nodes.Count)
                        {
                            forceMsg.ReceiverList = new List <string>();
                            foreach (TreeNode t in treeNodes)
                            {
                                forceMsg.ReceiverList.Add(t.Text);
                            }
                        }
                        NIM.NIMTextMessage msg = new NIM.NIMTextMessage();
                        msg.ReceiverID         = root.Text;
                        msg.SessionType        = NIM.Session.NIMSessionType.kNIMSessionTypeTeam;
                        msg.TextContent        = "群推送消息..." + new Random().NextDouble().ToString();
                        NIM.TalkAPI.SendTeamFrocePushMessage(msg, forceMsg);
                    });
                    ContextMenu m = new ContextMenu();
                    m.MenuItems.Add(menu);
                    m.Show(treeView1, e.Location);
                }
                else
                {
                    MenuItem menu = new MenuItem("邀请入群", (s, args) =>
                    {
                        CreateTeamForm form    = new CreateTeamForm(FormType.InviteMemeber);
                        form.MembersIDSelected = (n, i) =>
                        {
                            NIM.Team.TeamAPI.Invite(_teamId, i, n, (r) =>
                            {
                                if (r.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResTeamInviteSuccess)
                                {
                                    MessageBox.Show("邀请成功,等待对方验证: " + r.TeamEvent.ResponseCode.ToString());
                                }
                                else if (r.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
                                {
                                    foreach (var id in r.TeamEvent.IdCollection)
                                    {
                                        NIM.Team.TeamAPI.QuerySingleMemberInfo(_teamId, id, (ret) =>
                                        {
                                            _teamMmebers[id] = ret;
                                        });
                                    }
                                    UpdateTreeView();
                                }
                                else
                                {
                                    MessageBox.Show("邀请失败:" + r.TeamEvent.ResponseCode.ToString());
                                }
                            });
                        };
                        form.Show();
                    });
                    ContextMenu m = new ContextMenu();

                    m.MenuItems.Add(menu);
                    m.Show(treeView1, e.Location);
                }
            }
        }