Beispiel #1
0
        //得到ticket 如果文件里时间 超时则重新获取

        /*
         * private string getJsApiTicket()
         * {
         *  string ticket = string.Empty;
         *  //这里我从数据库读取
         *  //  DT = DbSession.Default.FromSql("select jsapi_ticket,ticket_expires from table where ID=1").ToDataTable();
         *  //  int expire_time = (int)DT.Rows[0]["ticket_expires"];
         *  // string ticket = DT.Rows[0]["jsapi_ticket"].ToString();
         *  int expire_time = ShopConfig.ReadConfigInfo().ExpireTime;
         *  ticket = ShopConfig.ReadConfigInfo().Ticket;
         *
         *  string accessToken = GetAccessToken(appId,appSecret);//获取系统的全局token
         *  if (expire_time < ConvertDateTimeInt(DateTime.Now))
         *  {
         *      string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" + accessToken + "";
         *      Jsapi api = JsonConvert.DeserializeObject<Jsapi>(httpGet(url).Trim());
         *      ticket = api.ticket;
         *      if (ticket != "")
         *      {
         *          expire_time = ConvertDateTimeInt(DateTime.Now) + 7000;
         *          //存入数据库操作
         *
         *          ShopConfigInfo config = ShopConfig.ReadConfigInfo();
         *          config.Ticket = ticket;
         *          config.ExpireTime = expire_time;
         *
         *          ShopConfig.UpdateConfigInfo(config);
         *      }
         *  }
         *  return ticket;
         * }
         * */
        /// <summary>
        /// 根据当前日期 判断Access_Token 是否超期 如果超期返回新的Access_Token 否则返回之前的Access_Token
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string IsExistAccess_Token()
        {
            string   Token = string.Empty;
            DateTime YouXRQ;
            // 读取XML文件中的数据,并显示出来 ,注意文件路径
            string filepath      = System.Web.HttpContext.Current.Server.MapPath(@"/Config/access_token.xml");
            string errorfilepath = System.Web.HttpContext.Current.Server.MapPath(@"/Config/error.xml");

            StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
            XmlDocument  xml = new XmlDocument();

            xml.Load(str);
            str.Close();
            str.Dispose();

            StreamReader errorstr = new StreamReader(errorfilepath, System.Text.Encoding.UTF8);
            XmlDocument  errorXml = new XmlDocument();

            errorXml.Load(errorstr);
            errorstr.Close();
            errorstr.Dispose();

            Token  = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
            YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);

            errorXml.SelectSingleNode("error").SelectSingleNode("errorstr").InnerText = "date later(" + DateTime.Now.AddMinutes(-2) + ") than now(" + YouXRQ + ")";
            if (DateTime.Now.AddMinutes(-2) > YouXRQ)
            {
                DateTime     _youxrq = DateTime.Now;
                Access_token mode    = GetAccess_token();

                XmlElement node = errorXml.CreateElement("access_token");
                node.InnerText = mode.access_token;
                errorXml.SelectSingleNode("error").AppendChild(node);
                if (!string.IsNullOrEmpty(mode.access_token))
                {
                    xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
                    _youxrq = _youxrq.AddSeconds(mode.expires_in);
                    xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
                    xml.Save(filepath);
                    Token = mode.access_token;
                }
            }
            errorXml.Save(errorfilepath);
            return(Token);
        }
Beispiel #2
0
        public static Access_token GetAccess_token()
        {
            string       url    = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
            string       result = httpGet(url);
            Access_token mode   = new Access_token();

            if (!string.IsNullOrEmpty(result))
            {
                JsonData jd = JsonMapper.ToObject(result);

                if (!((IDictionary)jd).Contains("errmsg"))
                {
                    mode.access_token = (string)jd["access_token"];
                    mode.expires_in   = (int)jd["expires_in"];
                }
            }
            return(mode);
        }