Foxtable(狐表)用户栏目专家坐堂 → usesid


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

主题:usesid

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


加好友 发短信
等级:幼狐 帖子:138 积分:1532 威望:0 精华:0 注册:2017/3/5 20:17:00
usesid  发帖心情 Post By:2019/4/26 15:49:00 [只看该作者]

  说明书上取userid的例子,从逻辑上来说,有可能出现当第一次点击菜单时,通过code与e.Cookies("userid")取不到userid .这样进入授权链接。只有当再次点击菜单时,才能取出userid.这样会用户一个错觉,认为网打不开,或没有数据,因为没有userid时,就得不到这个人的相关权限。有没有更好的方法,点击一次就能取到userid呢?
If e.host = "wexin.foxtable.com" Then '需要授权才能访问的域名
    Dim UserId As String
    Dim UserName As String
   
Dim sb As New StringBuilder
    sb.AppendLine("<meta name='viewport' c>")
    If e.GetValues.ContainsKey("code") Then
'
如果通过授权链接跳转而来,就根据传递过来的code参数调用接口,获取用户的UserId
        Dim ul As String  = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}"
        ul = CExp(ul,Functions.Execute("GetQYAccessToken"),e.GetValues("code"))
        Dim hc As new HttpClient(ul)
        Dim jo As JObject = JObject.Parse(hc.GetData)
        If jo("UserId") IsNot Nothing Then
            UserId = jo("UserId")
        End If
    Else
        UserId = e.Cookies("userid")
'
否则从cookie中提取useridusername
    End If
    Dim Verified As Boolean
    Dim dr As DataRow = DataTables("Users").Find("userid ='" & UserId & "'")
'
根据openid找出对应的行
    If UserId  > "" AndAlso dr IsNot Nothing AndAlso dr("permit") = True
'
授权成功
        Verified  = True
        UserName = dr("name")
        e.AppendCookie("userid",UserId)
'
useridusername存储在Cookie
    ElseIf e.GetValues.ContainsKey("code") = False Then
'
如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
        Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa31aba4cd83af57e&redirect_uri=http%3a%2f%2fwexin.foxtable.com&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
        sb.Append("<meta http-equiv='Refresh' c>")
'
跳转到授权链接
        e.WriteString(sb.ToString)
        Return
    End If
    If Verified = False Then
        sb.AppendLine(
"
你无权访问本系统")
    Else
        sb.AppendLine(
"
欢迎" & UserName & " , <a href='http://wexin.foxtable.com'>刷新页面</a>")
    End If
    e.WriteString(sb.ToString)

End
If

 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106550 积分:541915 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/4/26 16:08:00 [只看该作者]

帮助的逻辑是没有问题的。第一次点击如果网页不是提过授权进来的,会重新提过授权加入网页,授权后就可以直接跳转了。

如果是通过公众号的菜单打开的,菜单里设置的url就应该是包含授权的url(上面ul变量的内容),这样既不需要再跳转一次网页。

不管我觉得您的问题应该不是这个,贴出您的代码说明问题。

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


加好友 发短信
等级:幼狐 帖子:138 积分:1532 威望:0 精华:0 注册:2017/3/5 20:17:00
  发帖心情 Post By:2019/4/26 16:24:00 [只看该作者]

老师:我的跳转链接好象不起作用,也就是跳转不到 e.post = "sxwytx.iego.cn"这个位置。不知什么原因?

if e.post = "sxwytx.iego.cn" then
If e.GetValues.ContainsKey("code" Then
    Dim ul As String  = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}"
    ul = CExp(ul,Functions.Execute("GetQYAccessToken1"),e.GetValues("code"))
    Dim hc As new HttpClient(ul)
    Dim jo As JObject = JObject.Parse(hc.GetData)
    If jo("UserId") IsNot Nothing Then
        UserId = jo("UserId")
    End If
Else
    UserId = e.Cookies("userid") '否则从cookie中提取userid和username
    If userid = "" andalso e.GetValues.ContainsKey("code") = false then
        Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww51c9a9c0c93a7d18&redirect_uri=http%3a%2f%2fsxwytx.iego.cn&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
        sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
        e.WriteString(sb.ToString)
        Return
    End If
End If
If userid > "" Then
    e.AppendCookie("userid",UserId) '将userid和username存储在Cookie中
    Dim dr1 As DataRow = DataTables("人员信息表").SQLFind("人员ID = '" & userid & "'")
    If dr1 IsNot Nothing Then
        Userrols = dr1("角色")
        usergroup = dr1("地市")
    End If
End If


Select Case e.Path
    Case "gxyh.htm"
        Functions.Execute("gxyh",e)
    Case "ccss.htm"
        sb.AppendLine("<meta name='viewport' c>")
        sb.AppendLine("您的帐号是:" &  userid & "<br/><br/>")
        sb.AppendLine("您的角色是:" & Userrols  & "<br/><br/>")
        sb.AppendLine("您的地市是:" & usergroup  & "<br/><br/>")
        e.WriteString(sb.ToString)
End Select
end if

[此贴子已经被作者于2019/6/8 21:58:19编辑过]

 回到顶部
帅哥,在线噢!
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106550 积分:541915 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/4/26 16:49:00 [只看该作者]

这个是跳转的问题,cookie设置后要跳转或者刷新页面才能生效。上面代码第一次验证后没有经过任何跳转就直接进入页面了,此时cookie还没有生效。可以这样

Select Case e.Path
    Case "gxyh.htm"
        Functions.Execute("gxyh",e,UserId) ‘把UserId也传入函数,在函数里直接使用传入的UserId使用

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


加好友 发短信
等级:幼狐 帖子:138 积分:1532 威望:0 精华:0 注册:2017/3/5 20:17:00
  发帖心情 Post By:2019/6/8 22:01:00 [只看该作者]

老师:我的跳转链接好象不起作用,也就是跳转不到 e.post = "sxwytx.iego.cn"这个位置。不知什么原因?
        实际情况是:只要cookies中没有userid,只第一次获取不出openid,微信第二次打开时,才能获取出openid.经测试

if e.post = "sxwytx.iego.cn" then
If e.GetValues.ContainsKey("code" Then
    Dim ul As String  = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}"
    ul = CExp(ul,Functions.Execute("GetQYAccessToken1"),e.GetValues("code"))
    Dim hc As new HttpClient(ul)
    Dim jo As JObject = JObject.Parse(hc.GetData)
    If jo("UserId") IsNot Nothing Then
        UserId = jo("UserId")
    End If
Else
    UserId = e.Cookies("userid") '否则从cookie中提取userid和username
    If userid = "" andalso e.GetValues.ContainsKey("code") = false then
        Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww51c9a9c0c93a7d18&redirect_uri=http%3a%2f%2fsxwytx.iego.cn&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
        sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
        e.WriteString(sb.ToString)
        Return
    End If
End If
If userid > "" Then
    e.AppendCookie("userid",UserId) '将userid和username存储在Cookie中
    Dim dr1 As DataRow = DataTables("人员信息表").SQLFind("人员ID = '" & userid & "'")
    If dr1 IsNot Nothing Then
        Userrols = dr1("角色")
        usergroup = dr1("地市")
    End If
End If
Select Case e.Path
    Case "ccss.htm"
        sb.AppendLine("<meta name='viewport' c>")
        sb.AppendLine("您的帐号是:" &  userid & "<br/><br/>")
        sb.AppendLine("您的角色是:" & Userrols  & "<br/><br/>")
        sb.AppendLine("您的地市是:" & usergroup  & "<br/><br/>")
        e.WriteString(sb.ToString)
End Select
end if

[此贴子已经被作者于2019/6/8 22:07:19编辑过]

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


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2019/6/9 10:22:00 [只看该作者]

 

在你的httprequest事件,加入代码 msgbox(e.path) 弹出每一次的访问路径看看

 

 


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


加好友 发短信
等级:幼狐 帖子:138 积分:1532 威望:0 精华:0 注册:2017/3/5 20:17:00
  发帖心情 Post By:2019/6/9 11:38:00 [只看该作者]

    甜老师:每次路径都是"ccss.htm",应该没有问题。经测试只要cookies存有userid ,则每次都 能正常获取出userid,如cookies 没存有userid 则第一次肯定获取不出userid,反复测试,应该是跳转问题,也就是跳转不到If e.host = "sxwytx.iego.cn" Then 这个位置,但跳转代码肯定执行了,因为再次进入的时候,能从coad 中获取出openid.只是跳转不到起始位置。苹果系统每次屏保后,因为系统的原因,每次都会清空cookies 所以出现这种情况的概率高。

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


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2019/6/9 11:43:00 [只看该作者]

在httprequest事件,弹出 e.path 的值看一下啊。

 

把cookie弄成空的时候,测试,看是否跳转过来了啊,如果跳转了,就没问题啊。


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


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2019/6/9 11:44:00 [只看该作者]

 

你访问微信获取userid的时候,跳转回去,你是跳转到了主页面啊,也就是,这个时候e.path应该是空值啊。

 

 

 


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


加好友 发短信
等级:幼狐 帖子:138 积分:1532 威望:0 精华:0 注册:2017/3/5 20:17:00
  发帖心情 Post By:2019/6/9 13:04:00 [只看该作者]

甜老师:这是我刚放msgbox(e.path)的位置。同时我也显示了一下:在code后面放了msgbox("1:" & UserId)  ;UserId = e.Cookies("userid")后面放了msgbox("2:" & UserId) 。我把cookies清空后,第一次进入微信后,msgbox("1:" & UserId)没有显示,msgbox("2:" & UserId) 显示为空。再次进入入微信,msgbox("1:" & UserId)还是没有显示,msgbox("2:" & UserId) 显示正常的openid。我的理解是第二次进入微信应该是从code中获取出openid,但确是是从cookies中获取出openid的。也就是说清空cookies后,第一次进入微信,正常跳转是没有问题,openid也正常取出来了,但为什么显示不出来呢?



msgbox(e.path)  '刚才显示的位置
if e.post = "sxwytx.iego.cn" then
If e.GetValues.ContainsKey("code" Then
    Dim ul As String  = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}"
    ul = CExp(ul,Functions.Execute("GetQYAccessToken1"),e.GetValues("code"))
    Dim hc As new HttpClient(ul)
    Dim jo As JObject = JObject.Parse(hc.GetData)
    If jo("UserId") IsNot Nothing Then
        UserId = jo("UserId")
msgbox("1:" & UserId)
    End If
Else
    UserId = e.Cookies("userid") '否则从cookie中提取userid和username
msgbox("2:" & UserId)
    If userid = "" andalso e.GetValues.ContainsKey("code") = false then
        Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww51c9a9c0c93a7d18&redirect_uri=http%3a%2f%2fsxwytx.iego.cn&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
        sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
        e.WriteString(sb.ToString)
        Return
    End If
End If
If userid > "" Then
    e.AppendCookie("userid",UserId) '将userid和username存储在Cookie中
    Dim dr1 As DataRow = DataTables("人员信息表").SQLFind("人员ID = '" & userid & "'")
    If dr1 IsNot Nothing Then
        Userrols = dr1("角色")
        usergroup = dr1("地市")
    End If
End If
Select Case e.Path
    Case "ccss.htm"
        sb.AppendLine("<meta name='viewport' c>")
        sb.AppendLine("您的帐号是:" &  userid & "<br/><br/>")
        sb.AppendLine("您的角色是:" & Userrols  & "<br/><br/>")
        sb.AppendLine("您的地市是:" & usergroup  & "<br/><br/>")
        e.WriteString(sb.ToString)
End Select
end if


 回到顶部
总数 13 1 2 下一页