以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  自定义函数返回USERID  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=99898)

--  作者:bobolan521
--  发布时间:2017/4/28 23:15:00
--  自定义函数返回USERID
请老师帮忙看一下,企业微信里运行后出现图片里的问题,企业微信里的可信域名为www.664748.com:8083
到底是哪里出了问题
Dim e As RequestEventArgs = args(0)
If e.host = "www.664748.com" Then \'需要授权才能访问的域名
    If e.path = "aaa.htm"
        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中提取userid和username
        End If
        If UserId  > "" then\'授权成功
            e.AppendCookie("userid",UserId) \'将userid和username存储在Cookie中
            Dim u3 As String = "http://www.664748.com:8083/jiyekrs.htm"
            sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
        ElseIf e.GetValues.ContainsKey("code") = False Then \'如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
            Dim ul1 As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=SCOPE&agentid=AGENTID&state=STATE#wechat_redirect"
            Dim ul2 As String = UrlEncode("http://www.664748.com:8083/aaa.htm")
            ul1 = CExp(ul1,"ww95bdb1894744965a",ul2)
            sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
          

            e.WriteString(sb.ToString)
            Return ""
        End If
        e.WriteString(sb.ToString)
    End If
End If

[此贴子已经被作者于2017/5/2 14:17:18编辑过]

--  作者:有点蓝
--  发布时间:2017/4/29 8:51:00
--  
企业微信授权必须绑定一个应用

Dim ul1 As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=SCOPE&agentid={2}&state=STATE#wechat_redirect"
ul1 = CExp(ul1,"ww95bdb1894744965a",ul2,这里输入应用ID)

另外可信域名必须到这个应用配置中设置,设置到其它应用是无效的

--  作者:bobolan521
--  发布时间:2017/4/29 11:12:00
--  
谢谢老师,上面的代码测试了可以用了,我想把上面的代码写成个内部函数,当COOKIE没有USERID时就去执行一下,代码该如何改写
--  作者:有点蓝
--  发布时间:2017/4/29 11:32:00
--  
1楼的代码不就已经是一个内部函数 了吗


--  作者:bobolan521
--  发布时间:2017/4/29 11:38:00
--  
老师你理解错了,我说是的很多页面打面时都去执行内部函数来获取USERID,
比如说A页面打开时,如果没有e.cookie("userid"),就去执行B页面的内部函数来返回
USERID给A页面。我写了内部函数了,但返回不了值

--  作者:bobolan521
--  发布时间:2017/4/29 11:44:00
--  
我打开http://www.664748.com:8083/jiyekrs.htm时报错,好像返回不了USERID
麻烦老师帮我看一下,我哪里写错了
.NET Framework 版本:2.0.50727.5485
Foxtable 版本:2017.4.22.1
错误所在事件:自定义函数getuserid
详细错误信息:
调用的目标发生了异常。
索引超出了数组界限。

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:1.rar

图片点击可在新窗口打开查看此主题相关图片如下:1.jpg
图片点击可在新窗口打开查看

[此贴子已经被作者于2017/4/29 11:52:40编辑过]

--  作者:有点蓝
--  发布时间:2017/4/29 11:55:00
--  
验证函数

Dim e As RequestEventArgs = args(0)

Dim UserId As String
Dim sb As New StringBuilder
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中提取userid和username
End If
If UserId  > "" then\'授权成功
    e.AppendCookie("userid",UserId) \'将userid和username存储在Cookie中
    Return UserId
ElseIf e.GetValues.ContainsKey("code") = False Then \'如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
    Dim ul1 As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=SCOPE&agentid=AGENTID&state=STATE#wechat_redirect"
    Dim ul2 As String = UrlEncode("http://www.664748.com:8083/getuserid.htm")
    ul1 = CExp(ul1,"ww95bdb1894744965a",ul2)
    sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
    e.WriteString(sb.ToString)
    Return ""
End If

需要使用的时候这样:
if e.host = "www.664748.com" Then \'需要授权才能访问的域名
    If e.path = "aaa.htm"
dim userid = Functions.Execute("验证函数",e)
if userid > "" then
        验证成功的代码
       else 
       验证不成功的代码
       end if
    end if
end if

--  作者:bobolan521
--  发布时间:2017/5/2 15:32:00
--  
老师麻烦帮我看一下,为什么获取不到USERID
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:userid.rar


--  作者:有点色
--  发布时间:2017/5/2 16:12:00
--  

 你要理解一个知识点,你要访问的不是 jiyekrs.htm 网页

 

 你要访问的,是这个地址,如 https://open.weixin.qq.com/connect/oauth2/authorize

 

 先访问的是微信的地址,然后,成功以后,微信才会跳转回到你的 jiyekrs.htm 去的

 

 下面代码,得出的是你要访问的地址

 

Dim ul1 As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect"
Dim ul2 As String = UrlEncode("验证成功后跳转的地址")
ul1 = CExp(ul1,"wxa31aba4cd83af57e",ul2,"123")
Output.Show(ul1)

 

http://www.foxtable.com/mobilehelp/scr/0237.htm

 


--  作者:bobolan521
--  发布时间:2017/5/3 8:15:00
--  
老师我只想要获取USERID,获取后,我再来决定要不要跳转,不用系统自动跳转