public JsonResult GetSubDepartment(string id, string departlist) { string accessToken = WeChatCommonService.GetWeiXinToken(AppId); //修改Department数据源 先根据AppId获取应用信息 allowPartys Pending var config = WeChatCommonService.GetWeChatConfigByID(AppId); var app = AppApi.GetAppInfo(accessToken, int.Parse(config.WeixinAppId)); GetAppInfo_AllowPartys allowPartys = app.allow_partys; // TODO: async/await执行较长等待的task var subdepartList = MailListApi.GetDepartmentList(accessToken, Int32.Parse(id)).department;//.Where(x => allowPartys.partyid.Contains(x.id)).ToList() var listReturn = EasyUITreeData.GetTreeData(subdepartList, "id", "name", "parentid"); listReturn.ForEach(a => { a.state = "closed"; }); if (!string.IsNullOrEmpty(departlist)) { var departids = departlist.Split('|'); EasyUITreeData.SetChecked <string>(departids.ToList(), listReturn); } return(Json(listReturn, JsonRequestBehavior.AllowGet)); }
public void GetAppInfoTest() { var accessToken = AccessTokenContainer.GetToken(_corpId); var result = AppApi.GetAppInfo(accessToken, 2); Assert.IsNotNull(result.agentid); Assert.AreEqual(result.agentid, "2"); }
/// <summary> /// 建议走缓存 /// </summary> /// <param name="iAppID"></param> /// <returns></returns> public static GetAppInfoResult GetAppInfo(int iWeChatID) { var objConfig = GetWeChatConfigByID(iWeChatID); string strToken = AccessTokenContainer.TryGetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); return(AppApi.GetAppInfo(strToken, int.Parse(objConfig.WeixinAppId))); // Ret.allow_userinfos }
public static GetAppInfoResult GetUserOrDept(int iAppID) { var objConfig = WeChatCommonService.GetWeChatConfig(iAppID); string strToken = AccessTokenContainer.TryGetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); return(AppApi.GetAppInfo(strToken, iAppID)); // Ret.allow_userinfos }
public virtual void PrepareEditData() { string accessToken = WeChatCommonService.GetWeiXinToken(AppId); //修改tag数据源 先根据AppId获取应用信息 allowTag Pending var app = AppApi.GetAppInfo(accessToken, AppId); GetAppInfo_AllowTags allowTags = app.allow_tags; var tagList = MailListApi.GetTagList(accessToken).taglist;//.Where(x=> allowTags.tagid.Contains(int.Parse(x.tagid))).ToList() ViewBag.taglist = tagList; }
///// <summary> ///// 获取所有App信息 ///// </summary> ///// <param name="appid"></param> ///// <returns></returns> public List <GetAppInfoResult> GetListInfo(int iPage, int iCount, ref int iTotal) { List <GetAppInfoResult> lst = new List <GetAppInfoResult>(); string aPPList = Request["APPList"]; var strToken = rtntoken(); //获取所有app //var applist = AppApi.GetAppList(strToken); var corpId = CommonService.GetSysConfig("WeixinCorpId", string.Empty); var applist = CommonService.lstSysWeChatConfig.Where(a => a.WeixinCorpId == corpId).ToList(); GetAppInfoResult result = new GetAppInfoResult(); //获取所有app信息 if (string.IsNullOrEmpty(aPPList)) { foreach (var rst in applist) { try { result = AppApi.GetAppInfo(strToken, int.Parse(rst.WeixinAppId)); } catch { continue; } lst.Add(result); } } else { try { result = AppApi.GetAppInfo(strToken, int.Parse(aPPList)); lst.Add(result); } catch { } } if (lst.Count != 0) { iTotal = lst.Count(); return(lst.OrderBy(a => int.Parse(a.agentid)).Skip((iPage - 1) * iCount).Take(iCount).ToList()); } return(null); }
///// <summary> ///// 判断用户是否在APP的可见范围内 ///// </summary> ///// <param name="appId"></param> ///// <param name="currentUserId"></param> ///// <param name="AccountManageID"></param> ///// <returns></returns> //public bool IsValidateUser(string appId, string currentUserId, int AccountManageID) //{ // if (HttpRuntime.Cache.Get("validateUsers") != null) // { // var obj = HttpRuntime.Cache.Get("validateUsers"); // List<String> allVisiblePerson = (List<String>)obj; // return allVisiblePerson.Contains(currentUserId); // } // else // { // var appInfo = lstSysWeChatConfig.SingleOrDefault(a => a.AccountManageId == AccountManageID); // var token = CommonApi.GetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret); // var results = AppApi.GetAppInfo(token.access_token, Int32.Parse(appId)); // List<string> allVisiblePerson = new List<string>(); // foreach (var user in results.allow_userinfos.user) // { // allVisiblePerson.Add(user.userid); // } // foreach (var groupId in results.allow_partys.partyid) // { // var members = MailListApi.GetDepartmentMemberInfo(token.access_token, groupId, 1, 1); // foreach (var user in members.userlist) // { // allVisiblePerson.Add(user.userid); // } // } // HttpRuntime.Cache.Insert("validateUsers", allVisiblePerson, null, DateTime.Now.AddHours(2), TimeSpan.Zero); // return allVisiblePerson.Contains(currentUserId); // } //} /// <summary> /// 判断用户是否在APP的可见范围内 /// </summary> /// <param name="appId"></param> /// <param name="objUser"></param> /// <param name="AccountManageID"></param> /// <returns></returns> public static bool IsValidateUser(int appId, SysAddressBookMember objUser, int AccountManageID) { var AppInfo = cacheManager.Get <GetAppInfoResult>(string.Format("APP_{0}_{1}", AccountManageID, appId), () => { Logger.Debug("cache is empty, creating cache...."); var appInfo = GetWeChatConfigByID(appId); var token = CommonApi.GetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret); var results = AppApi.GetAppInfo(token.access_token, int.Parse(appInfo.WeixinAppId)); return(results); }); //部门 if (!string.IsNullOrWhiteSpace(objUser.Department)) { string strParty = objUser.Department.Insert(objUser.Department.Length - 1, ",").Insert(1, ","); if (AppInfo.allow_partys.partyid.Count(a => strParty.IndexOf("," + a.ToString() + ",") >= 0) > 0) { return(true); } var departments = lstDepartment(AccountManageID); List <int> allDepartments = new List <int>(); FindAllParentDepartments(strParty, departments, allDepartments); if (AppInfo.allow_partys.partyid.Any(a => allDepartments.Contains(a))) { return(true); } } //tag if (!string.IsNullOrWhiteSpace(objUser.TagList)) { string strTag = objUser.TagList.Insert(objUser.TagList.Length - 1, ",").Insert(1, ","); if (AppInfo.allow_tags.tagid.Count(a => strTag.IndexOf("," + a.ToString() + ",") >= 0) > 0) { return(true); } } //user if (AppInfo.allow_userinfos.user.Exists(a => objUser.UserId == a.userid)) { return(true); } return(false); }
/// <summary> /// 获取企业号应用集合 /// </summary> public List <GetAppInfoResult> GetCorpInfos() { var pList = new List <GetAppInfoResult>(); this.ExecuteTryCatch(() => { //获取AccessToken string strAccessToken = Senparc.Weixin.MP.CommonAPIs.WeiXinSDKExtension.GetCurrentAccessTokenQY(); WeiXinConfiguration.corpInfos.ForEach((pCorp) => { var papps = AppApi.GetAppInfo(strAccessToken, pCorp.aid); pList.Add(papps); pCorp.aname = papps.name; pCorp.round_logo_url = papps.round_logo_url; }); }); return(pList); }
/// <summary> /// 获取应用详情 /// </summary> /// <param name="QyID">企业ID</param> /// <param name="agentId">企业应用的id,可在应用的设置页面查看</param> /// <returns></returns> public GetAppInfoResult GetAppInfo(string QyID, int agentId) { GetAppInfoResult result = null; try { result = AppApi.GetAppInfo(GetAccessToken(QyID), agentId); } catch (Exception ex) { LogWriter.Error(ex, string.Format("获取QyID为{0}的应用详情失败", QyID)); result = AppApi.GetAppInfo(GetAccessToken(QyID, true), agentId); } if (result.errcode != ReturnCode_QY.请求成功) { LogWriter.Info(string.Format("获取QyID为{0}的应用详情失败,原因:{1}", QyID, result.errmsg)); } return(result); }
public void GetAppInfoTest() { { //使用AppKey测试 //常规AccessToken测试 var appKey = AccessTokenContainer.BuildingKey(_corpId, base._corpSecret); var result = AppApi.GetAppInfo(appKey, 2); Assert.IsNotNull(result.agentid); Assert.AreEqual(result.agentid, "2"); Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result)); } { //常规AccessToken测试 var accessToken = AccessTokenContainer.GetToken(_corpId, base._corpSecret); var result = AppApi.GetAppInfo(accessToken, 2); Assert.IsNotNull(result.agentid); Assert.AreEqual(result.agentid, "2"); Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result)); } }
public override ActionResult Edit(string id) { string strToken = rtntoken(int.Parse(id)); GetAppInfoResult result = AppApi.GetAppInfo(strToken, int.Parse(id)); ViewBag.result = result; //部门信息 //List<DepartmentList> subdepartList = MailListApi.GetDepartmentList(GetToken(), 0).department; List <DepartmentList> subdepartList = new List <DepartmentList>(); if (WeChatCommonService.lstDepartment != null) { subdepartList = WeChatCommonService.lstDepartment; } if (result.allow_partys != null) { String allowpart = string.Join(",", result.allow_partys.partyid); List <string> departname = new List <string>(); foreach (var part in allowpart.Split(',')) { foreach (var allow in subdepartList) { if (allow.id.ToString() == part) { departname.Add(allow.name); break; } } } ViewBag.depatlist = departname; } else { ViewBag.depatlist = new List <string>(); } //标签信息 //var tagList = MailListApi.GetTagList(strToken).taglist; //ViewBag.taglist = tagList; List <TagItem> tagList = new List <TagItem>(); if (WeChatCommonService.lstTag != null) { tagList = WeChatCommonService.lstTag; } if (result.allow_tags != null) { String allowtag = string.Join(",", result.allow_tags.tagid); List <string> tagname = new List <string>(); foreach (var tag in allowtag.Split(',')) { foreach (var tag_list in tagList) { if (tag_list.tagid == tag) { tagname.Add(tag_list.tagname); break; } } } ViewBag.tagname = tagname; } else { ViewBag.tagname = new List <string>(); } //用户信息 List <GetMemberResult> user_List = new List <GetMemberResult>(); if (WeChatCommonService.lstUser != null) { user_List = WeChatCommonService.lstUser; } List <GetAppInfo_AllowUserInfos_User> userList = result.allow_userinfos.user; Dictionary <string, string> username = new Dictionary <string, string>(); foreach (var user in userList) { foreach (var user1 in user_List) { if (user1.userid == user.userid) { username.Add(user1.name, user1.avatar); break; } } } ViewBag.userlist = username; //获取corpid与secret var Config = WeChatCommonService.GetWeChatConfig(int.Parse(id)); if (Config != null) { ViewBag.corpid = Config.WeixinCorpId; ViewBag.secret = Config.WeixinCorpSecret; ViewBag.welcomemessage = Config.WelcomeMessage; ViewBag.configid = Config.Id; } return(View("../appmanage/Edit", result)); }
//public SysWechatConfig GetAppSysWechatConfig(string appId) { // return lstSysWeChatConfig.Find(a => a.WeixinAppId == appId); //} public List <PersonGroup> GetAllPersonAndGroup(string appId, int accountManageId) { var id = int.Parse(appId); SysWechatConfig appInfo = null; if (id == 0) { appInfo = lstSysWeChatConfig.FirstOrDefault(a => a.AccountManageId == accountManageId); } else { appInfo = lstSysWeChatConfig.SingleOrDefault(a => a.Id == id && a.AccountManageId == accountManageId); } string token = (appInfo.IsCorp != null && !appInfo.IsCorp.Value) ? Innocellence.Weixin.MP.CommonAPIs.AccessTokenContainer.GetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret) : Innocellence.Weixin.QY.CommonAPIs.AccessTokenContainer.TryGetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret); var results = AppApi.GetAppInfo(token, int.Parse(appInfo.WeixinAppId)); List <PersonGroup> list = new List <PersonGroup>(); if (results.allow_userinfos != null) { foreach (var person in results.allow_userinfos.user) { PersonGroup personGroup = new PersonGroup(); personGroup.Type = "Person"; var p = MailListApi.GetMember(token, person.userid); personGroup.WeixinName = p.name; personGroup.WeixinId = p.userid; personGroup.Avatar = p.avatar; list.Add(personGroup); } } if (results.allow_partys != null) { foreach (var groupId in results.allow_partys.partyid) { PersonGroup personGroup = new PersonGroup(); personGroup.Type = "Group"; var d = MailListApi.GetDepartmentList(token, groupId).department.FirstOrDefault(); if (d != null) { personGroup.WeixinName = d.name; personGroup.WeixinId = d.id.ToString(); list.Add(personGroup); } } } if (results.allow_tags != null) { var allTagList = MailListApi.GetTagList(token).taglist; foreach (var groupId in results.allow_tags.tagid) //foreach (var tag in allTagList) { PersonGroup personGroup = new PersonGroup(); personGroup.Type = "Tag"; var tag = allTagList.Find(a => a.tagid == groupId.ToString()); if (tag != null) { personGroup.WeixinName = tag.tagname; personGroup.WeixinId = tag.tagid; list.Add(personGroup); } } } return(list); }
/// <summary> /// 设置企业号应用 /// </summary> /// <param name="data"></param> //[AllowAnonymous] //[HttpPost] public ActionResult SetAppInfo(SetAppPostData data, string Id) { //更新SysWechatConfig数据库 //验证错误 if (!BeforeAddOrUpdate(data, Id) || !ModelState.IsValid) { return(Json(GetErrorJson(), JsonRequestBehavior.AllowGet)); } #region 更新菜单 var loginUser = (SysUser)Session["UserInfo"]; var objModel = sys.Repository.GetByKey(int.Parse(Request["ConfigID"])); var menuModel = _sysMenuService.GetMenusByUserID(loginUser, null).Where(a => a.AppId == int.Parse(Request["ConfigID"]) && a.NavigateUrl.Equals("/")).FirstOrDefault(); if (menuModel != null) { menuModel.MenuName = data.name; menuModel.MenuTitle = data.name; _sysMenuService.Repository.Update(menuModel); } #endregion var Config = WeChatCommonService.GetWeChatConfigByID(int.Parse(Request["ConfigID"])); #region 步到微信 //var strToken = Rtntoken(int.Parse(data.agentid)); var strToken = WeChatCommonService.GetWeiXinToken(Config.Id); if (!string.IsNullOrEmpty(Request["MediaID"])) { data.logo_mediaid = Request["MediaID"]; } if (string.IsNullOrEmpty(data.redirect_domain)) { data.redirect_domain = string.Empty; } AppApi.SetApp(strToken, data); #endregion #region 存DB var objNewModel = new SysWechatConfig(); #region 从微信获取App logo, 存入objNewModel try { GetAppInfoResult result = AppApi.GetAppInfo(strToken, int.Parse(Config.WeixinAppId)); objNewModel.CoverUrl = result.square_logo_url; } catch (Exception ex) { _Logger.Error("an error occurd when get app logo :{0}", ex); } #endregion var lst = new List <string>(); objNewModel.Id = objModel.Id; objNewModel.AppName = data.name; if (Request["CorpID"] != objModel.WeixinCorpId && !string.IsNullOrEmpty(Request["Secret"])) { objNewModel.WeixinCorpId = Request["CorpID"]; } if (Request["Secret"] != objModel.WeixinCorpSecret && Request["Secret"] != "******" && !string.IsNullOrEmpty(Request["Secret"])) { objNewModel.WeixinCorpSecret = Request["Secret"]; } if (Request["Welcome"] != objModel.WelcomeMessage) { objNewModel.WelcomeMessage = Request["Welcome"]; } if (Request["Token"] != objModel.WeixinToken && Request["Token"] != "******" && !string.IsNullOrEmpty(Request["Token"])) { objNewModel.WeixinToken = Request["Token"]; } if (Request["EncodingAESKey"] != objModel.WeixinEncodingAESKey && Request["EncodingAESKey"] != "******" && !string.IsNullOrEmpty(Request["EncodingAESKey"])) { objNewModel.WeixinEncodingAESKey = Request["EncodingAESKey"]; } sys.Repository.Update(objNewModel); #endregion #region 清理缓存 //清理缓存 if (WeChatCommonService.lstDepartment(AccountManageID) != null) { cacheManager.Remove("DepartmentList"); } if (WeChatCommonService.lstTag(AccountManageID) != null) { cacheManager.Remove("TagItem"); } if (WeChatCommonService.lstUser(AccountManageID) != null) { cacheManager.Remove("UserItem" + AccountManageID); } if (WeChatCommonService.lstSysWeChatConfig != null) { cacheManager.Remove("SysWeChatConfig"); } var newMenus = _sysMenuService.GetMenusByUserID(loginUser, null); loginUser.Menus = newMenus; Session["UserInfo"] = loginUser; #endregion return(Json(doJson(null))); }
///// <summary> ///// 获取所有App信息 ///// </summary> ///// <param name="appid"></param> ///// <returns></returns> /// <summary> /// 取得并同步一个微信号(企业号)下所有App信息 /// </summary> /// <param name="accountManageId"></param> /// <returns></returns> public List <GetAppInfoResult> GetAndSyncApps(int accountManageId, bool?isSyncMenu) { var result = new List <GetAppInfoResult>(); var appSelected = Request["APPList"]; // ------------从微信服务器获取所有的App,以此为基础同步我们的App管理表 // 取得accessToken var config = WeChatCommonService.lstSysWeChatConfig.FirstOrDefault(a => a.AccountManageId == accountManageId); var accessToken = WeChatCommonService.GetWeiXinToken(config.Id); // 如果是搜索App if (!string.IsNullOrEmpty(appSelected)) { var app = WeChatCommonService.lstSysWeChatConfig.FirstOrDefault(a => a.AccountManageId == accountManageId && a.WeixinAppId == appSelected); var appInfo = AppApi.GetAppInfo(accessToken, int.Parse(appSelected)); // 取得平台内AppId appInfo.agentid = app.Id.ToString(); result.Add(appInfo); return(result); } var wxApps = AppApi.GetAppList(accessToken); var applist = WeChatCommonService.lstSysWeChatConfig.Where(a => a.AccountManageId == accountManageId).ToList(); // 同步App // TODO:目前的效率不高,在每次刷新页面或者翻页时都会重新去同步App _sysWechatConfigService.SyncWechatApps(accountManageId, accessToken, wxApps.agentlist, applist, isSyncMenu ?? true); // 将Session中的Menu更新 var loginUser = (SysUser)Session["UserInfo"]; var newMenus = _sysMenuService.GetMenusByUserID(loginUser, null); loginUser.Menus = newMenus; Session["UserInfo"] = loginUser; // 返回List var newAppList = WeChatCommonService.lstSysWeChatConfig.Where(a => a.AccountManageId == accountManageId).ToList(); //这段代码完全没必要了 //foreach (var newApp in newAppList) //{ // try // { // var appInfo = AppApi.GetAppInfo(accessToken, int.Parse(newApp.WeixinAppId)); // // 前台需要的是平台的AppID,而不是微信的agentId // // 所以此处借用agentid字段来存储appId(因为类GetAppInfoResult已经打包为dll作为底层了,修改的话势必可能会影响到别的地方) // appInfo.agentid = newApp.Id.ToString(); // result.Add(appInfo); // } // catch // { // } //} return(result); }