/// <summary> /// 获取用于JS-SDK的相关参数列表(该方法对accessToken和JSTicket都进行了指定时间的缓存处理,多次调用不会重复生成) /// 集合里面包括jsapi_ticket、noncestr、timestamp、url、signature、appid、rawstring /// </summary> /// <param name="appid">应用ID</param> /// <param name="Longcode">长期授权码</param> /// <param name="url">页面URL</param> /// <returns></returns> public Hashtable GetJSAPI_Parameters(string appid, string Longcode, string token, DateTime expressYxq, string url) { log.AppenLog("GetJSAPI_Parameters"); //string accessToken = AccessTokenHelper.IsExistAccess_Token(); string accessToken = AccessTokenHelper.GetQiye(Longcode, appid, token, expressYxq); log.AppenLog("accessToken1:" + accessToken); if (accessToken.Contains("expires_in")) { //跟环数据库中的token和时间 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(QiYeaccess_token)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(accessToken)); QiYeaccess_token obj = (QiYeaccess_token)ser.ReadObject(ms); string sqlstr2 = string.Format($"exec proc_Weixin 'UpdateAtoken','','','','','{appid}','','{obj.access_token}','{DateTime.Now.AddSeconds(int.Parse(obj.expires_in)).ToString("yyyy-MM-dd HH:mm:ss")}'"); string info2 = sql.EditDataCommand(sqlstr2); accessToken = obj.access_token; } log.AppenLog("accessToken:" + accessToken); Tuple <bool, string> t = GetJSAPI_Ticket(accessToken); log.AppenLog("t.Item2 " + t.Item1 + ":" + t.Item2); //TextHelper.Fun_WriteTxt($"appid:{appid}\r\nappSecret:{appSecret}\r\nurl:{url}\r\naccessToken:{accessToken}\r\njsTicket:{t.Item2}"); // WriteLog("jsTicket:" + t.Item2); return(GetParameters(appid, t.Item2, url)); }
/// <summary> /// 得到企业token /// </summary> /// <param name="compid">加密后的公司id</param> /// <returns></returns> public static string GetQiyeAttoken(string compid) { compid = Base64MIMA.JIE(compid); string sqlstr = string.Format($"select * from Company where CompanyID='{compid}'"); DataTable dt = sql.GetDataTableCommand(sqlstr); string accecctoken = AccessTokenHelper.GetQiye(dt.Rows[0]["Longcode"].ToString(), compid, dt.Rows[0]["attoken"].ToString(), DateTime.Parse(dt.Rows[0]["expressYxq"].ToString())); QiYeaccess_token obj = new QiYeaccess_token(); if (accecctoken.Contains("access_token")) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(QiYeaccess_token)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(accecctoken)); obj = (QiYeaccess_token)ser.ReadObject(ms); string sqlstr2 = string.Format($"exec proc_Weixin 'UpdateAtoken','','','','','{compid}','','{obj.access_token}','{DateTime.Now.AddSeconds(int.Parse(obj.expires_in)).ToString("yyyy-MM-dd HH:mm:ss")}'"); string info2 = sql.EditDataCommand(sqlstr2); accecctoken = obj.access_token; } return(accecctoken); }
public Image GetImage(string imgServerId, string compid) { log.AppenLog("获取图片"); //根据公司得到公司的长期授权码 string sqlstr = string.Format($"select * from Company where CompanyID='{compid}'"); DataTable dt = sql.GetDataTableCommand(sqlstr); string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", AccessTokenHelper.GetQiye(dt.Rows[0]["Longcode"].ToString(), compid, dt.Rows[0]["attoken"].ToString(), DateTime.Parse(dt.Rows[0]["expressYxq"].ToString())), imgServerId); try { // 1.创建httpWebRequest对象 WebRequest webRequest = WebRequest.Create(url); //WriteLog("tryurl:" + url); HttpWebRequest httpRequest = webRequest as HttpWebRequest; //WriteLog("try2"); // 2.填充httpWebRequest的基本信息 httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Method = "get"; //WriteLog("try3"); Stream responseStream = httpRequest.GetResponse().GetResponseStream(); //WriteLog("try5"); //WriteLog("responseStream" + responseStream); Image img = Image.FromStream(responseStream); //WriteLog("try4"); responseStream.Close(); return(img); } catch (Exception ex) { log.AppenLog("获取图片时出错:" + ex.Message); throw ex; } }