老师我把代码加在红字位置了,没加 Dim Verified As Boolean 在红字位置就不走不下去了
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, "w。。。。。。。。", "f...................", 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")
' msgbox("获取openid", 64, "提示")
Dim dr As DataRow = DataTables("客户").SQLFind("openid ='" & Openid & "'")
If dr IsNot Nothing Then
UserName = dr("nickname")
' msgbox("昵称", 64, "提示")
Else
msgbox("要增行网页ul", 64, "提示")
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)
msgbox("判断openid", 64, "提示")
If jo("openid") IsNot Nothing Then
UserName = jo("nickname")
msgbox("增行前", 64, "提示")
dr = DataTables("客户").AddNew()
msgbox("增行后", 64, "提示")
Dim nms() As String = {"openid", "nickname", "headimgurl", "性别"} '"", "客户住址_县市", "country", "客户住址_省市"} '""
For Each nm As String In nms
dr(nm) = jo(nm)
' msgbox("提取数据", 64, "提示")
Next
msgbox("判断tjkhid", 64, "提示")
If OpenId > "" AndAlso e.GetValues.ContainsKey("khid") Then 'e.GetValues.ContainsKey("khid")获取二维码推荐人的khid
msgbox("写入推荐人", 64, "提示")
dr("客户来源_KHID") = e.GetValues("khid") 'e.GetValues.ContainsKey("khid")获取二维码推荐人的TJkhid ,保存khid
msgbox("保存", 64, "提示")
dr.Save
End If
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("存缓存", 64, "提示")
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")
' msgbox("获取缓存", 64, "提示")
Dim dr As DataRow = DataTables("客户").SQLFind("openid ='" & Openid & "'") '根据openid找出对应的行
If dr Is Nothing OrElse UserName = "" OrElse OpenId = "" Then '空
' msgbox("判断名 openid 行 空", 64, "提示")
' If userName = "" OrElse OpenID = "" Then
Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx................&redirect_uri=http%3a%2f%2fwx.....cn&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(sb.ToString)
' msgbox("跳转授权链接", 64, "提示")
Return
End If
End If