using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PMS.Plugins.Common; using PMS.Plugins.PluginsModels; using QWPlatform.SystemLibrary.LogManager; using System; using System.Collections.Generic; using System.Configuration; namespace PMS.Plugins.WeChart { public class WeChartMessage { public static Logger Log = Logger.Instance; public static string AppID = ConfigurationManager.AppSettings["AppID"].ToStringEx();//AppID public static string AppSecret = ConfigurationManager.AppSettings["AppSecret"].ToStringEx();//AppSecret public static string IsWechatPush = ConfigurationManager.AppSettings["IsWechatPush"].ToStringEx();//是否推送消息 /// /// 获取微信用户信息 /// /// /// public static WeChatUserInfo WeChatUserInfo(string code) { var _weChatUserInfo = new WeChatUserInfo(); try { Log.Info("获取微信用户信息【0】用户code:" + code); var access_token_url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppID, AppSecret, code); var openid_result = WechatHelper.InvokeMedicalWebApiMethod(access_token_url); if (openid_result != null) { JObject jResult = JObject.Parse(openid_result); Logger.Instance.Info("获取微信用户信息【1】获取SNS:" + openid_result); JObject jObjectToken = JObject.Parse(openid_result); _weChatUserInfo.wx_openid = jObjectToken["openid"].ToString(); var get_userinfo = UpdateToken(_weChatUserInfo.wx_openid); var jump = false;//不用再次请求token JObject jObject = JObject.Parse(get_userinfo); try { if (jObject["errcode"].ToString() == "40001")//防止微信无缘无故失效 { jump = true; } } catch (Exception ex) { _weChatUserInfo.wx_nickname = jObject["nickname"].ToString(); _weChatUserInfo.sex = jObject["sex"].ToString(); _weChatUserInfo.headimgurl = jObject["headimgurl"].ToString(); ///正常返回会在里面抛出异常 } if (jump)//重新获取token,并获取信息 { get_userinfo = UpdateToken(_weChatUserInfo.wx_openid); jObject = JObject.Parse(get_userinfo); } if (jObject["subscribe"].ToString() == "0")//未关注公众号 { _weChatUserInfo.NoAttention = 0; } } } catch (Exception ex) { Logger.Instance.Error("可能未关注公众号+获取微信用户信息异常:", ex); _weChatUserInfo.NoAttention = 0; } return _weChatUserInfo; } /// /// 根据openid获取用户信息 /// /// /// public static dynamic UpdateToken(string wx_openid) { var wx_access_token = WechatHelper.GetAccessToken(AppID, AppSecret); var access_token_url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", wx_access_token.access_token, wx_openid); var get_userinfo = WechatHelper.InvokeMedicalWebApiMethod(access_token_url);//使用openid access_token 获取用户的信息[4] return get_userinfo; } /// /// 发送微信消息 /// /// /// public static string SendWeChartTempletMessge(WxMessgPush wxMessgPush) { string strReturn = string.Empty; if(IsWechatPush=="0") { return "推送关闭"; } try { var cacheData = WechatHelper.GetAccessToken(AppID, AppSecret); var wx_access_token = cacheData.access_token; //ar wx_access_token = "59_2K4nWDN2YxJKvllR120Bm-Hk70c8WNtbCdTJ78ymQyfjML_83RX35OwcQnedfIIs5ZKlKXWSwXh17_Nc7kxKCk9peN5RuFuWWFpSBL2-XHZ_ErUmN_d_oA8eOyYO6G2MlNTFtJRgTHkqYbTFKPPjAJAAFF"; string url = @"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + wx_access_token; string temp = "{\"touser\": \"" + wxMessgPush.wx_openid + "\"," + "\"url\": \"" + wxMessgPush.url + "\", " + "\"template_id\": \"" + wxMessgPush.template_id + "\", " + "\"topcolor\": \"#FF0000\", " + "\"data\": " + wxMessgPush.data + "}"; //核心代码 var state = WechatHelper.GetResponseData(temp, @url, "text/json"); Log.Info("微信公众号准备第一次推送消息【1】:" + url + "\r\n\r\n" + "参数:" + temp + "\r\n\r\n" + "返回结果:" + state); JObject jObject = JObject.Parse(state); var jump = false;//不用再次请求token try { if (jObject["errcode"].ToString() == "40001")//防止微信无缘无故失效 { jump = true; } } catch (Exception ex) { ///正常返回会在里面抛出异常 } if (jump)//重新获取token,并获取信息 { WxTickectInfo wx = new WxTickectInfo(); HashtableHelper.AddCacheIitems("GetAccessToken", wx);//当前时间加上7100秒 cacheData = WechatHelper.GetAccessToken(AppID, AppSecret); wx_access_token = cacheData.access_token; url = @"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + wx_access_token; state = WechatHelper.GetResponseData(temp, @url, "text/json"); Log.Info("微信公众号准备第二次推送消息【2】:" + @url + "\r\n\r\n" + "参数:" + temp + "\r\n\r\n" + "返回结果:" + state); } strReturn = "推送成功"; } catch (Exception ex) { strReturn = ex.Message; } return strReturn; } } }