using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using LitJson; using ProtoCSStruct; namespace HttpProxy { public class WechatMini { public static readonly string WXMini = "https://api.weixin.qq.com/"; public static readonly string WXMiniUrl = "sns/jscode2session"; public static string appkey = ""; public static bool AuthToken(out string openId, string code, ref SSHttpCheckAccountToken accountCheckToken, out bool exception) { //成功,打印一下日志 TraceLog.Debug("WechatMini.AuthToken, start code {0}", code); openId = ""; string appid = HttpProxyServerUtils.GetServerConfig().wechatMini_appid; string appkeyConfig = HttpProxyServerUtils.GetServerConfig().wechatMini_appsecreat; string fullUrl = string.Format("{0}?appid={1}&secret={2}&js_code={3}&grant_type={4}" , WXMini + WXMiniUrl , appid , appkeyConfig , code , "authorization_code"); string strRet = HttpUtils.HttpGet(fullUrl, out exception, null); TraceLog.Debug("WechatMini.AuthToken, get ret:{0}", strRet); if (strRet == null) { TraceLog.Error("WechatMini.AuthToken, HttpGet return null query failed"); return false; } LitJson.JsonData jsonData = JsonMapper.ToObject(strRet); if (jsonData.ContainsKey("errcode")) { LitJson.JsonData errCode = jsonData["errcode"]; LitJson.JsonData errMsg = jsonData["errmsg"]; if (errCode.ToString() != "0") { TraceLog.Error("WechatMini.AuthToken , msdk server ack failed, accountid:{0} errCode {1} errMsg:{2}", 0, errCode.ToString(), errMsg.ToString()); return false; } } LitJson.JsonData oid = jsonData["openid"]; LitJson.JsonData sessionKey = jsonData["session_key"]; //LitJson.JsonData unionId = jsonData["unionid"]; accountCheckToken.SessionKey.SetString(sessionKey.ToString()); accountCheckToken.OpenId.SetString(oid.ToString()); openId = oid.ToString(); //成功,打印一下日志 TraceLog.Debug("WeixinAuth.AuthToken ,ret {0}, openId {1}", strRet, openId); return true; } /// /// 获取微信头像等信息 /// /// /// /// /// public static string[] QueryUserInfo(string accountid, string token, out bool exception) { //成功,打印一下日志 TraceLog.Debug("WechatMini.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("WechatMini.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("WechatMini.QueryUserInfo, ret {0}, no ret or no msg", strRet); return null; } if (ret.ToString() != "0") { TraceLog.Error("WechatMini.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("WechatMini.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("WechatMini.QueryUserInfo ,ret {0}, accountid {1}", strRet, accountid); return result; } } }