If e.host = "www.soft.net" Then '
Dim UserNamewx As String
Dim openid As String
Dim appid As String
Dim secret As String
Dim zhangtao As String
If e.GetValues.ContainsKey("code") And e.GetValues("state") = "quanzhou" Then '如果是通过授权链接跳转而来,就从链接重提取code来获取openid
appid = "wx99..."
secret = "d7df..."
End If
If e.GetValues.ContainsKey("code") And e.GetValues("state") = "shandong" Then '如果是通过授权链接跳转而来,就从链接重提取code来获取openid
appid = "wx89"
secret = "bd8ca"
End If
If e.GetValues.ContainsKey("code") And e.GetValues("state") = "sichuan" Then '如果是通过授权链接跳转而来,就从链接重提取code来获取openid
appid = "wx005d"
secret = "3c1b8b"
End If
If e.GetValues.ContainsKey("code") And e.GetValues("state") = "shanxi" Then '如果是通过授权链接跳转而来,就从链接重提取code来获取openid
appid = "wx686ef"
secret = "07b"
End If
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,appid,secret,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 drwx As DataRow = DataTables("WXUsers").sqlFind("openid ='" & openid & "'")
If drwx IsNot Nothing Then
' UserNamewx = drwx("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
UserNamewx = jo("nickname")
drwx = DataTables("WXUsers").AddNew()
Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl"} '""
For Each nm As String In nms
drwx(nm) = jo(nm)
Next
drwx("addtime") = Date.now
drwx.Save
DataTables("WXUsers").loadfilter = "openid = ''"
DataTables("WXUsers").load
Else
e.WriteString(jo.ToString) '在用户浏览器显示错误信息
Return
End If
End If
' e.AppendCookie("usernamewx",UserNamewx) '用户名和openid存储在Cookie中
wb.AppendCookie("openid",openid)
e.AppendCookie("openid",openid)
e.Cookies.add("openid",openid)
messagebox.show(Date.now & openid)
Else
e.WriteString(jo.ToString) '在用户浏览器显示错误信息
Return
End If
e.WriteString(wb.Build) '生成网页
End If