//同步 protected void btnSyn_Click(object sender, EventArgs e) { string err = ""; Model.wx_userweixin weixin = GetWeiXinCode(); WeiXinCRMComm wcr = new WeiXinCRMComm(); string accessToken = wcr.getAccessToken(weixin.id, out err); if (err != "") { return; } GroupsJson gJson = Groups.Get(accessToken); List <GroupsJson_Group> gjg = gJson.groups; #region 将数据插入到数据库里 BLL.wx_crm_group gBll = new BLL.wx_crm_group(); Model.wx_crm_group group = new Model.wx_crm_group(); try { gBll.DeleteByWid(weixin.id); int ttCount = gjg.Count; int insertCount = 0; for (int i = 0; i < gjg.Count; i++) { group.id = gjg[i].id; group.name = gjg[i].name; group.count = gjg[i].count; group.wid = weixin.id; bool succ = gBll.Add(group); if (succ) { insertCount++; } } if (ttCount == insertCount) { //将此次同步的日期更新到设置表里 BLL.wx_crm_setting setBll = new BLL.wx_crm_setting(); setBll.UpdateGroupSysDate(weixin.id, ttCount, DateTime.Now); JscriptMsg("分组同步成功!", "group_list.aspx", "Success"); } else { JscriptMsg("分组同步失败!", "", "Error"); return; } } catch (Exception ex) { JscriptMsg("分组同步失败!" + ex.Message, "", "Error"); return; } #endregion }
/// <summary> /// 获取所有微信分组信息的实体列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strAccessToken"></param> /// <returns></returns> public List <GroupsJson_Group> GetWCGroupList(string strAccessToken) { GroupsJson pGroup = Senparc.Weixin.MP.AdvancedAPIs.GroupsApi.Get(strAccessToken); if (pGroup == null || pGroup.groups == null) { return(null); } return(pGroup.groups); }
////[SupportFilter(ActionName = "Index")] public JsonResult GetNetList() { WC_OfficalAccounts model = account_BLL.GetCurrentAccount(); GroupsJson groupjson = GroupsApi.Get(model.AccessToken); foreach (var item in groupjson.groups) { WC_Group entity = new WC_Group(); if (m_BLL.m_Rep.Find(item.id) == null) { entity.Name = item.name; entity.Count = item.count.ToString(); entity.OfficalAccountId = model.Id.ToString(); m_BLL.m_Rep.Create(entity); } } return(Json(JsonHandler.CreateMessage(1, "获取成功"))); }
public JsonResult GetNetList() { WC_OfficalAccountsModel model = account_BLL.GetCurrentAccount(); GroupsJson groupjson = GroupsApi.Get(model.AccessToken); foreach (var item in groupjson.groups) { WC_GroupModel entity = new WC_GroupModel(); if (m_BLL.GetById(item.id.ToString()) == null) { entity.Id = item.id.ToString(); entity.Name = item.name; entity.Count = item.count; entity.OfficalAccountId = model.Id; m_BLL.Create(ref errors, entity); } } return(Json(JsonHandler.CreateMessage(1, "获取成功"))); }
/// <summary> /// 更新微信分组 /// </summary> /// <param name="MpID"></param> private void RefreshGroup(string MpID) { #region 初始化 StringBuilder sb = new StringBuilder(); string groupinsertstr = @" insert into MpGroup (ID,MpID,WxGroupID,Name,FansCount,ParentID,FullPath,Length,ChildCount) values('{0}','{1}',{2},'{3}',{4},'{5}','{6}',{7},{8})"; string groupupdatestr = @" update MpGroup set Name='{0}',FansCount={1} where ID='{2}'"; var dt = StaticObjects.wechatdh.ExecuteTable(string.Format("select ID,WxGroupID,Length from MpGroup where MpID='{0}'", MpID)).AsEnumerable() .Select(c => new { ID = DbTool.ToString(c["ID"]), WxGroupID = DbTool.ToNullInt(c["WxGroupID"]), Length = DbTool.ToInt(c["Length"]), }); GroupsJson result = null; try { result = GroupsApi.Get(WxApi.GetAccessToken(MpID)); } catch (Exception ex) { LogWriter.Error(ex, string.Format("获取MpID为{0}的分组报错", MpID)); result = GroupsApi.Get(WxApi.GetAccessToken(MpID, true)); } #endregion #region 处理根节点 var rootgroup = dt.Where(c => c.Length == 1).FirstOrDefault(); string rootid; if (rootgroup == null) { rootid = DbTool.CreateGuid(); sb.AppendFormat(groupinsertstr, rootid, MpID, "null", "全部", result.groups.Sum(c => c.count), -1, rootid, 1, result.groups.Count()); } else { rootid = rootgroup.ID; } #endregion #region 保存最新的节点 var oldgroups = dt.Where(c => c.Length == 2).ToList(); var notgroupid = ""; foreach (var wxgroup in result.groups) { var g = oldgroups.Where(c => c.WxGroupID == wxgroup.id).FirstOrDefault(); if (g == null) { var id = DbTool.CreateGuid(); sb.AppendFormat(groupinsertstr, id, MpID, wxgroup.id, wxgroup.name, wxgroup.count, rootid, string.Format("{0}.{1}", rootid, id), 2, 0); if (wxgroup.name == "未分组") notgroupid = g.ID; } else { sb.AppendFormat(groupupdatestr, wxgroup.name, wxgroup.count, g.ID); if (wxgroup.name == "未分组") notgroupid = g.ID; } } #endregion #region 剔除老的节点 var deletegroups = oldgroups.Where(c => !result.groups.Select(d => d.id).Contains(c.WxGroupID.Value)).ToList(); if (notgroupid == "") throw new Exception("找不到未分组节点"); sb.AppendFormat(@" update MpFans set groupid='{0}' from MpFans a inner join MpGroup b on a.GroupID=b.ID where b.MpID='{1}' and a.MpID='{1}' and b.ID in ('{2}') delete MpGroup where MpID='{1}' and ID in ('{2}')" , notgroupid, MpID, string.Join("','", deletegroups.Select(c => c.ID))); #endregion StaticObjects.wechatdh.ExecuteNonQuery(sb.ToString(), 3600); }
/// <summary> /// 订阅(关注)事件 /// </summary> /// <returns></returns> public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { var rs = CacheHelper.Get(string.Format("SubscribeRequest_{0}", account.AppID)); var rstype = CacheHelper.Get(string.Format("SubscribeRequest_{0}_Type", account.AppID)); var opid = requestMessage.FromUserName; #region 记录日志 try { var entityevent = new MpEventScanLog(); entityevent.ID = Formula.FormulaHelper.CreateGuid(); entityevent.MpID = account.ID; entityevent.OpenID = opid; entityevent.EventContent = requestMessage.EventKey.StartsWith("qrscene_") ? requestMessage.EventKey.Substring(8) : requestMessage.EventKey;; entityevent.EventType = "未关注"; entityevent.MsgID = requestMessage.MsgId.ToString(); entityevent.CreateDate = System.DateTime.Now; entities.Set <MpEventScanLog>().Add(entityevent); entities.SaveChanges(); } catch (Exception ex) { LogWriter.Info(string.Format("MPID{0}记录扫码事件出错:原因{1}", account.ID, ex.Message)); } #endregion #region 更新粉丝 WxFO wxfo = new WxFO(); try { UserInfoJson wxinfo = null; try { wxinfo = UserApi.Info(wxfo.GetAccessToken(account.ID), opid); } catch (Exception ex) { LogWriter.Error(ex, string.Format("获取MpID为{0},openid为{1}的用户信息报错", account.ID, opid)); wxinfo = UserApi.Info(wxfo.GetAccessToken(account.ID, true), opid); } if (wxinfo.errcode != ReturnCode.请求成功) { throw new Exception(string.Format("获取MpID为{0},openid为{1}的用户信息报错,错误编号:{2},错误消息:{3}", account.ID, opid, wxinfo.errcode, wxinfo.errmsg)); } var group = entities.Set <MpGroup>().Where(c => c.MpID == account.ID && c.WxGroupID == wxinfo.groupid).FirstOrDefault(); var entityfans = entities.Set <MpFans>().Where(c => c.MpID == account.ID && c.OpenID == opid).FirstOrDefault(); #region 保存分组 if (group == null) { var rootgroup = entities.Set <MpGroup>().Where(c => c.MpID == account.ID && c.Length == 1).FirstOrDefault(); if (rootgroup == null) { rootgroup = new MpGroup(); rootgroup.ID = Formula.FormulaHelper.CreateGuid(); rootgroup.MpID = account.ID; rootgroup.Name = "全部"; rootgroup.ParentID = "-1"; rootgroup.FullPath = rootgroup.ID; rootgroup.Length = 1; rootgroup.ChildCount = 0; entities.Set <MpGroup>().Add(rootgroup); } var g = new MpGroup(); g.ID = Formula.FormulaHelper.CreateGuid(); g.MpID = account.ID; g.ParentID = rootgroup.ID; g.FullPath = string.Format("{0}.{1}", rootgroup.ID, g.ID); g.Length = 2; g.ChildCount = 0; g.WxGroupID = wxinfo.groupid; GroupsJson groups = null; try { groups = GroupsApi.Get(wxfo.GetAccessToken(account.ID)); } catch (Exception ex) { LogWriter.Error(ex, string.Format("获取MpID为{0}的分组报错", account.ID)); groups = GroupsApi.Get(wxfo.GetAccessToken(account.ID, true)); } if (groups.errcode != ReturnCode.请求成功) { throw new Exception(string.Format("获取MpID为{0}的分组报错,错误编号:{1},错误消息:{2}", account.ID, groups.errcode, groups.errmsg)); } var wg = groups.groups.Where(c => c.id == wxinfo.groupid).FirstOrDefault(); if (wg != null) { g.Name = wg.name; g.FansCount = wg.count; } entities.Set <MpGroup>().Add(g); } #endregion #region 保存粉丝 if (entityfans == null) { entityfans = new MpFans(); entityfans.ID = Formula.FormulaHelper.CreateGuid(); entityfans.City = wxinfo.city; entityfans.Country = wxinfo.country; entityfans.HeadImgUrl = wxinfo.headimgurl; entityfans.IsFans = "1"; entityfans.Language = wxinfo.language; entityfans.MpID = account.ID; entityfans.NickName = wxinfo.nickname; entityfans.OpenID = wxinfo.openid; entityfans.Province = wxinfo.province; entityfans.Remark = wxinfo.remark; entityfans.Sex = wxinfo.sex.ToString(); entityfans.SubscribeTime = DateTimeHelper.GetDateTimeFromXml(wxinfo.subscribe_time); entityfans.UniionID = wxinfo.unionid; entityfans.WxGroupID = wxinfo.groupid; entityfans.GroupID = group.ID; entityfans.UpdateTime = DateTime.Now; entities.Set <MpFans>().Add(entityfans); } else { entityfans.City = wxinfo.city; entityfans.Country = wxinfo.country; entityfans.HeadImgUrl = wxinfo.headimgurl; entityfans.IsFans = "1"; entityfans.Language = wxinfo.language; entityfans.MpID = account.ID; entityfans.NickName = wxinfo.nickname; entityfans.OpenID = wxinfo.openid; entityfans.Province = wxinfo.province; entityfans.Remark = wxinfo.remark; entityfans.Sex = wxinfo.sex.ToString(); entityfans.SubscribeTime = DateTimeHelper.GetDateTimeFromXml(wxinfo.subscribe_time); entityfans.UniionID = wxinfo.unionid; entityfans.WxGroupID = wxinfo.groupid; entityfans.GroupID = group.ID; entityfans.UpdateTime = DateTime.Now; } #endregion entities.SaveChanges(); } catch (Exception ex) { LogWriter.Error(string.Format("粉丝订阅更新数据库失败,原因:{0}", ex.Message)); } #endregion #region 推送消息 if (rs == null || rstype == null) { var eventtype = MpEventType.Subscribe.ToString(); var entity = entities.Set <MpEvent>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.EventType == eventtype).FirstOrDefault(); if (entity != null) { CacheHelper.Set(string.Format("SubscribeRequest_{0}_Type", account.AppID), entity.ReplyType, cachesecond); if (entity.ReplyType == MpMessageType.none.ToString()) { return(null); } else if (entity.ReplyType == MpMessageType.image.ToString()) { var responseMessage = base.CreateResponseMessage <ResponseMessageImage>(); responseMessage.Image.MediaId = entity.ImageMediaID; CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond); return(responseMessage); } else if (entity.ReplyType == MpMessageType.text.ToString()) { var responseMessage = base.CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = entity.Content; CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond); return(responseMessage); } else if (entity.ReplyType == MpMessageType.voice.ToString()) { var responseMessage = base.CreateResponseMessage <ResponseMessageVoice>(); responseMessage.Voice.MediaId = entity.VoiceMediaID; CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond); return(responseMessage); } else if (entity.ReplyType == MpMessageType.video.ToString()) { var responseMessage = base.CreateResponseMessage <ResponseMessageVideo>(); var video = entities.Set <MpMediaVideo>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.ID == entity.VideoID).FirstOrDefault(); if (video == null) { return(null); } responseMessage.Video.MediaId = video.MediaID; responseMessage.Video.Title = video.Title; responseMessage.Video.Description = video.Description; CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond); return(responseMessage); } else if (entity.ReplyType == MpMessageType.mpnews.ToString()) { var responseMessage = base.CreateResponseMessage <ResponseMessageNews>(); var article = entities.Set <MpSelfArticle>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.ID == entity.ArticleID).FirstOrDefault(); if (article == null) { return(null); } responseMessage.Articles.Add(new Article() { Title = article.Title, Description = article.Description, Url = article.Url, PicUrl = string.Format("http://{0}/wechatservice/api/Image/Get/{1}", domain, article.PicFileID), }); CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond); return(responseMessage); } else if (entity.ReplyType == MpMessageType.mpmultinews.ToString()) { var responseMessage = base.CreateResponseMessage <ResponseMessageNews>(); var article = entities.Set <MpSelfArticleGroup>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.ID == entity.ArticleGroupID).FirstOrDefault(); if (article == null || article.MpSelfArticleGroupItem == null || article.MpSelfArticleGroupItem.Count(c => c.MpSelfArticle != null) < 2) { return(null); } foreach (var item in article.MpSelfArticleGroupItem.Where(c => c.MpSelfArticle != null)) { responseMessage.Articles.Add(new Article() { Title = item.MpSelfArticle.Title, Description = item.MpSelfArticle.Description, Url = item.MpSelfArticle.Url, PicUrl = string.Format("http://{0}/wechatservice/api/Image/Get/{1}", domain, item.MpSelfArticle.PicFileID), }); } CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond); return(responseMessage); } else { return(null); } } //其他回复 else { return(null); } } else { var rstp = rstype.ToString(); if (rstp == MpMessageType.image.ToString()) { return(rs as ResponseMessageImage); } else if (rstp == MpMessageType.mpmultinews.ToString()) { return(rs as ResponseMessageNews); } else if (rstp == MpMessageType.mpnews.ToString()) { return(rs as ResponseMessageNews); } else if (rstp == MpMessageType.text.ToString()) { return(rs as ResponseMessageText); } else if (rstp == MpMessageType.video.ToString()) { return(rs as ResponseMessageVideo); } else if (rstp == MpMessageType.voice.ToString()) { return(rs as ResponseMessageVoice); } return(rs as IResponseMessageBase); } #endregion }