Foxtable(狐表)用户栏目专家坐堂 → 关于代码优化


  共有2964人关注过本帖树形打印复制链接

主题:关于代码优化

帅哥哟,离线,有人找我吗?
ygg8310
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:989 积分:8827 威望:0 精华:0 注册:2016/4/10 14:33:00
关于代码优化  发帖心情 Post By:2019/7/25 15:02:00 [只看该作者]

为什么我这个一直报错?
Static UserTable As DataTable '定义一个变量,用于存储用户随机身份ID,以及最后一次活动时间.
Static
ClearTime As Date
If
UserTable Is Nothing Then '创建用于记录登录信息的临时表
    ClearTime = Date.Now()
   
Dim dtb As New DataTableBuilder("UserInfos")
    dtb.AddDef("UserName", Gettype(String), 16)
    dtb.AddDef("UserID",Gettype(String),16)
    dtb.AddDef("ActiveTime",Gettype(Date))
    UserTable = dtb.Build(True)

End
If
If
(Date.Now - ClearTime).TotalMinutes >= 30 Then '清除超过30分钟没有操作的登录信息
    UserTable.DeleteFor("ActiveTime < #" & Date.Now.AddMinutes(-30) & "#")
    ClearTime = Date.Now()
End
If
Dim wb As New weui
'
身份验证
Dim
UserName As String
Dim
Password As String
Dim
UserID As String
If
e.Path = "logon.htm" '验证用户名和密码
    If e.PostValues.ContainsKey("username") AndAlso e.PostValues.ContainsKey("password"Then
        Dim Verified As Boolean
'
用于标记用户是否通过了身份验证
        UserName = e.PostValues("username")
        Password = e.PostValues("password")
        If UserName =
"
张三" AndAlso Password = "888" Then  '实际使用的时候,请改为从数据库读取用户名和密码进行比较
            Verified  = True
        ElseIf Username =
"
李四" AndAlso Password="999" Then
            Verified  = True
        End If
        If Verified Then
            UserID = Rand.NextString(16)
'
生成随机用户ID
            UserName = EncryptText(UserName,"123","123")
'
将用户名加密.
            Dim dr As DataRow = UserTable.Find("UserName = '" & UserName & "'")
            If  dr IsNot Nothing Then
'
如果是重复登录,删除以前的登录信息
                dr.Delete()
            End If
            dr = UserTable.AddNew()
            dr("UserName") = UserName
            dr("UserID") = UserId
            dr("ActiveTime") = Date.Now
'
记录登录时间
            wb.AppendCookie("username",UserName)
'
将用户名和密码写入cookie
            wb.AppendCookie("userid",UserID)
            wb.InsertHTML("<meta http-equiv='Refresh' c>")
'
直接跳转到首页
            e.WriteString(wb.Build)
'
生成网页
            Return
'
必须的
        End If
    End
If

Else
'其它页面从Cookie提取登录信息进行验证
    UserName = e.Cookies("username"
'
cookie中获取用户名
    UserID = e.Cookies("userid"
'
cookie中获取 随机ID
    Dim dr As DataRow = UserTable.Find("UserName = '" & UserName & "'")
    If dr IsNot Nothing AndAlso dr("UserID") = UserID Then 
'
如果通过验证,更新活动时候,继续访问其它页面.
        dr("ActiveTime") = Date.Now
'
更新活动时间
    Else
'
如果验证失败
        wb.InsertHTML("<meta http-equiv='Refresh' c>")
'
那么直接跳转到登录页面
        e.WriteString(wb.Build)
'
生成网页
        Return
'
必须的
    End
If

End
If
'
开始生成网页
Select
Case e.path
   
Case "logon.htm" '登录页面
        wb.AddPageTitle("","pageheader","销售系统","由湛江辉迅基于Foxtable开发")
        If e.PostValues.ContainsKey("username") AndAlso e.PostValues.ContainsKey("password"Then '判断是否是验证失败后的重新登录
            wb.AddTopTips("","toptip1","用户名或密码错误!").msec = 2000 '如果用户通过登录按钮访问,则给用户一个2秒的提示.
       
End If
        wb.AddForm("","form1","logon.htm")
        With wb.AddInputGroup("form1","ipg1")
            .AddInput("username",
"
户名","text")
            .AddInput("password",
"
密码","password")
        End With
        With wb.AddButtonGroup("form1","btg1",True)
            .Add("btn1",
"
登录", "submit")
        End With
    Case "exit.htm"
'
退出登录
        wb.DeleteCookie("username")
'
清除cookie中原来的用户名和密码
        wb.DeleteCookie("password")
        wb.InsertHTML("<meta http-equiv='Refresh' c>")
'
然后直接跳转到登录页面
    Case "", "default.htm"
'
首页
        wb.AddPageTitle("","pageheader","销售系统","由湛江辉迅基于Foxtable开发")
Dim wb As New weui
Select
Case e.Path
    Case "addnew.htm"
        If e.PostValues.Count = 0 Then
            wb.AddForm("","form1","addnew.htm")
            With wb.AddInputGroup("form1","ipg1",
"
客户资料")
                .AddInput(
"
姓名","姓名","Text") '前一个"姓名"ID,后一个"姓名"是标题
                .AddInput(
"
年龄","年龄","number")
                .AddInput(
"
日期","日期","date")
                .AddSelect(
"
级别","级别","普通会员|高级会员|VIP会员")
                .AddSwitch(
"
停权","停权").Value = "True"
            End With
            With wb.AddRadioGroup("form1",
"
学历","最高学历")
                .Add(
"
本科","本科")
                .Add(
"
硕士","硕士")
                .Add(
"
博士","博士")
            End With
            With wb.AddCheckGroup("form1",
"
偏好","品牌偏好")
               
.Add("苹果","苹果")
                .Add("华为","华为")
               
.Add("三星","三星")
            End With
            With wb.AddButtonGroup("form1","btg1",True)
                .Add("btn1",
"
确定", "submit")
            End With
            e.WriteString(wb.Build)
        Else
            Dim nms() As String =
{"
姓名","年龄","日期","级别"} '不能为空的列名数组
            For Each nm As String In nms

               
If e.PostValues.ContainsKey(nm) = False Then '生成错误提示页
                    With wb.AddMsgPage("","msgpage","增加失败", nm & "列不能为空!")
                        .icon = "Warn" '改变图标
                        .AddButton("btn1","返回").Attribute = ""
                    End With
                    e.WriteString(wb.Build)
                    Return '必须返回
                End If
            Next
            nms = New String() {"姓名","年龄","日期","级别","停权","学历"} 
'重新定义了nms数组,增加了两列.
           
Dim dr As DataRow = DataTables("员工").AddNew()
            For Each nm As String In nms
                If e.
PostValues.ContainsKey(nm) Then
                    dr(nm) = e.
PostValues(nm)
                End If
            Next
            '以下代码处理品牌复选列表项
            Dim pp As String
            nms = New String() {"苹果","华为","三星"}
'将nms重新定义为品牌数组
           
For Each nm As String In nms
                If e.PostValues.ContainsKey(nm) AndAlso e.PostValues(nm).Trim() = "on" Then
'
不能省略Trim
                    pp = pp & nm  & ","
                End If
            Next
            If pp > "" Then
                dr(
"
偏好") = pp.Trim(",")
            End If
           
'
保存并生成增加成功提示页面
            dr.save()
            With wb.
AddMsgPage("","msgpage","增加成功", "好好学习,天天向上") '生成成功提示页
               
.AddButton("btn1","继续增加","addnew.htm")
            End With
            e.WriteString(wb.Build)
        End
If
End
Select

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/7/25 15:15:00 [只看该作者]

    Case "", "default.htm" '首页
        wb.AddPageTitle("","pageheader","销售系统","由湛江辉迅基于Foxtable开发")
'Dim wb As New weui 删除这句
'Select
 Case e.Path 删除这句
    Case "addnew.htm"
        If e.PostValues.Count = 0 Then

 回到顶部
帅哥哟,离线,有人找我吗?
ygg8310
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:989 积分:8827 威望:0 精华:0 注册:2016/4/10 14:33:00
  发帖心情 Post By:2019/7/25 16:04:00 [只看该作者]

非常有用,谢谢老师
[此贴子已经被作者于2019/7/25 16:13:04编辑过]

 回到顶部