/// <summary> /// 获取个人小程序码图片 /// </summary> /// <param name="path">小程序码跳转的路径</param> /// <param name="session">用户session</param> /// <returns></returns> public string GetUserQRCode(string path, string session) { HF.Cloud.BLL.Common.Logger.Error("GetUserQRCode方法,参数path:" + path + "------session:" + session); string imgPath = ""; //判断数据库中是否有值 SB_UserEL userEL = GetUserELBySession(session); //如果QRCode有值,则返回数据库中QRCode的值 if (!string.IsNullOrEmpty(userEL.QRCode) && userEL.QRCode != "") { //获取图片名称 string qrCodeImageName = userEL.QRCode; //获取图片路径 string qrCodeImagePath = System.Configuration.ConfigurationManager.AppSettings["QRCodeGet_User"]; imgPath = qrCodeImagePath + qrCodeImageName; } //数据库中QRCode没有数据,则需要获取小程序码图片保存图片,并把图片名称保存到数据库中 else { //获取token WX_TokenBLL tokenBLL = new WX_TokenBLL(); string token = tokenBLL.GetToken(); //获取小程序码接口 string url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + token; Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("path", path); JavaScriptSerializer js = new JavaScriptSerializer(); js.MaxJsonLength = int.MaxValue; string json = js.Serialize(dic); HF.Cloud.BLL.Common.Logger.Error("GetUserQRCode方法,获取小程序码的json:" + json); HF.Cloud.BLL.Common.Logger.Error("GetUserQRCode方法,url:" + url + "-----Path:" + path); //post返回的小程序码流 Stream QRCodeStream = WeChatAppDecrypt.Post(url, json); //将图片流转换成图片 Bitmap tp = new Bitmap(QRCodeStream); string QRCodeSave_User = System.Configuration.ConfigurationManager.AppSettings["QRCodeSave_User"]; string image_userName = Guid.NewGuid().ToString(); string qrCodeImageName = image_userName + ".jpg"; HF.Cloud.BLL.Common.Logger.Error("GetUserQRCode方法,保存小程序图片路径名称:" + QRCodeSave_User + qrCodeImageName); tp.Save(QRCodeSave_User + qrCodeImageName); //把小程序码图片名称保存到数据库中 userEL.QRCode = qrCodeImageName; int ra; long returnValue = userEL.ExecNonQuery(27, out ra); HF.Cloud.BLL.Common.Logger.Error("GetUserQRCode方法,保存小程序图片名称结果:" + ra); if (ra > 0) { string QRCodeGet_User = System.Configuration.ConfigurationManager.AppSettings["QRCodeGet_User"]; imgPath = QRCodeGet_User + qrCodeImageName; } else { imgPath = "error"; } } HF.Cloud.BLL.Common.Logger.Error("GetUserQRCode方法,返回的结果:" + imgPath); return(imgPath); }
/// <summary> /// 通过code获取session /// </summary> /// <param name="code">小程序login获取的Code</param> /// <returns></returns> public string GetSessionByCode(string code) { string strJson = ""; BLL.Common.Logger.Error("GetSessionByCode方法接受到的参数code:" + code); JavaScriptSerializer js = new JavaScriptSerializer(); Dictionary <string, object> dic = new Dictionary <string, object>(); string str_OpenidAndSessinKey = ""; if (!string.IsNullOrEmpty(code)) { //获取openid和Session_Key WeChatAppDecrypt Wechat = new WeChatAppDecrypt(); str_OpenidAndSessinKey = Wechat.GetOpenIdAndSessionKeyString(code); BLL.Common.Logger.Error("GetSessionByCode方法获取到openid和Sessionkey:" + str_OpenidAndSessinKey); var openidAndSessionKey = js.Deserialize <Dictionary <string, object> >(str_OpenidAndSessinKey); string strOpenID = openidAndSessionKey["openid"].ToString(); string strSession_Key = openidAndSessionKey["session_key"].ToString(); //通过openID获取用户记录 SB_UserEL userEL = new SB_UserEL(); userEL.OpenID = strOpenID; DataTable dt = userEL.ExecDT(43); string sessionResult = ""; string isHadRegister = ""; if (dt != null && dt.Rows.Count > 0) { sessionResult = dt.Rows[0]["Session_True"].ToString(); isHadRegister = "1"; } else { //生成session //生成6位随机数 string strRandom = new Random().Next(100000, 1000000).ToString(); //session_Key格式:q/jk63En5ojUGqgi6vLHmA==(24个字符) //openID格式:oFQX10O9zkRnsNYefrc48KfQWi9o(28个字符) //strSession_true=strSession_Key+strOpenID+strRandom //session等于sesion_key去掉后两位的“=”号(22位),加openid(28位),加六位随机数(6位) //sessionResult=q/jk63En5ojUGqgi6vLHmAoFQX10O9zkRnsNYefrc48KfQWi9o654321 string strSession_true = strSession_Key.Substring(0, strSession_Key.Length - 2) + strOpenID + strRandom; sessionResult = strSession_true; isHadRegister = "0"; } dic.Add("Session", sessionResult); dic.Add("IsHadRegister", isHadRegister); strJson = js.Serialize(dic); HF.Cloud.BLL.Common.Logger.Error("GetSessionByCode方法返回的数据为:" + strJson); } else { strJson = "error"; } return(strJson); }
/// <summary> /// 获取小程序的token /// </summary> /// <returns></returns> public string GetToken() { string token = string.Empty; //1,查看数据库中是否有未过期的token,有的话就用此token WX_TokenEL wx_TokenEL = new WX_TokenEL(); DataTable dt = wx_TokenEL.ExecDT(3); HF.Cloud.BLL.Common.Logger.Error("GetToken方法查询表结果:" + dt.Rows.Count); if (dt.Rows.Count > 0) { string tokenStr = dt.Rows[0]["Token"].ToString(); HF.Cloud.BLL.Common.Logger.Error("GetToken方法,表中获取的token:" + tokenStr); Dictionary <string, object> json_Token = new Dictionary <string, object>(); JavaScriptSerializer js_Token = new JavaScriptSerializer(); js_Token.MaxJsonLength = int.MaxValue; json_Token = js_Token.Deserialize <Dictionary <string, object> >(tokenStr); if (json_Token.Keys.Contains <string>("access_token")) { token = json_Token["access_token"].ToString(); } } //2,数据库中没有未过期的token,则从新请求小程序服务器获取最新token else { string appId = System.Configuration.ConfigurationManager.AppSettings["AppId"]; string appSecret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"]; string url_Str = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + appId + "&secret=" + appSecret; WeChatAppDecrypt wcad = new WeChatAppDecrypt(); string tokenStr = wcad.GetResponse(url_Str); HF.Cloud.BLL.Common.Logger.Error("GetToken方法,接口中获取的token:" + tokenStr); token = GetValueByKey(tokenStr, "access_token"); //2.1,把当前的token表记录设置为无效 int ra; long returnValue_Up = wx_TokenEL.ExecNonQuery(2, out ra); //2.2,把最新的token添加到数据库中 wx_TokenEL.Token = tokenStr; wx_TokenEL.CreateTime = DateTime.Now.ToString(); wx_TokenEL.Valid = 1; long returnValue_Add = wx_TokenEL.ExecNonQuery(1, out ra); } HF.Cloud.BLL.Common.Logger.Error("GetToken方法获取token值:" + token); return(token); }
/// <summary> /// 获取群组小程序码图片路径 /// </summary> /// <param name="path">小程序码跳转路径</param> /// <param name="groupID">群组ID</param> /// <returns></returns> public string GetQRCode_Group(string path, string groupID) { HF.Cloud.BLL.Common.Logger.Error("GetQRCode_Group方法,获取的参数path:" + path + "------groupID:" + groupID); string imgPath = ""; //判断数据库中是否有值 GroupEL groupEL = new GroupEL(); groupEL.ID = long.Parse(groupID); DataTable dt = groupEL.ExecDT(3); HF.Cloud.BLL.Common.Logger.Error("GetQRCode_Group方法,查找群组记录:" + dt.Rows.Count); if (dt != null && dt.Rows.Count > 0) { //如果数据库中"QRCode"有值,则从数据库中取值 if (!string.IsNullOrEmpty(dt.Rows[0]["QRCode"].ToString()) && dt.Rows[0]["QRCode"].ToString() != "") { //获取图片名称 string qrCodeImageName = dt.Rows[0]["QRCode"].ToString(); //获取图片路径 string qrCodeImagePath = System.Configuration.ConfigurationManager.AppSettings["QRCodeGet_Group"]; imgPath = qrCodeImagePath + qrCodeImageName; } //如果没有值则请求小程序服务器获取小程序码图片,并把图片名称保存到数据库中 else { //获取token WX_TokenBLL tokenBLL = new WX_TokenBLL(); string token = tokenBLL.GetToken(); //获取小程序码接口 string url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + token; Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("path", path); JavaScriptSerializer js = new JavaScriptSerializer(); js.MaxJsonLength = int.MaxValue; string json = js.Serialize(dic); HF.Cloud.BLL.Common.Logger.Error("GetQRCode_Group方法,url:" + url + "-----Path:" + path); //post返回的小程序码流 Stream QRCodeStream = WeChatAppDecrypt.Post(url, json); //将图片流转换成图片 Bitmap tp = new Bitmap(QRCodeStream); string QRCodeSave_Group = System.Configuration.ConfigurationManager.AppSettings["QRCodeSave_Group"]; string image_userName = Guid.NewGuid().ToString(); string qrCodeImageName = image_userName + ".jpg"; tp.Save(QRCodeSave_Group + qrCodeImageName); //把小程序码图片名称保存到数据库中 groupEL.QRCode = qrCodeImageName; int ra; long returnValue = groupEL.ExecNonQuery(21, out ra); HF.Cloud.BLL.Common.Logger.Error("GetQRCode_Group方法,保存小程序图片名称结果:" + ra); if (ra > 0) { string QRCodeGet_Group = System.Configuration.ConfigurationManager.AppSettings["QRCodeGet_Group"]; imgPath = QRCodeGet_Group + qrCodeImageName; } else { imgPath = "error"; } } } else { imgPath = "error"; } HF.Cloud.BLL.Common.Logger.Error("GetQRCode_Group方法,小程序码图片路径:" + imgPath); return(imgPath); }