/// <summary> /// 验证accesstoken /// </summary> /// <returns></returns> public bool ValidAccessToken() { string err = ""; Model.wx_userweixin weixin = GetWeiXinCode(); WeiXinCRMComm wcr = new WeiXinCRMComm(); string accessToken = wcr.getAccessToken(weixin.id, out err); if (err != "") { lblInfo.Text = "该功能仅限于认证过的服务号,其他的都无法使用!AccessToken获取失败,请检查AppId和AppSecret填写是否正确!错误信息如下:" + err; lblInfo.ForeColor = System.Drawing.Color.Red; btnSyn.Enabled = false; btnSyn.Style.Add("display", "none"); hidErr.Value = lblInfo.Text; MessageBox.ResponseScript(this, " $(\"#btn_anniu\").hide();"); return(false); } else { hidErr.Value = ""; //获取上次更新的时间 BLL.wx_crm_setting setBll = new BLL.wx_crm_setting(); Model.wx_crm_setting setting = setBll.GetModelByWid(weixin.id); if (setting == null || setting.groupSynDate == null) { lblInfo.Text = "请点击按钮更新分组信息"; } else { lblInfo.Text = "上次更新时间为:" + setting.groupSynDate.Value; } return(true); } }
/// <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, int wid, string accessToken, string nexOpenid, DateTime updateTime) { string ret = ""; OpenIdResultJson gJson = Senparc.Weixin.MP.AdvancedAPIs.User.Get(accessToken, nexOpenid); List <string> openidStr = gJson.data.openid;//此次拉取的openid字符串 totCount += InsertUserInfo(wid, accessToken, openidStr, updateTime); if (gJson.next_openid != "" && gJson.count == 1000) { SysPersonFun(totCount, wid, accessToken, gJson.next_openid, updateTime); } else { int sjTtCount = gJson.total; if (sjTtCount == totCount) { //将此次同步的日期更新到设置表里 BLL.wx_crm_setting setBll = new BLL.wx_crm_setting(); setBll.UpdatePersonSysDate(wid, totCount, updateTime); ret = "粉丝同步成功!"; } else { ret = "粉丝同步失败!"; } } return(ret); }
//同步 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 }