Example #1
0
        /// <summary>
        /// 根据当前日期 判断Access_Token 是否超期  如果超期返回新的Access_Token,否则返回之前的Access_Token
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            var token          = string.Empty;
            var expirationTime = DateTime.Now;
            // 读取XML文件中的数据,并显示出来 ,注意文件路径

            var appid  = "";
            var secret = "";

            var filePath = Utils.GetMapPath("~/xmlconfig/WeChatMiniProgram.config");
            var doc      = new XmlDocument();

            doc.Load(filePath);
            appid  = doc.SelectSingleNode(@"Root/appid")?.InnerText;
            secret = doc.SelectSingleNode(@"Root/secret")?.InnerText;

            token = doc.SelectSingleNode(@"Root/Access_Token")?.InnerText;
            var expirationTimeString = doc.SelectSingleNode(@"Root/Access_ExpirationTime")?.InnerText;

            if (ValidateUtil.IsDateTime(expirationTimeString))
            {
                expirationTime = Convert.ToDateTime(expirationTimeString);
            }

            if (!string.IsNullOrEmpty(appid) && !string.IsNullOrEmpty(secret) && (string.IsNullOrEmpty(token) || DateTime.Now > expirationTime))
            {
                expirationTime = DateTime.Now;
                var strUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
                var model  = new AccessToken();

                var returnContent = HttpRequestUtil.HwGet(strUrl);

                var accessToken = JsonUtil.JsonToObject <AccessToken>(returnContent);
                model.access_token = accessToken.access_token;
                model.expires_in   = accessToken.expires_in;

                doc.SelectSingleNode(@"Root/Access_Token").InnerText = model.access_token;
                expirationTime = expirationTime.AddSeconds(int.Parse(model.expires_in));
                doc.SelectSingleNode(@"Root/Access_ExpirationTime").InnerText = expirationTime.ToString("yyyy-MM-dd HH:mm:ss:ffff");
                doc.Save(filePath);
                token = model.access_token;
            }
            return(token);
        }
Example #2
0
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <param name="dt">不要用UTC时间,直接用本地时间</param>
        /// <param name="precision">精度(秒:s,毫秒:ms)</param>
        /// <returns></returns>
        public static long GetTimeStamp(string dt, string precision = "s")
        {
            long currentTicks = DateTime.Now.ToUniversalTime().Ticks;

            if (ValidateUtil.IsDateTime(dt))
            {
                currentTicks = DateTime.Parse(dt).ToUniversalTime().Ticks;
            }
            if (precision.Equals("ms", StringComparison.OrdinalIgnoreCase))
            {
                //毫秒
                return((currentTicks - 621355968000000000) / 10000);
            }
            else if (precision.Equals("s", StringComparison.OrdinalIgnoreCase))
            {
                //秒
                return((currentTicks - 621355968000000000) / 10000000);
            }
            else
            {
                return((currentTicks - 621355968000000000) / 10000000);
            }
        }
Example #3
0
 /// <summary>
 /// 判断是否为合法日期,必须大于1800年1月1日
 /// </summary>
 /// <param name="dateTime">输入日期字符串</param>
 /// <returns>True/False</returns>
 public static bool IsDateTime(string dateTime)
 {
     return(ValidateUtil.IsDateTime(dateTime));
 }