/// <summary> /// 创建微信分组并同步 /// </summary> /// <param name="strGroupName"></param> /// <returns></returns> public List <GroupsJson_Group> CreateWXGroupAndLoad(string strGroupName) { List <GroupsJson_Group> pList = new List <GroupsJson_Group>(); this.ExecuteTryCatch(() => { //通过sdk获取accessToken string strAccessToken = Senparc.Weixin.MP.CommonAPIs.WeiXinSDKExtension.GetCurrentAccessToken(); WXGroupServiceWeChat pService = new WXGroupServiceWeChat(); //首先创建分组 pService.CreateWXGroup(strAccessToken, strGroupName); //再从微信API加载所有分组信息 pList = pService.GetWCGroupList(strAccessToken); if (pList == null || pList.Count == 0) { EGExceptionOperator.ThrowX <Exception>("没有微信端用户分组信息", EGActionCode.缺少目标数据); } WXGroupServiceLocal pServiceLocal = new WXGroupServiceLocal(); //再更新至数据库并清空缓存 if (!pServiceLocal.UpdateWCGroupList(pList)) { throw new Senparc.Weixin.Exceptions.WeixinException("微信限制接口"); } }); return(pList); }
/// <summary> /// 移动用户分组 /// </summary> /// <param name="strListOpenID"></param> /// <param name="strTargetGroupID"></param> public void ChangeWXGroup(string strListOpenID, string strTargetGroupID) { this.ExecuteTryCatch(() => { if (string.IsNullOrEmpty(strListOpenID)) { EGExceptionOperator.ThrowX <Exception>("请選擇移動目標用戶", EGActionCode.缺少必要参数); } if (string.IsNullOrEmpty(strTargetGroupID)) { EGExceptionOperator.ThrowX <Exception>("请選擇移動目標分組", EGActionCode.缺少必要参数); } int iGroupID = Convert.ToInt16(strTargetGroupID); //通过sdk获取accessToken string strAccessToken = Senparc.Weixin.MP.CommonAPIs.WeiXinSDKExtension.GetCurrentAccessToken(); //初始化服务 WXGroupServiceWeChat pGroupServiceWX = new WXGroupServiceWeChat(); WXGroupServiceLocal pGroupServiceLocal = new WXGroupServiceLocal(); WCUserServiceLocal <WeChatUser> pUserService = new WCUserServiceLocal <WeChatUser>(); //将前端生成json串转为openID集合 List <string> pListOpenID = CommonFunction.FromJsonTo <List <string> >(strListOpenID); //遍历更新微信端移动用户分组 foreach (string openID in pListOpenID) { Senparc.Weixin.MP.Entities.WxJsonResult pResult = pGroupServiceWX.MemberUpdate(strAccessToken, openID, iGroupID); if (pResult.errcode != Senparc.Weixin.ReturnCode.请求成功) { EGExceptionOperator.ThrowX <Exception>(pResult.errmsg, EGActionCode.未知错误); } } //更新用户新分组信息至数据库,并清空缓存 pUserService.UpdateWCUserForGroupID(pListOpenID, iGroupID); //根据用户新分组信息,同步分组表信息,并清空缓存 pGroupServiceLocal.ReloadWCGroupFromUser(); }); }