Ejemplo n.º 1
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参数错误\"}");
            }
        }