自由设置好友

本节的内容可以参考CaseStudy目录下的文件: 内置验证QQServer.Table,内置验证QQClient.Table

上一节讲到,使用内置身份验证时,虽然可以根据用户分组或角色选择好友, 但局限性比较大,不够灵活。

你可以自行扩展服务器项目,让服务端管理人员可以给登录者自由选择好友。

设计步骤

1、首先在服务端项目增加一个名为“好友”的表,这个表有两列,分别是“用户名”和“好友”,前者是字符型,后者是备注型:

提示:

上表的好友列,可以包括单个的用户名,也可以包括分组名,其中分组名用方括号括起来。
例如上表中董燕的好友包括贺辉、周明、杨刚,以及所有分组为员工的用户。

2、然后设计一个下图所示的窗口:

窗口的AfterLoad事件代码为:

Dim trv1 As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
trv2 As WinForm.TreeView = e.Form.Controls("TreeView2")
Dim
trvs() As WinForm.TreeView = {trv1,trv2}
For
Each trv As WinForm.TreeView In trvs
    For Each u As UserInfo In Users
        If u.Type = UserTypeEnum.User Then
            If trv.Nodes.Contains(u.Group) = False Then
                trv.Nodes.Add(u.Group)
            End If
            trv.Nodes(u.Group).Nodes.Add(u.Name)
        End If
   
Next

Next

trv2
.CheckBoxes = True '右边目录树显示复选框

左边目录树的AfterSelectNode事件代码设置为:

Dim trv1 As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
trv2 As WinForm.TreeView = e.Form.Controls("TreeView2")
Dim
dr As DataRow
If
e.node.Level = 1 Then
    dr = DataTables(
"
好友").Find("员工 = '" & e.Node.Name & "'")
End
If
Dim
nms As New List(of String)
If
dr IsNot Nothing AndAlso dr.IsNull("好友") = False
    nms.AddRange(dr(
"
好友").Split(","))
End
If
For
Each nd As WinForm.TreeNode In trv2.AllNodes
    If nd.Level = 0 Then
        nd.Checked = nms.Contains("[" &nd.Name & "]")
    Else
        nd.Checked = nms.Contains(nd.Name)
   
End If
Next

trv2
.Enabled = (e.node.Level = 1)

右边目录树的AfterCheckNode事件代码设置为:

Dim trv1 As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
trv2 As WinForm.TreeView = e.Form.Controls("TreeView2")
Dim
nd1 As WinForm.TreeNode = trv1.SelectedNode
If
nd1.Level = 0 Then
   
Return

End
If
Dim
dr As DataRow
If
e.Node.ParentNode IsNot Nothing Then '取消父节点的选中标记,因为选中子节点,就不要选中父节点了
    e.Node.ParentNode.Checked =
False

End
If
For
Each cnd As WinForm.TreeNode In e.Node.Nodes '取消子节点的选中标记,因为选中父节点,就不要选中子节点了
    cnd.Checked =
False

Next

dr
= DataTables("好友").Find("员工 = '" & nd1.Name & "'")
If
dr Is Nothing  Then
    dr = DataTables(
"
好友").AddNew()
    dr(
"
员工") = nd1.Name
End
If
Dim
nms As String
For
Each nd2 As WinForm.TreeNode In trv2.Nodes '先添加分组
    If nd2.Checked Then
        nms  = nms & ",[" &  nd2.Name & "]" 
'
分组名用方括号括起来,表示这是一个分组.
    End
If

Next
For
Each nd2 As WinForm.TreeNode In trv2.AllNodes '然后是普通用户
    If nd2.Level = 1 AndAlso nd2.Checked AndAlso nd1.Name <> nd2.Name Then
        nms = nms & "," &  nd2.Name
    End
If

Next
If
nms > "" Then
    dr(
"
好友") = nms.Trim(",")
Else

    dr("好友") =
Nothing

End
If

4、最后将QQserver的UserLogging事件代码设置为:

If e.User Is Nothing Then
   
Return

Else

    e.Success =
True

End
If
Dim
dr As DataRow
dr
= DataTables("好友").Find("员工 = '" & e.User.Name & "'")
If
dr IsNot Nothing Then
    Dim nms As New List(of String)
    nms.AddRange(dr(
"
好友").Split(","))
    For Each u As UserInfo In Users
        If u.Name <> e.User.Name Then
            If nms.Contains(u.Name) OrElse nms.Contains("[" & u.Group & "]")
                If u.Group > ""  Then
                    e.Buddies.Add(u.Group & "." & u.Name)
                Else
                    e.Buddies.Add(u.Name)
                End If
            End If
        End If
   
Next

Else

    For Each u As UserInfo In Users
        If u.Name <> e.User.Name  Then
            If u.Group > ""  Then
                e.Buddies.Add(u.Group & "." & u.Name)
            Else
                e.Buddies.Add(u.Name)
           
End If
        End If
   
Next

End
If


本页地址:http://www.foxtable.com/webhelp/topics/3013.htm