/// <summary>获取在线人数(15分钟内活动的用户),加了缓存</summary> public static int GetOnlineCount() { return((int)Asp.GetCacheData( "OnlineCount", DateTime.Now.AddMinutes(5), () => { DateTime lastDt = DateTime.Now.AddMinutes(-15); return Set.Where(o => o.UpdateDt > lastDt).Count(); })); }
/// <summary>获取公众号调用微信JS接口的临时票据(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket)(未测试)</summary> static string GetJsSdkTicket(string accessToken) { var dt = DateTime.Now.AddMinutes(60); return(Asp.GetCacheData <string>("WechatJSAPITicket", dt, () => { var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", accessToken); var txt = HttpHelper.Get(url); return txt.ParseJObject()["ticket"].ToString(); })); }
/// <summary>获取微信网页授权登录AccessToken</summary> /// <remarks> /// https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842 /// 网页授权流程分为四步: /// 1、引导用户进入授权页面同意授权,获取code /// 2、通过code换取网页授权access_token(与基础支持中的access_token不同) /// 3、如果需要,开发者可以刷新网页授权access_token,避免过期 /// 4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制) /// </remarks> public static OAuthGetTokenReply OAuthGetAccessToken(string code) { var key = "WechatWebAccessToken"; var dt = DateTime.Now.AddMinutes(60); return(Asp.GetCacheData <OAuthGetTokenReply>(key, dt, () => { var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", WechatConfig.OpenAppId, WechatConfig.OpenAppSecret, code ); var reply = HttpHelper.Get(url); return reply.ParseJson <OAuthGetTokenReply>(); })); }