Ejemplo n.º 1
0
        //微信群发
        public static void SendAll()
        {
            //获取待群发的文章
            AbstractDbHelper            DbHelper    = CreateDb();
            string                      strsql      = @"SELECT top 10 a.ID,a.title,a.linkurl FROM ews_ArticleList a
                                WHERE isshow=1 AND wxflag=0 order by createdate";
            DataTable                   dtAL        = DbHelper.GetDataTable(strsql);
            Dictionary <string, string> articleList = new Dictionary <string, string>();

            for (int i = 0; i < dtAL.Rows.Count; i++)
            {
                articleList.Add(dtAL.Rows[i]["title"].ToString(), dtAL.Rows[i]["linkurl"].ToString());
                strsql = @"UPDATE ews_ArticleList SET wxflag=1 WHERE ID=" + dtAL.Rows[i]["ID"].ToString();
                DbHelper.DoCommand(strsql);
            }
            //微信获取access_token
            string         wxurl_getaccecsstoken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
            string         retJson = HttpHelper.GetResponseString(HttpHelper.CreateGetHttpResponse(wxurl_getaccecsstoken));
            wx_accesstoken wx_at   = Newtonsoft.Json.JsonConvert.DeserializeObject <wx_accesstoken>(retJson);
            //微信群发(需要认证)
            string wxurl_sendall = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + wx_at.access_token;
            string tpl           = ContextTemplate(contextType.群发, articleList);
            string text          = @"{
                           \""filter\"":{
                              \""is_to_all\"":false,
                              \""tag_id\"":2
                           },
                           \""text\"":{
                              \""content\"":\""" + tpl + @"\""
                           },
                            \""msgtype\"":\""text\""
                        }";

            retJson = HttpHelper.GetResponseString(HttpHelper.CreatePostHttpResponse(wxurl_sendall, text));
            miniCoreFrame.Common.Log.Info(retJson);
        }