Dim sb As New StringBuilder '公众号授权详情页
sb.AppendLine("<meta name='viewport' c>")
If e.host = "www.123.com.cn" Then '授权测试
Dim UserName As String
Dim OpenID As String
Dim KHID As String
MsgBox(e.Request.URL.ToString) '--------------------
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, "wx88888888888888", "fuhgfdjknnbh58546588855", 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("客户").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("客户").AddNew()
Dim nms() As String = {"openid", "nickname", "headimgurl", "性别"} '"", "客户住址_县市", "country", "客户住址_省市"} '""
For Each nm As String In nms
dr(nm) = jo(nm)
' msgbox("提取数据", 64, "提示")
Next
msgbox("判断tjr有=" & e.GetValues.ContainsKey("state")) '--------------------
If e.GetValues.ContainsKey("state") Then 'e.GetValues.ContainsKey("tjr")获取二维码推荐人的TJkhid
msgbox("写入推荐人: " & e.GetValues("state"), 64, "提示")
dr("客户来源_KHID") = e.GetValues("state") 'e.GetValues.ContainsKey("TJkhid")获取二维码推荐人的TJkhid ,保存tjkhid
End If
dr.Save
Else
e.WriteString(jo.ToString) '在用户浏览器显示错误信息
' msgbox("不能增行token里openid空", 64, "提示空")
Return
End If
End If
e.AppendCookie("username", UserName) '用户名和openid存储在Cookie中
e.AppendCookie("openid", OpenID)
' e.AppendCookie("KHID", KHID)
' MsgBox("OpenID=" & OpenID & ",UserName=" & UserName) '----------------------------
Else
e.WriteString(jo.ToString) '在用户浏览器显示错误信息
' msgbox("openid空的", 64, "提示")
Return
End If
Else
UserName = e.Cookies("username") '从cookie获取用户名和openid
OpenID = e.Cookies("openid")
' KHID = e.Cookies("KHID")
Dim dr As DataRow = DataTables("客户").SQLFind("openid ='" & Openid & "'") '根据openid找出对应的行
MsgBox("tjr= " & (e.GetValues.ContainsKey("tjr")))
MsgBox("dr = " & (dr Is Nothing)) '-------------------------------------------------------------
If (dr Is Nothing OrElse UserName = "" OrElse OpenID = "") AndAlso e.GetValues.ContainsKey("tjr") Then '空
Dim url As String = e.Request.URL.ToString
MsgBox(url)
'看官方文档state仅支持字母和数字,不支持中文和特殊符号,而redirect_uri是支持有参数的
Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx88888888888888&redirect_uri=" & UrlEncode(url) & "&response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect"
MsgBox(ul) '--------------------------------------------------------------------------------------------
sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(sb.ToString)
' msgbox("跳转授权链接", 64, "提示")
Return
End If
End If
……