Esempio n. 1
0
        /// <summary>
        /// 拼接图文消息素材Json字符串
        /// </summary>
        public static string GetArticlesJsonStr(PageBase page, string access_token, DataTable dt)
        {
            StringBuilder sbArticlesJson = new StringBuilder();

            sbArticlesJson.Append("{\"articles\":[");
            int i = 0;

            foreach (DataRow dr in dt.Rows)
            {
                string path = page.MapPath(dr["ImgUrl"].ToString());
                if (!File.Exists(path))
                {
                    return("{\"code\":0,\"msg\":\"要发送的图片不存在\"}");
                }
                string msg      = WXApi.UploadMedia(access_token, "image", path); // 上图片返回媒体ID
                string media_id = Tools.GetJsonValue(msg, "media_id");
                sbArticlesJson.Append("{");
                sbArticlesJson.Append("\"thumb_media_id\":\"" + media_id + "\",");
                sbArticlesJson.Append("\"author\":\"" + dr["Author"].ToString() + "\",");
                sbArticlesJson.Append("\"title\":\"" + dr["Title"].ToString() + "\",");
                sbArticlesJson.Append("\"content_source_url\":\"" + dr["TextUrl"].ToString() + "\",");
                sbArticlesJson.Append("\"content\":\"" + dr["Content"].ToString() + "\",");
                sbArticlesJson.Append("\"digest\":\"" + dr["Content"].ToString() + "\",");
                if (i == dt.Rows.Count - 1)
                {
                    sbArticlesJson.Append("\"show_cover_pic\":\"1\"}");
                }
                else
                {
                    sbArticlesJson.Append("\"show_cover_pic\":\"1\"},");
                }
                i++;
            }
            sbArticlesJson.Append("]}");

            return(sbArticlesJson.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// 群发
        /// </summary>
        public string Send()
        {
            string type = Request["type"];
            string data = Request["data"];

            string        access_token = AdminUtil.GetAccessToken(this); //获取access_token
            List <string> openidList   = WXApi.GetOpenIDs(access_token); //获取关注者OpenID列表
            UserInfo      loginUser    = AdminUtil.GetLoginUser(this);   //当前登录用户

            string resultMsg = null;

            //发送文本
            if (type == "1")
            {
                resultMsg = WXApi.Send(access_token, WXMsgUtil.CreateTextJson(data, openidList));
            }

            //发送图片
            if (type == "2")
            {
                string path = MapPath(data);
                if (!File.Exists(path))
                {
                    return("{\"code\":0,\"msg\":\"要发送的图片不存在\"}");
                }
                string msg      = WXApi.UploadMedia(access_token, "image", path);
                string media_id = Tools.GetJsonValue(msg, "media_id");
                resultMsg = WXApi.Send(access_token, WXMsgUtil.CreateImageJson(media_id, openidList));
            }

            //发送图文消息
            if (type == "3")
            {
                DataTable dt           = ImgItemDal.GetImgItemTable(loginUser.OrgID, data);
                string    articlesJson = ImgItemDal.GetArticlesJsonStr(this, access_token, dt);
                string    newsMsg      = WXApi.UploadNews(access_token, articlesJson);
                string    newsid       = Tools.GetJsonValue(newsMsg, "media_id");
                resultMsg = WXApi.Send(access_token, WXMsgUtil.CreateNewsJson(newsid, openidList));
            }

            //结果处理
            if (!string.IsNullOrWhiteSpace(resultMsg))
            {
                string errcode = Tools.GetJsonValue(resultMsg, "errcode");
                string errmsg  = Tools.GetJsonValue(resultMsg, "errmsg");
                if (errcode == "0")
                {
                    return("{\"code\":1,\"msg\":\"\"}");
                }
                else
                {
                    return("{\"code\":0,\"msg\":\"errcode:"
                           + errcode + ", errmsg:"
                           + errmsg + "\"}");
                }
            }
            else
            {
                return("{\"code\":0,\"msg\":\"type参数错误\"}");
            }
        }