Exemple #1
0
        public static void refresh_user_authorizer_access_token(string authorizer_appid)
        {
            WeserviceEntities entity = new WeserviceEntities();
            var list = entity.dt_wx_auth_info.Where(s => s.authorizer_appid == authorizer_appid).ToList();

            if (list.Count > 0)
            {
                var    item = list[0];
                double ts   = DateTime.Now.Subtract((DateTime)item.last_time).TotalSeconds;
                if (ts > (item.expires_in - 600))//已过期
                {
                    try
                    {
                        string url  = "https:// api.weixin.qq.com /cgi-bin/component/api_authorizer_token?component_access_token=" + get_component_access_token();
                        string json = "{\"component_appid\":\"wx24f0419c030f897a\",\"authorizer_appid\":" +
                                      "\"" + authorizer_appid + "\",\"authorizer_refresh_token\":\"" + item.authorizer_refresh_token + "\"}";
                        AuthResult ar = Post.PostGetJson <AuthResult>(url, null, new MemoryStream(System.Text.Encoding.Default.GetBytes(json)));
                        item.authorizer_access_token = ar.authorization_info.authorizer_access_token;
                        item.expires_in = ar.authorization_info.expires_in;
                        item.authorizer_refresh_token = ar.authorization_info.authorizer_refresh_token;
                    }
                    catch (Exception e)
                    {
                        LogHelper.WriteException(e);
                    }
                }
            }
            int count = entity.SaveChanges();

            LogHelper.WriteInfo("refresh_user_authorizer_access_token => count5 : " + count);
        }
Exemple #2
0
        public static void update_auth_info(AuthResult ar)
        {
            WeserviceEntities entity = new WeserviceEntities();
            var list = entity.dt_wx_auth_info.Where(s => s.authorizer_appid == ar.authorization_info.authorizer_appid).ToList();

            if (list.Count == 0)
            {
                var item = new dt_wx_auth_info
                {
                    authorizer_appid         = ar.authorization_info.authorizer_appid,
                    authorizer_access_token  = ar.authorization_info.authorizer_access_token,
                    authorizer_refresh_token = ar.authorization_info.authorizer_refresh_token,
                    expires_in = ar.authorization_info.expires_in,
                    last_time  = DateTime.Now,
                    func_info  = Tools.JsonToString(ar.authorization_info.func_info)
                };
                entity.dt_wx_auth_info.Add(item);
            }
            else
            {
                var item = list[0];
                item.authorizer_access_token  = ar.authorization_info.authorizer_access_token;
                item.authorizer_refresh_token = ar.authorization_info.authorizer_refresh_token;
                item.expires_in = ar.authorization_info.expires_in;
                item.last_time  = DateTime.Now;
                item.func_info  = Tools.JsonToString(ar.authorization_info.func_info);
            }
            int count = entity.SaveChanges();

            LogHelper.WriteInfo("update_auth_info => count4 : " + count);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }