Esempio n. 1
0
        private static string get_component_access_token()
        {
            WeserviceEntities entity = new WeserviceEntities();
            var list = entity.dt_wx_token.Where(s => s.info_type == "component_verify_ticket").ToList();

            if (list.Count == 0)
            {
                return("");
            }
            var ticket_item = list[0];

            //刷新获取第三方平台component_access_token
            list = entity.dt_wx_token.Where(s => s.info_type == "component_access_token").ToList();
            string component_access_token = "";

            if (list.Count > 0)
            {
                var    token = list[0];
                double ts    = DateTime.Now.Subtract((DateTime)token.last_time).TotalSeconds;
                if (ts > (token.expires_in - 600) || token.token == null)//已过期
                {
                    //刷新token
                    TokenResult tr = refresh_component_access_token(ticket_item.token);
                    if (tr != null)
                    {
                        token.last_time  = DateTime.Now;
                        token.token      = tr.component_access_token;
                        token.expires_in = tr.expires_in;
                        token.appid      = ticket_item.appid;
                    }
                }
                component_access_token = token.token;
            }
            else
            {
                TokenResult tr = refresh_component_access_token(ticket_item.token);
                if (tr != null)
                {
                    var new_token = new dt_wx_token
                    {
                        info_type  = "component_access_token",
                        last_time  = DateTime.Now,
                        token      = tr.component_access_token,
                        expires_in = tr.expires_in,
                        appid      = ticket_item.appid
                    };
                    component_access_token = new_token.token;
                    entity.dt_wx_token.Add(new_token);
                }
            }
            int count = entity.SaveChanges();

            LogHelper.WriteInfo("update_ticket => count2 : " + count);
            return(component_access_token);
        }
Esempio n. 2
0
        public static void update_ticket(XDocument xdoc)
        {
            LogHelper.WriteInfo(xdoc.ToString());
            WeserviceEntities entity                = new WeserviceEntities();
            string            InfoType              = xdoc.Root.XPathSelectElement("//InfoType").Value;
            string            AppId                 = xdoc.Root.XPathSelectElement("//AppId").Value;
            string            CreateTime            = xdoc.Root.XPathSelectElement("//CreateTime").Value;
            string            ComponentVerifyTicket = "";

            if (InfoType == "component_verify_ticket")
            {
                ComponentVerifyTicket = xdoc.Root.XPathSelectElement("//ComponentVerifyTicket").Value;
            }
            var list = entity.dt_wx_token.Where(s => s.info_type == InfoType && s.appid == AppId).ToList();

            //有对应记录则更新
            if (list.Count > 0)
            {
                var item = list[0];
                item.last_time = Tools.GetTimeFromUnix(int.Parse(CreateTime));
                item.token     = ComponentVerifyTicket;
            }
            //没有则插入新记录
            else
            {
                dt_wx_token item = new dt_wx_token
                {
                    info_type = InfoType,
                    appid     = AppId,
                    last_time = Tools.GetTimeFromUnix(int.Parse(CreateTime)),
                    token     = ComponentVerifyTicket
                };
                entity.dt_wx_token.Add(item);
            }
            int count = entity.SaveChanges();

            LogHelper.WriteInfo("update_ticket => count1 : " + count);
        }