using LitJson; using Sog; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Json; using System.Text; using System.Threading.Tasks; namespace Chat { public static class WechatCheckSvc { private static string accessToken = null; private static long expireTimeSec = 0; public static void CheckFreshAccessToken(bool force = false) { long now = ChatServerUtils.GetTimeSecond(); if(now > expireTimeSec || force) { //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET string url = "https://api.weixin.qq.com/cgi-bin/stable_token"; LitJson.JsonData j = new JsonData(); j["grant_type"] = "client_credential"; j["appid"] = ChatServerUtils.GetServerConfig().wxAppId; j["secret"] = ChatServerUtils.GetServerConfig().wxSecret; j["force_refresh"] = false; var cont = j.ToJson(); HttpContent content = new StringContent(cont, Encoding.UTF8, "application/json"); string ret = HttpUtils.HttpPost(url, content, out bool exception); TraceLog.Debug("WechatCheckSvc.checkFreshAccessToken, get ret:{0}", ret); if (ret == null) { return ; } LitJson.JsonData jsonData = JsonMapper.ToObject(ret); //if (jsonData.ContainsKey("errcode")) //{ // LitJson.JsonData errCode = jsonData["errcode"]; // LitJson.JsonData errMsg = jsonData["errmsg"]; // if (errCode.ToString() != "0") // { // TraceLog.Error("checkFreshAccessToken , msdk server ack failed, accountid:{0} errCode {1} errMsg:{2}", 0, errCode.ToString(), errMsg.ToString()); // return ; // } //} accessToken = jsonData["access_token"].ToString(); expireTimeSec = now + ((long)jsonData["expires_in"]); } } public static bool CheckDirtyName(string name) { CheckFreshAccessToken(); string url = "https://api.weixin.qq.com/wxa/game/content_spam/msg_sec_check?access_token="+accessToken; var allParams = new Dictionary(); allParams["scene"] = "1"; allParams["version"] = "2"; allParams["content"] = name; var content = new FormUrlEncodedContent(allParams); string ret = HttpUtils.HttpPost(url, content, out bool exception); TraceLog.Trace("WechatCheckSvc.CheckDirtyName, get ret:{0}", ret); if (ret == null) { return false; } LitJson.JsonData jsonData = JsonMapper.ToObject(ret); if (jsonData.ContainsKey("errcode")) { LitJson.JsonData errCode = jsonData["errcode"]; LitJson.JsonData errMsg = jsonData["errmsg"]; if (errCode.ToString() != "0") { int code = int.Parse(errCode.ToString()); if(code == 40001)//access_token 无效或不为最新获取的 access_token,请开发者确认access_token的有效性 { CheckFreshAccessToken(true); return CheckDirtyName(name); } TraceLog.Error("CheckDirtyName , msdk server ack failed, accountid:{0} errCode {1} errMsg:{2}", 0, errCode.ToString(), errMsg.ToString()); return false; } LitJson.JsonData res = jsonData["result"]; if (res.ContainsKey("suggest") && res["suggest"].ToString() == "pass") { return true; } } return false; } } }