以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]公众号授权的问题  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=162083)

--  作者:fczhaobo
--  发布时间:2021/4/5 11:54:00
--  [求助]公众号授权的问题
问题:如果用户同意授权,会自动切换,如果拒绝的话怎么样关闭页面,谢谢!

Dim e As RequestEventArgs = args(0)
Dim jsbh As String = e.GetValues("jsbh")
Dim url As String = "tuanbao.htm?jsbh=" & jsbh
Dim sb As New StringBuilder
sb.AppendLine("<meta name=\'viewport\' c>")
Dim UserName As String
Dim OpenID As String
If e.GetValues.ContainsKey("code") Then \'如果是通过授权链接跳转而来,就从链接重提取code来获取openid
    Dim ul As String  = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
    ul = CExp(ul,vars("appid"),vars("AppSecret"),e.GetValues("code"))
    Dim hc As new HttpClient(ul)
    Dim jo As JObject = JObject.Parse(hc.GetData)
    If jo("openid") IsNot Nothing Then \'如果获取openid成功(成功的话,还会同时返回一个accesstiken,用于获取用户详情)
        OpenID = jo("openid")
        Dim dr As DataRow = DataTables("WXUsers").sqlFind("openid =\'" & Openid & "\'")
        If dr IsNot Nothing Then
            UserName = dr("nickname")
        Else
            ul = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN "
            \'根据openid和accesstoken获取用户详情,注意这里这个accesstoken不是普通accesston,只能用于网页授权
            hc = New HttpClient(CExp(ul, jo("access_token"), OpenId))
            jo = jo.Parse(hc.GetData)
            If jo("openid") IsNot Nothing Then
                \'UserName = jo("nickname")
                dr = DataTables("WXUsers").AddNew()
                Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl"} \'""
                For Each nm As String In nms
                    dr(nm) = jo(nm)
                Next
                dr.Save
            Else
                e.WriteString(jo.ToString) \'在用户浏览器显示错误信息
                Return ""
            End If
        End If
        e.AppendCookie("openid",OpenID)
    Else
        e.WriteString(jo.ToString) \'在用户浏览器显示错误信息
        Return ""
    End If
Else
    OpenID = e.Cookies("openid")
    If userName = "" OrElse OpenID = "" Then
        Dim ul1 As String  = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state={2}#wechat_redirect"
        Dim ul2 As String = UrlEncode("http://" & vars("域名") & "/" & url) \'包装原基本URL
        ul1 = CExp(ul1,vars("AppId"),ul2,"123")
        sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
        e.WriteString(sb.ToString)
        Return ""
    End If
End If
e.WriteString(sb.ToString)

--  作者:有点蓝
--  发布时间:2021/4/5 21:31:00
--  
设置授权链接的时候增加一个cookie,判断这个cookie是否有值,如果有说明拒绝

Else
dim a as string = e.Cookies("authorize")
if a = "1" then
        With wb.AddMsgPage("","msgpage","请退出","您已拒绝授权,请关闭页面退出")
        
End With
e.WriteString(wb.Build
return
end if
Dim wb As New weui
    OpenID = e.Cookies("openid")
    If userName = "" OrElse OpenID = "" Then
        Dim ul1 As String  = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state={2}#wechat_redirect"
        Dim ul2 As String = UrlEncode("http://" & vars("域名") & "/" & url) \'包装原基本URL
        ul1 = CExp(ul1,vars("AppId"),ul2,"123")
        wb.InsertHTML("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
wb.AppendCookie("authorize","1")
        e.WriteString(wb.Build)
        Return ""
    End If
End If