Ejemplo n.º 1
0
        /// <summary>
        /// 批量为用户打标签
        /// </summary>
        /// <param name="openIDList"></param>
        /// <param name="tagid"></param>
        /// <returns></returns>
        public static bool batchtagging(List <string> openIDList, int tagid)
        {
            if (openIDList == null || openIDList.Count == 0 || tagid == 0)
            {
                return(false);
            }
            TagUserList tlist = new TagUserList();

            tlist.openid_list = openIDList;
            tlist.tagid       = tagid;
            try
            {
                string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
                string url          = string.Format("https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={0}", access_token);
                string posJson      = JsonHelper.SerializeObject(tlist);
                string resultJson   = HttpUtils.Ins.POST(url, posJson);
                Dictionary <string, object> reslut = JsonHelper.DeserializeObject(resultJson);
                if (reslut["errcode"].TryToInt(0) == 0)
                {
                    return(true);
                }
            } catch (Exception ex)
            {
                ClassLoger.Error("WeChatTagsAPI.batchtagging", ex);
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取模板ID
        /// </summary>
        /// <param name="template_id_short">模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式</param>
        /// <returns></returns>
        public static string GetTemplateID(string template_id_short)
        {
            string templateID = string.Empty;

            try
            {
                string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
                string url          = string.Format("https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token={0}", access_token);
                string json         = "{\"template_id_short\":\"" + template_id_short + "\"}";
                string resultJson   = HttpUtils.Ins.POST(url, json);
                Dictionary <string, object> reslut = JsonHelper.DeserializeObject(resultJson);
                if (reslut["errcode"].TryToInt(0) == 0)
                {
                    templateID = reslut["template_id"].TryToString();
                }
                else
                {
                    ClassLoger.Fail("WeChatTemplateAPI.GetTemplateID", resultJson);
                }
            } catch (Exception ex)
            {
                ClassLoger.Error("WeChatTemplateAPI.GetTemplateID", ex);
            }
            return(templateID);
        }
Ejemplo n.º 3
0
        public static List <WeChatTags> GetAllGroup()
        {
            List <WeChatTags> grouplist = new List <WeChatTags>();

            try
            {
                string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
                string url          = string.Format("https://api.weixin.qq.com/cgi-bin/tags/get?access_token={0}", access_token);
                string json         = HttpUtils.Ins.GET(url);
                if (json.Contains("errcode"))
                {
                    ClassLoger.Fail("WeChatTagsAPI.GetAllGroup", json);
                    return(null);
                }
                Dictionary <string, object>         dic    = JsonHelper.DeserializeObject(json);
                List <Dictionary <string, object> > groups = dic["tags"] as List <Dictionary <string, object> >;
                foreach (var group in groups)
                {
                    WeChatTags info = new WeChatTags();
                    info.count = group["count"].TryToInt(0);
                    info.id    = group["id"].TryToInt(0);
                    info.name  = group["name"].TryToString();
                    grouplist.Add(info);
                }
            }
            catch (Exception ex)
            {
                ClassLoger.Error("WeChatGroupAPI.GetAllGroup", ex);
            }
            return(grouplist);
        }
        /// <summary>
        /// 创建管理员微信公共号菜单
        /// </summary>
        /// <returns></returns>
        public static bool CreateAdminMenu()
        {
            string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
            string url          = string.Format("https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token={0}", access_token);
            string json         = SystemSet.AdminMenu;
            string resultJson   = HttpUtils.Ins.POST(url, json);
            Dictionary <string, object> reslut = JsonHelper.DeserializeObject(resultJson);

            ClassLoger.Info("WeChatMenuAPI.CreateAdminMenu", resultJson);
            if (resultJson.Contains("errcode"))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 根据用户OpenID获取用户所在的分组
        /// </summary>
        /// <param name="openID"></param>
        /// <returns></returns>
        public static int GetUserGroupID(string openID)
        {
            string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
            string url          = string.Format("https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={0}", access_token);
            string json         = "{\"openid\":\"" + openID + "\"}";
            string resultJson   = HttpUtils.Ins.POST(url, json);

            if (resultJson.Contains("errcode"))
            {
                ClassLoger.Fail("WeChatGroupAPI.GetUserGroupID", json);
                return(0);
            }
            Dictionary <string, object> result = JsonHelper.DeserializeObject(resultJson);

            return(result["groupid"].TryToInt(0));
        }
        /// <summary>
        /// 删除自定义菜单
        /// </summary>
        /// <returns></returns>
        public static bool DeleteAllMenu()
        {
            string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
            string url          = string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}", access_token);

            string resultJson = HttpUtils.Ins.GET(url);

            ClassLoger.Info("WeChatMenuAPI.DeleteAllMenu", resultJson);
            Dictionary <string, object> reslut = JsonHelper.DeserializeObject(resultJson);

            if (reslut["errcode"].TryToInt(0) != 0)
            {
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// 获取所有素材
        /// </summary>
        /// <returns></returns>
        public static string GetmaterialList()
        {
            string jsonresult = string.Empty;

            try
            {
                string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
                string url          = string.Format("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={0}", access_token);
                string postjson     = "{\"type\":\"news\",\"offset\":0,\"count\":10}";
                jsonresult = HttpUtils.Ins.POST(url, postjson);
            } catch (Exception ex)
            {
                ClassLoger.Error("WeChatMaterialAPI.GetmaterialList", ex);
            }
            return(jsonresult);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 把用户移动到指定分组
        /// </summary>
        /// <param name="openID">用户openid</param>
        /// <param name="groupid">分组ID</param>
        /// <returns></returns>
        public static bool UpdateUserGroup(string openID, int groupid)
        {
            string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
            string url          = string.Format("https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token={0}", access_token);
            string json         = "{\"openid\":\"" + openID + "\",\"to_groupid\":" + groupid + "}";
            string resultJson   = HttpUtils.Ins.POST(url, json);
            Dictionary <string, object> reslut = JsonHelper.DeserializeObject(resultJson);

            if (reslut["errcode"].TryToInt(0) == 0)
            {
                return(true);
            }
            else
            {
                ClassLoger.Fail("WeChatGroupAPI.UpdateUserGroup", resultJson);
            }
            return(false);
        }
        /// <summary>
        /// 创建基础自定义菜单
        /// </summary>
        /// <returns></returns>
        public static bool CreateBaseMenu()
        {
            string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
            string url          = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", access_token);
            string json         = SystemSet.BaseMenu;
            string resultJson   = HttpUtils.Ins.POST(url, json);
            Dictionary <string, object> reslut = JsonHelper.DeserializeObject(resultJson);

            if (reslut["errcode"].TryToInt(0) == 0)
            {
                return(true);
            }
            else
            {
                ClassLoger.Fail("WeChatMenuAPI.CreateBaseMenu", resultJson);
            }
            return(false);
        }
        /// <summary>
        /// 设置用户备注名称
        /// </summary>
        /// <param name="openID"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool SetWeChatUserName(string openID, string name)
        {
            string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
            string url          = string.Format("https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token={0}", access_token);
            string json         = "{\"openid\":\"" + openID + "\",\"remark\":\"" + name + "\"}";
            string resultJson   = HttpUtils.Ins.POST(url, json);
            Dictionary <string, object> result = JsonHelper.DeserializeObject(resultJson);

            if (result["errcode"].TryToInt(0) == 0)
            {
                return(true);
            }
            else
            {
                ClassLoger.Fail("WeChatUserInfoAPI.SetWeChatUserName", resultJson);
            }
            return(false);
        }
        /// <summary>
        /// 获取微信公共号的用户信息
        /// </summary>
        /// <param name="UnionID"></param>
        /// <returns></returns>
        public static WeChatUserInfo GetWeChatUserInfo(string opendid)
        {
            WeChatUserInfo uinfo        = new WeChatUserInfo();
            string         access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
            string         url          = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", access_token, opendid);
            string         jsonResult   = HttpUtils.Ins.GET(url);

            ClassLoger.Info("WeChatAPIHelper.GetWeChatUserInfo", url, jsonResult);
            if (!jsonResult.IsNull())
            {
                if (jsonResult.Contains("errcode"))
                {
                    ClassLoger.Fail("WeChatAPIHelper.GetWeChatUserInfo", url);
                    ClassLoger.Fail("WeChatAPIHelper.GetWeChatUserInfo", jsonResult);
                }
                else
                {
                    Dictionary <string, object> resultDic = JsonHelper.DeserializeObject(jsonResult);
                    uinfo.subscribe = resultDic["subscribe"].TryToInt(0);
                    if (resultDic["subscribe"].TryToInt(0) != 0)
                    {
                        uinfo.city       = resultDic["city"].TryToString();
                        uinfo.country    = resultDic["country"].TryToString();
                        uinfo.headimgurl = resultDic["headimgurl"].TryToString();
                        uinfo.nickname   = resultDic["nickname"].TryToString();
                        uinfo.openid     = resultDic["openid"].TryToString();
                        uinfo.province   = resultDic["province"].TryToString();
                        uinfo.sex        = resultDic["sex"].TryToInt(0);
                        if (resultDic.ContainsKey("unionid"))
                        {
                            uinfo.unionid = resultDic["unionid"].TryToString();
                        }
                        uinfo.groupid = resultDic["groupid"].TryToInt(0);
                    }
                }
            }
            return(uinfo);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// 推送模板消息
 /// </summary>
 /// <param name="templateJson">模板数据json</param>
 public static void SendTemplate(string templateJson)
 {
     string access_token = WeChatAccessTokenAPI.GetWeChatAccess_token();
     string url          = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", access_token);
     string resultJson   = HttpUtils.Ins.POST(url, templateJson);
 }