using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using LitJson; namespace HttpProxy { public class WeixinAuth { public static bool AuthToken(string accountid, string token, out bool exception) { //成功,打印一下日志 TraceLog.Debug("WeixinAuth.AuthToken, start accountid {0}", accountid); long timestamp = HttpProxyServerUtils.GetTimeSecond(); string msdkkey = HttpProxyServerUtils.GetServerConfig().msdkkey; string appid = HttpProxyServerUtils.GetServerConfig().qq_appid; string appkey = HttpProxyServerUtils.GetServerConfig().qq_appkey; string sig = MSDKApi.CalcSig(msdkkey, timestamp); string fullUrl = string.Format("{0}?timestamp={1}&appid={2}&sig={3}&openid={4}&encode=2" , MSDKApi.MSDKUrl + MSDKApi.WXAuthUrl , timestamp , appid , sig , accountid); List> contentParams = new List>(); contentParams.Add(new KeyValuePair("openid", accountid)); contentParams.Add(new KeyValuePair("accessToken", token)); string strRet = HttpUtils.HttpPost(fullUrl, contentParams, out exception); if (strRet == null) { TraceLog.Error("WeixinAuth.AuthToken, HttpGet return null query failed"); return false; } LitJson.JsonData jsonData = JsonMapper.ToObject(strRet); LitJson.JsonData ret = jsonData["ret"]; LitJson.JsonData msg = jsonData["msg"]; if (ret == null || msg == null) { TraceLog.Error("WeixinAuth.AuthToken, ret {0}, no ret or no msg", strRet); return false; } if (ret.ToString() != "0") { TraceLog.Error("WeixinAuth.AuthToken , msdk server ack failed, ret {0}", strRet); return false; } //成功,打印一下日志 TraceLog.Debug("WeixinAuth.AuthToken ,ret {0}, accountid {1}", strRet, accountid); return true; } public static string[] QueryUserInfo(string accountid, string token, out bool exception) { //成功,打印一下日志 TraceLog.Debug("WeixinAuth.QueryUserInfo, start accountid {0}", accountid); long timestamp = HttpProxyServerUtils.GetTimeSecond(); string msdkkey = HttpProxyServerUtils.GetServerConfig().msdkkey; string appid = HttpProxyServerUtils.GetServerConfig().qq_appid; string appkey = HttpProxyServerUtils.GetServerConfig().qq_appkey; string sig = MSDKApi.CalcSig(msdkkey, timestamp); string fullUrl = string.Format("{0}?timestamp={1}&appid={2}&sig={3}&openid={4}&encode=2" , MSDKApi.MSDKUrl + MSDKApi.wxprofile , timestamp , appid , sig , accountid); List> contentParams = new List>(); contentParams.Add(new KeyValuePair("appid", appid)); contentParams.Add(new KeyValuePair("openid", accountid)); contentParams.Add(new KeyValuePair("accessToken", token)); string strRet = HttpUtils.HttpPost(fullUrl, contentParams, out exception); if (strRet == null) { TraceLog.Error("WeixinAuth.QueryUserInfo, HttpGet return null query failed"); return null; } LitJson.JsonData jsonData = JsonMapper.ToObject(strRet); LitJson.JsonData ret = jsonData["ret"]; LitJson.JsonData msg = jsonData["msg"]; if (ret == null || msg == null) { TraceLog.Error("WeixinAuth.QueryUserInfo, ret {0}, no ret or no msg", strRet); return null; } if (ret.ToString() != "0") { TraceLog.Error("WeixinAuth.QueryUserInfo , msdk server ack failed, ret {0}", strRet); return null; } LitJson.JsonData nickName = jsonData["nickName"]; LitJson.JsonData sex = jsonData["sex"]; LitJson.JsonData picture = jsonData["picture"]; if (nickName == null || sex == null || picture == null) { TraceLog.Error("WeixinAuth.QueryUserInfo , nickName sex picture40 failed, ret {0}", strRet); return null; } string[] result = new string[4]; result[0] = nickName.ToString(); result[1] = sex.ToString(); result[2] = picture.ToString(); //成功,打印一下日志 TraceLog.Debug("WeixinAuth.QueryUserInfo ,ret {0}, accountid {1}", strRet, accountid); return result; } } }