/// <summary> /// 同步处理,递归算法 /// </summary> /// <param name="totCount"></param> /// <param name="wid"></param> /// <param name="accessToken"></param> /// <param name="nexOpenid"></param> /// <param name="updateTime"></param> public string SysPersonFun(int totCount, string wxid, string accessToken, string nexOpenid, DateTime updateTime) { string ret = ""; OpenIdResultJson gJson = UserApi.Get(accessToken, nexOpenid); List <string> openidStr = gJson.data.openid;//此次拉取的openid字符串 totCount += InsertUserInfo(wxid, accessToken, openidStr, updateTime); if (gJson.next_openid != "" && gJson.count == 1000) { SysPersonFun(totCount, wxid, accessToken, gJson.next_openid, updateTime); } else { int sjTtCount = gJson.total; if (sjTtCount == totCount) { //将此次同步的日期更新到设置表里 WxBaseConfigEntity weixin = config.GetDefaultConfig(); weixin.PersonSynDate = updateTime; weixin.OpenidCount = totCount; config.SubmitForm(weixin); ret = "粉丝同步成功!"; } else { ret = "粉丝同步失败!"; } } return(ret); }
/// <summary> /// 读取配置 /// </summary> /// <returns></returns> public WxBaseConfigEntity GetForm() { Code.SysConfig.WeixinConfig entity = new Code.SysConfig.WeixinConfigApp().LoadConfig(); if (entity != null) { WxBaseConfigEntity model = new WxBaseConfigEntity(); model.WxName = entity.WxName; model.WxId = entity.WxId; model.WxCode = entity.WxCode; model.HeadPic = entity.HeadPic; model.Token = entity.Token; model.ApiUrl = entity.ApiUrl; model.AppId = entity.AppId; model.AgentId = entity.AgentId; model.AppKey = entity.AppKey; model.AppSecret = entity.AppSecret; model.Encoding = entity.Encoding; model.WxType = entity.WxType; model.CloseKW = entity.CloseKW; model.TimeStamp = entity.TimeStamp; model.PersonSynDate = entity.PersonSynDate; model.OpenidCount = entity.OpenidCount; return(model); } else { return(null); } }
/// <summary> /// 保存配置 /// </summary> /// <param name="mEntity"></param> public void SubmitForm(WxBaseConfigEntity mEntity) { try { Code.SysConfig.WeixinConfig model = new Code.SysConfig.WeixinConfigApp().LoadConfig(); model.WxName = mEntity.WxName; model.WxId = mEntity.WxId; model.WxCode = mEntity.WxCode; model.HeadPic = mEntity.HeadPic; model.Token = mEntity.Token; model.ApiUrl = mEntity.ApiUrl; model.AppId = mEntity.AppId; model.AgentId = mEntity.AgentId; model.AppKey = mEntity.AppKey; model.AppSecret = mEntity.AppSecret; model.Encoding = mEntity.Encoding; model.WxType = mEntity.WxType; model.CloseKW = mEntity.CloseKW; model.TimeStamp = mEntity.TimeStamp; model.PersonSynDate = mEntity.PersonSynDate; model.OpenidCount = mEntity.OpenidCount; new Code.SysConfig.WeixinConfigApp().SaveConifg(model); } catch (Exception ex) { throw new Exception("保存出错:" + ex.Message); } }
public static void SendTemplateMessage(string openId, string templateId, object data, string url = null) { WxBaseConfigApp app = new WxBaseConfigApp(); WxBaseConfigEntity weixin = app.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId, true); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { throw new Exception(token.errmsg); } if (!string.IsNullOrEmpty(templateId)) { //获取AccessToken try { var result = TemplateApi.SendTemplateMessage(token.access_token, openId, templateId, url, data); //new BLL.wx_message_template_sendlog().AddLog(AppId, templateId, temp_title, openId, string.Format("【微信公众号发送模板消息接口调用】返回参数:errcode:{0},erromsg:{1},misgid:{2}", result.errcode, result.errmsg, result.msgid)); } catch (Exception ex) { throw new Exception("微信模板消息:" + ex.Message); //new BLL.wx_message_template_sendlog().AddLog(AppId, templateId, temp_title, openId, string.Format("【微信公众号发送模板消息接口调用】错误:{0}", ex.Message)); } } }
public bool wxCloseKW(string apiid) { WxBaseConfigEntity model = new WxBaseConfigEntity(); model = GetForm(); if (model != null) { return(model.CloseKW); } return(true); }
public string GetWeiXinToken(string apiid) { WxBaseConfigEntity model = new WxBaseConfigEntity(); model = GetForm(); if (model != null) { return(model.Token); } return(string.Empty); }
public WxBaseConfigEntity GetDefaultConfig(string apiid = "") { WxBaseConfigEntity model = new WxBaseConfigEntity(); var expression = ExtLinq.True <WxBaseConfigEntity>(); if (!string.IsNullOrEmpty(apiid)) { expression = expression.And(t => t.WxCode.Contains(apiid)); } expression = expression.And(t => t.Status == true); expression = expression.And(t => t.Status == true); return(model = service.IQueryable(expression).FirstOrDefault <WxBaseConfigEntity>()); }
/// <summary> /// 验证accesstoken /// </summary> /// <returns></returns> public bool ValidAccessToken() { WxBaseConfigEntity weixin = config.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(false); } else { return(true); } }
public bool wxCloseKW(string apiid) { WxBaseConfigEntity model = new WxBaseConfigEntity(); var expression = ExtLinq.True <WxBaseConfigEntity>(); if (!string.IsNullOrEmpty(apiid)) { expression = expression.And(t => t.uuId.Contains(apiid)); } model = service.IQueryable(expression).FirstOrDefault <WxBaseConfigEntity>(); if (model != null) { return(model.CloseKW); } return(true); }
public ActionResult RefreshAccessToken() { try { WxBaseConfigEntity weixin = app.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId, true); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Error(token.errmsg)); } return(Success("Access_Token更新成功。")); } catch (Exception ex) { return(Error("执行过程出现错误:" + ex.Message)); } }
public string GetWeiXinToken(string apiid) { WxBaseConfigEntity model = new WxBaseConfigEntity(); var expression = ExtLinq.True <WxBaseConfigEntity>(); if (!string.IsNullOrEmpty(apiid)) { expression = expression.And(t => t.WxCode.Contains(apiid)); expression = expression.And(t => t.Status == true); } model = service.IQueryable(expression).FirstOrDefault <WxBaseConfigEntity>(); if (model != null) { return(model.Token); } return(string.Empty); }
public bool ExistsWidAndWxId(string apiid, string wxid) { WxBaseConfigEntity model = new WxBaseConfigEntity(); var expression = ExtLinq.True <WxBaseConfigEntity>(); if (!string.IsNullOrEmpty(apiid)) { expression = expression.And(t => t.uuId.Contains(apiid)); expression = expression.Or(t => t.WxId.Contains(wxid)); } model = service.IQueryable(expression).FirstOrDefault <WxBaseConfigEntity>(); if (model != null) { return(true); } return(false); }
public void SubmitForm(WxBaseConfigEntity mEntity, string keyValue) { if (service.IQueryable().Count(t => t.WxCode.Equals(mEntity.WxCode) && !t.uuId.Equals(keyValue)) > 0) { throw new Exception("微信号已存在"); } if (!string.IsNullOrEmpty(keyValue)) { mEntity.Modify(keyValue); service.Update(mEntity); } else { mEntity.Create(); service.Insert(mEntity); } }
public ActionResult DeleteMenu() { try { WxBaseConfigEntity weixin = app.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Error(token.errmsg)); } var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.DeleteMenu(token.access_token); //重新获得最新的菜单 return(Success("成功")); } catch (Exception ex) { return(Error("执行过程出现错误:" + ex.Message)); } }
public ActionResult GetWeixinUser() { try { WxBaseConfigEntity weixin = config.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId, true); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Error("更新失败,获取token失败:" + token.errmsg)); } //更新时间,作为一个标记 DateTime updateTime = DateTime.Now; try { string ret = SysPersonFun(0, weixin.uuId, token.access_token, "", updateTime); //查找此次为同步到的,则为已经跑路的 IList <UsersEntity> userlist = app.GetList2(updateTime); if (userlist != null && userlist.Count > 0) { for (int i = 0; i < userlist.Count; i++) { userlist[i].Status = 2; app.SubmitForm(userlist[i], userlist[i].OpenId); } } return(Success("更新成功")); } catch (Exception ex) { return(Error("更新失败:" + ex.Message)); } } catch (Exception ex) { return(Error("更新失败:" + ex.Message)); } }
public ActionResult SubmitForm(WxBaseConfigEntity userEntity) { app.SubmitForm(userEntity); return(Success("操作成功。")); }
public ActionResult SubmitForm(WxBaseConfigEntity userEntity, string keyValue) { app.SubmitForm(userEntity, keyValue); return(Success("操作成功。")); }
public ActionResult SubmitForm(WxMenu entity, string keyValue) { try { MenuManage mMrg = new MenuManage(); WxBaseConfigEntity weixin = app.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Error(token.errmsg)); } //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); IList <BaseButton> topList = new List <BaseButton>(); IList <SingleButton> subList = new List <SingleButton>(); #region 菜单设置 for (int i = 0; i < 3; i++) { string txtName = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1)); string txtKey = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1) + "Key"); string txtUrl = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1) + "Url"); if (string.IsNullOrEmpty(txtName)) { // 如果名称为空,则忽略该菜单,以及下面的子菜单 continue; } subList = new List <SingleButton>(); string txtSubName; string txtSubKey; string txtSubUrl; for (int j = 0; j < 5; j++) { #region 子菜单的设置 txtSubName = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1)); txtSubKey = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1) + "Key"); txtSubUrl = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1) + "Url"); if (string.IsNullOrEmpty(txtSubName)) { continue; } if (!string.IsNullOrEmpty(txtSubUrl)) { SingleViewButton sub = new SingleViewButton(); sub.name = txtSubName.Trim(); sub.url = Utils.UrlDecode(txtSubUrl.Trim()); subList.Add(sub); } else if (!string.IsNullOrEmpty(txtSubKey)) { SingleClickButton sub = new SingleClickButton(); sub.name = txtSubName.Trim(); sub.key = txtSubKey.Trim(); subList.Add(sub); } else { //报错 :子菜单必须有key和name return(Error("二级菜单的名称和key或者url必填!")); ; } #endregion } //子菜单循环结束 if (subList != null && subList.Count > 0) { //有子菜单, 该一级菜单是SubButton if (subList.Count < 1) { return(Error("子菜单个数为2~5个!")); } SubButton topButton = new SubButton(Utils.CutString(txtName.Trim(), 16)); topButton.sub_button.AddRange(subList); topList.Add(topButton); } else { // 无子菜单 if (string.IsNullOrEmpty(txtKey) && string.IsNullOrEmpty(txtUrl)) { return(Error("若无子菜单,必须填写key或者url值!")); } if (!string.IsNullOrEmpty(txtUrl)) { //view 页面跳转 SingleViewButton topSingleButton = new SingleViewButton(); topSingleButton.name = txtName.Trim(); topSingleButton.url = Utils.UrlDecode(txtUrl.Trim()); topList.Add(topSingleButton); } else if (!string.IsNullOrEmpty(txtKey)) { SingleClickButton topSingleButton = new SingleClickButton(); topSingleButton.name = txtName.Trim(); topSingleButton.key = txtKey.Trim(); topList.Add(topSingleButton); } } } #endregion bg.button.AddRange(topList); var result = mMrg.CreateMenu(token.access_token, bg); return(Success("菜单提交成功!")); } catch (Exception ex) { return(Error("报错:" + ex.Message)); } }
public ActionResult GetMenu() { try { MenuManage mMrg = new MenuManage(); WxBaseConfigEntity weixin = app.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Error(token.errmsg)); } GetMenuResult result = mMrg.GetMenu(token.access_token); if (result == null) { return(Error("未获得到菜单,请参考【使用规则】,自行排查问题!")); } var topButtonList = result.menu.button; int topNum = topButtonList.Count; //List<string> menu = new List<string>(); Dictionary <string, string> menu = new Dictionary <string, string>(); for (int i = 0; i < topNum; i++) { var topButton = topButtonList[i]; if (topButton != null) { menu.Add("Top" + (i + 1), topButton.name); if (topButton.GetType() != typeof(SubButton)) { //下面无子菜单 if (topButton.GetType() == typeof(SingleViewButton)) { //view 页面跳转 menu.Add("Top" + (i + 1) + "Key", ((SingleViewButton)topButton).url); } else { menu.Add("Top" + (i + 1) + "Url", ((SingleClickButton)topButton).key); } } else { //下面有子菜单 IList <SingleButton> subButtonList = ((SubButton)topButton).sub_button; if (subButtonList != null && subButtonList.Count > 0) { for (int j = 0; j < subButtonList.Count; j++) { if (subButtonList[j].GetType() == typeof(SingleViewButton)) { SingleViewButton sub = (SingleViewButton)subButtonList[j]; menu.Add("Menu" + (i + 1) + (j + 1), sub.name); menu.Add("Menu" + (i + 1) + (j + 1) + "Url", sub.url); } else { SingleClickButton sub = (SingleClickButton)subButtonList[j]; menu.Add("Menu" + (i + 1) + (j + 1), sub.name); menu.Add("Menu" + (i + 1) + (j + 1) + "Key", sub.key); } } } } } } return(Success("菜单获取成功", menu)); } catch (Exception ex) { return(Error(ex.Message)); } }